MaxKeyConfig.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
  20. import org.maxkey.authn.realm.ldap.LdapAuthenticationRealm;
  21. import org.maxkey.authn.realm.ldap.LdapServer;
  22. import org.maxkey.authn.realm.IAuthenticationServer;
  23. import org.maxkey.authn.realm.activedirectory.ActiveDirectoryAuthenticationRealm;
  24. import org.maxkey.authn.realm.activedirectory.ActiveDirectoryServer;
  25. import org.maxkey.authn.support.kerberos.KerberosProxy;
  26. import org.maxkey.authn.support.kerberos.RemoteKerberosService;
  27. import org.maxkey.authn.support.rememberme.AbstractRemeberMeService;
  28. import org.maxkey.constants.ConstantsPersistence;
  29. import org.maxkey.constants.ConstantsProperties;
  30. import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
  31. import org.maxkey.password.onetimepwd.algorithm.KeyUriFormat;
  32. import org.maxkey.password.onetimepwd.impl.MailOtpAuthn;
  33. import org.maxkey.password.onetimepwd.impl.SmsOtpAuthn;
  34. import org.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
  35. import org.maxkey.password.onetimepwd.impl.sms.SmsOtpAuthnAliyun;
  36. import org.maxkey.password.onetimepwd.impl.sms.SmsOtpAuthnTencentCloud;
  37. import org.maxkey.password.onetimepwd.impl.sms.SmsOtpAuthnYunxin;
  38. import org.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
  39. import org.maxkey.persistence.db.LoginHistoryService;
  40. import org.maxkey.persistence.db.LoginService;
  41. import org.maxkey.persistence.db.PasswordPolicyValidator;
  42. import org.maxkey.persistence.ldap.ActiveDirectoryUtils;
  43. import org.maxkey.persistence.ldap.LdapUtils;
  44. import org.maxkey.persistence.redis.RedisConnectionFactory;
  45. import org.mybatis.spring.annotation.MapperScan;
  46. import org.slf4j.Logger;
  47. import org.slf4j.LoggerFactory;
  48. import org.springframework.beans.factory.InitializingBean;
  49. import org.springframework.beans.factory.annotation.Value;
  50. import org.springframework.context.annotation.Bean;
  51. import org.springframework.context.annotation.ComponentScan;
  52. import org.springframework.context.annotation.Configuration;
  53. import org.springframework.context.annotation.PropertySource;
  54. import org.springframework.jdbc.core.JdbcTemplate;
  55. import org.springframework.security.crypto.password.PasswordEncoder;
  56. @Configuration
  57. //@ImportResource(locations = { "classpath:spring/maxkey.xml" })
  58. @PropertySource(ConstantsProperties.applicationPropertySource)
  59. @PropertySource(ConstantsProperties.maxKeyPropertySource)
  60. @ComponentScan(basePackages = {
  61. "org.maxkey.configuration",
  62. "org.maxkey.domain",
  63. "org.maxkey.domain.apps",
  64. "org.maxkey.domain.userinfo",
  65. "org.maxkey.api.v1.contorller",
  66. "org.maxkey.web.endpoint",
  67. "org.maxkey.web.contorller",
  68. "org.maxkey.web.interceptor",
  69. //single sign on protocol
  70. "org.maxkey.authz.endpoint",
  71. "org.maxkey.authz.desktop.endpoint",
  72. "org.maxkey.authz.exapi.endpoint",
  73. "org.maxkey.authz.formbased.endpoint",
  74. "org.maxkey.authz.ltpa.endpoint",
  75. "org.maxkey.authz.token.endpoint"
  76. })
  77. public class MaxKeyConfig implements InitializingBean {
  78. private static final Logger _logger = LoggerFactory.getLogger(MaxKeyConfig.class);
  79. @Bean(name = "keyUriFormat")
  80. public KeyUriFormat keyUriFormat(
  81. @Value("${config.otp.keyuri.format.type:totp}")
  82. String keyuriFormatType,
  83. @Value("${config.otp.keyuri.format.domain:MaxKey.top}")
  84. String keyuriFormatDomain,
  85. @Value("${config.otp.keyuri.format.issuer:MaxKey}")
  86. String keyuriFormatIssuer,
  87. @Value("${config.otp.keyuri.format.digits:6}")
  88. int keyuriFormatDigits,
  89. @Value("${config.otp.keyuri.format.period:30}")
  90. int keyuriFormatPeriod) {
  91. KeyUriFormat keyUriFormat=new KeyUriFormat();
  92. keyUriFormat.setType(keyuriFormatType);
  93. keyUriFormat.setDomain(keyuriFormatDomain);
  94. keyUriFormat.setIssuer(keyuriFormatIssuer);
  95. keyUriFormat.setDigits(keyuriFormatDigits);
  96. keyUriFormat.setPeriod(keyuriFormatPeriod);
  97. _logger.debug("KeyUri Format " + keyUriFormat);
  98. return keyUriFormat;
  99. }
  100. //可以在此实现其他的登陆认证方式,请实现AbstractAuthenticationRealm
  101. @Bean(name = "authenticationRealm")
  102. public JdbcAuthenticationRealm authenticationRealm(
  103. PasswordEncoder passwordEncoder,
  104. PasswordPolicyValidator passwordPolicyValidator,
  105. LoginService loginService,
  106. LoginHistoryService loginHistoryService,
  107. AbstractRemeberMeService remeberMeService,
  108. JdbcTemplate jdbcTemplate) {
  109. JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm(
  110. passwordEncoder,
  111. passwordPolicyValidator,
  112. loginService,
  113. loginHistoryService,
  114. remeberMeService,
  115. jdbcTemplate);
  116. return authenticationRealm;
  117. }
  118. //LdapAuthenticationRealm
  119. public LdapAuthenticationRealm ldapAuthenticationRealm(
  120. JdbcTemplate jdbcTemplate) {
  121. LdapAuthenticationRealm authenticationRealm = new LdapAuthenticationRealm(jdbcTemplate);
  122. LdapServer ldapServer=new LdapServer();
  123. String providerUrl = "ldap://localhost:389";
  124. String principal = "cn=root";
  125. String credentials = "maxkey";
  126. String baseDN = "dc=maxkey,dc=top";
  127. LdapUtils ldapUtils = new LdapUtils(providerUrl,principal,credentials,baseDN);
  128. ldapServer.setLdapUtils(ldapUtils);
  129. ldapServer.setFilterAttribute("uid");
  130. List<IAuthenticationServer> ldapServers = new ArrayList<IAuthenticationServer>();
  131. ldapServers.add(ldapServer);
  132. authenticationRealm.setLdapServers(ldapServers);
  133. _logger.debug("LdapAuthenticationRealm inited.");
  134. return authenticationRealm;
  135. }
  136. //ActiveDirectoryAuthenticationRealm
  137. public ActiveDirectoryAuthenticationRealm activeDirectoryAuthenticationRealm(
  138. JdbcTemplate jdbcTemplate) {
  139. ActiveDirectoryAuthenticationRealm authenticationRealm = new ActiveDirectoryAuthenticationRealm(jdbcTemplate);
  140. ActiveDirectoryServer ldapServer=new ActiveDirectoryServer();
  141. String providerUrl = "ldap://localhost:389";
  142. String principal = "cn=root";
  143. String credentials = "maxkey";
  144. String domain = "maxkey";
  145. ActiveDirectoryUtils ldapUtils = new ActiveDirectoryUtils(providerUrl,principal,credentials,domain);
  146. ldapServer.setActiveDirectoryUtils(ldapUtils);
  147. List<IAuthenticationServer> ldapServers = new ArrayList<IAuthenticationServer>();
  148. ldapServers.add(ldapServer);
  149. authenticationRealm.setActiveDirectoryServers(ldapServers);
  150. _logger.debug("LdapAuthenticationRealm inited.");
  151. return authenticationRealm;
  152. }
  153. @Bean(name = "timeBasedOtpAuthn")
  154. public TimeBasedOtpAuthn timeBasedOtpAuthn() {
  155. TimeBasedOtpAuthn tfaOtpAuthn = new TimeBasedOtpAuthn();
  156. _logger.debug("TimeBasedOtpAuthn inited.");
  157. return tfaOtpAuthn;
  158. }
  159. //default tfaOtpAuthn
  160. @Bean(name = "tfaOtpAuthn")
  161. public AbstractOtpAuthn tfaOptAuthn(
  162. @Value("${config.login.mfa.type}")String mfaType,
  163. @Value("${config.server.persistence}") int persistence,
  164. MailOtpAuthn tfaMailOtpAuthn,
  165. RedisConnectionFactory redisConnFactory) {
  166. AbstractOtpAuthn tfaOtpAuthn = null;
  167. if(mfaType.equalsIgnoreCase("SmsOtpAuthnAliyun")) {
  168. tfaOtpAuthn = new SmsOtpAuthnAliyun();
  169. _logger.debug("SmsOtpAuthnAliyun inited.");
  170. }else if(mfaType.equalsIgnoreCase("SmsOtpAuthnTencentCloud")) {
  171. tfaOtpAuthn = new SmsOtpAuthnTencentCloud();
  172. _logger.debug("SmsOtpAuthnTencentCloud inited.");
  173. }else if(mfaType.equalsIgnoreCase("SmsOtpAuthnYunxin")) {
  174. tfaOtpAuthn = new SmsOtpAuthnYunxin();
  175. _logger.debug("SmsOtpAuthnYunxin inited.");
  176. }else if(mfaType.equalsIgnoreCase("MailOtpAuthn")) {
  177. tfaOtpAuthn = tfaMailOtpAuthn;
  178. _logger.debug("MailOtpAuthn inited.");
  179. }else {
  180. tfaOtpAuthn = new TimeBasedOtpAuthn();
  181. _logger.debug("TimeBasedOtpAuthn inited.");
  182. }
  183. if (persistence == ConstantsPersistence.REDIS) {
  184. RedisOtpTokenStore redisOptTokenStore = new RedisOtpTokenStore(redisConnFactory);
  185. tfaOtpAuthn.setOptTokenStore(redisOptTokenStore);
  186. }
  187. tfaOtpAuthn.initPropertys();
  188. return tfaOtpAuthn;
  189. }
  190. @Bean(name = "tfaMailOtpAuthn")
  191. public MailOtpAuthn mailOtpAuthn(
  192. @Value("${spring.mail.properties.mailotp.message.subject}")
  193. String messageSubject,
  194. @Value("${spring.mail.properties.mailotp.message.template}")
  195. String messageTemplate
  196. ) {
  197. MailOtpAuthn mailOtpAuthn = new MailOtpAuthn();
  198. mailOtpAuthn.setSubject(messageSubject);
  199. mailOtpAuthn.setMessageTemplate(messageTemplate);
  200. _logger.debug("tfaMailOtpAuthn inited.");
  201. return mailOtpAuthn;
  202. }
  203. @Bean(name = "tfaMobileOtpAuthn")
  204. public SmsOtpAuthn smsOtpAuthn(
  205. @Value("${config.otp.sms}")String optSmsProvider,
  206. @Value("${config.server.persistence}") int persistence,
  207. RedisConnectionFactory redisConnFactory) {
  208. SmsOtpAuthn smsOtpAuthn = null;
  209. if(optSmsProvider.equalsIgnoreCase("SmsOtpAuthnAliyun")) {
  210. smsOtpAuthn = new SmsOtpAuthnAliyun();
  211. }else if(optSmsProvider.equalsIgnoreCase("SmsOtpAuthnTencentCloud")) {
  212. smsOtpAuthn = new SmsOtpAuthnTencentCloud();
  213. }else {
  214. smsOtpAuthn = new SmsOtpAuthnYunxin();
  215. }
  216. if (persistence == ConstantsPersistence.REDIS) {
  217. RedisOtpTokenStore redisOptTokenStore = new RedisOtpTokenStore(redisConnFactory);
  218. smsOtpAuthn.setOptTokenStore(redisOptTokenStore);
  219. }
  220. smsOtpAuthn.initPropertys();
  221. _logger.debug("SmsOtpAuthn inited.");
  222. return smsOtpAuthn;
  223. }
  224. @Bean(name = "kerberosService")
  225. public RemoteKerberosService kerberosService(
  226. @Value("${config.support.kerberos.default.userdomain}")
  227. String userDomain,
  228. @Value("${config.support.kerberos.default.fulluserdomain}")
  229. String fullUserDomain,
  230. @Value("${config.support.kerberos.default.crypto}")
  231. String crypto,
  232. @Value("${config.support.kerberos.default.redirecturi}")
  233. String redirectUri
  234. ) {
  235. RemoteKerberosService kerberosService = new RemoteKerberosService();
  236. KerberosProxy kerberosProxy = new KerberosProxy();
  237. kerberosProxy.setCrypto(crypto);
  238. kerberosProxy.setFullUserdomain(fullUserDomain);
  239. kerberosProxy.setUserdomain(userDomain);
  240. kerberosProxy.setRedirectUri(redirectUri);
  241. List<KerberosProxy> kerberosProxysList = new ArrayList<KerberosProxy>();
  242. kerberosProxysList.add(kerberosProxy);
  243. kerberosService.setKerberosProxys(kerberosProxysList);
  244. _logger.debug("RemoteKerberosService inited.");
  245. return kerberosService;
  246. }
  247. @Override
  248. public void afterPropertiesSet() throws Exception {
  249. // TODO Auto-generated method stub
  250. }
  251. }