MaxKeyConfig.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package org.maxkey;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.apache.catalina.Context;
  5. import org.apache.catalina.connector.Connector;
  6. import org.apache.tomcat.util.descriptor.web.SecurityCollection;
  7. import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
  8. import org.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
  9. import org.maxkey.authn.realm.ldap.LdapAuthenticationRealm;
  10. import org.maxkey.authn.realm.ldap.LdapServer;
  11. import org.maxkey.authn.realm.IAuthenticationServer;
  12. import org.maxkey.authn.realm.activedirectory.ActiveDirectoryAuthenticationRealm;
  13. import org.maxkey.authn.realm.activedirectory.ActiveDirectoryServer;
  14. import org.maxkey.authn.support.kerberos.KerberosProxy;
  15. import org.maxkey.authn.support.kerberos.RemoteKerberosService;
  16. import org.maxkey.authz.oauth2.provider.endpoint.TokenEndpointAuthenticationFilter;
  17. import org.maxkey.constants.ConstantsProperties;
  18. import org.maxkey.crypto.password.opt.algorithm.KeyUriFormat;
  19. import org.maxkey.crypto.password.opt.impl.MailOtpAuthn;
  20. import org.maxkey.crypto.password.opt.impl.SmsOtpAuthn;
  21. import org.maxkey.crypto.password.opt.impl.TimeBasedOtpAuthn;
  22. import org.maxkey.crypto.password.opt.impl.sms.SmsOtpAuthnYunxin;
  23. import org.maxkey.persistence.ldap.ActiveDirectoryUtils;
  24. import org.maxkey.persistence.ldap.LdapUtils;
  25. import org.mybatis.spring.annotation.MapperScan;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.beans.factory.InitializingBean;
  29. import org.springframework.beans.factory.annotation.Value;
  30. import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
  31. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  32. import org.springframework.context.annotation.Bean;
  33. import org.springframework.context.annotation.ComponentScan;
  34. import org.springframework.context.annotation.Configuration;
  35. import org.springframework.context.annotation.PropertySource;
  36. import org.springframework.jdbc.core.JdbcTemplate;
  37. @Configuration
  38. //@ImportResource(locations = { "classpath:spring/maxkey.xml" })
  39. @PropertySource(ConstantsProperties.applicationPropertySource)
  40. @PropertySource(ConstantsProperties.maxKeyPropertySource)
  41. @MapperScan("org.maxkey.dao.persistence,")
  42. @ComponentScan(basePackages = {
  43. "org.maxkey.config",
  44. "org.maxkey.domain",
  45. "org.maxkey.domain.apps",
  46. "org.maxkey.domain.userinfo",
  47. "org.maxkey.api.v1.contorller",
  48. "org.maxkey.web.endpoint",
  49. "org.maxkey.web.contorller",
  50. "org.maxkey.web.interceptor",
  51. //single sign on protocol
  52. "org.maxkey.authz.endpoint",
  53. "org.maxkey.authz.desktop.endpoint",
  54. "org.maxkey.authz.exapi.endpoint",
  55. "org.maxkey.authz.formbased.endpoint",
  56. "org.maxkey.authz.ltpa.endpoint",
  57. "org.maxkey.authz.token.endpoint"
  58. })
  59. public class MaxKeyConfig implements InitializingBean {
  60. private static final Logger _logger = LoggerFactory.getLogger(MaxKeyConfig.class);
  61. @Bean
  62. public FilterRegistrationBean<TokenEndpointAuthenticationFilter> TokenEndpointAuthenticationFilter() {
  63. _logger.debug("TokenEndpointAuthenticationFilter init ");
  64. FilterRegistrationBean<TokenEndpointAuthenticationFilter> registration = new FilterRegistrationBean<TokenEndpointAuthenticationFilter>();
  65. registration.setFilter(new TokenEndpointAuthenticationFilter());
  66. registration.addUrlPatterns("/oauth/v20/token/*");
  67. registration.setName("TokenEndpointAuthenticationFilter");
  68. registration.setOrder(1);
  69. return registration;
  70. }
  71. @Bean
  72. public Connector connector() {
  73. Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
  74. connector.setScheme("http");
  75. connector.setPort(80);
  76. connector.setSecure(false);
  77. connector.setRedirectPort(443);
  78. return connector;
  79. }
  80. @Bean
  81. public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
  82. TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
  83. @Override
  84. protected void postProcessContext(Context context) {
  85. SecurityConstraint securityConstraint = new SecurityConstraint();
  86. securityConstraint.setUserConstraint("CONFIDENTIAL");
  87. SecurityCollection collection = new SecurityCollection();
  88. collection.addPattern("/*");
  89. securityConstraint.addCollection(collection);
  90. context.addConstraint(securityConstraint);
  91. }
  92. };
  93. tomcat.addAdditionalTomcatConnectors(connector);
  94. return tomcat;
  95. }
  96. @Bean(name = "keyUriFormat")
  97. public KeyUriFormat keyUriFormat(
  98. @Value("${config.otp.keyuri.format.type:totp}")
  99. String keyuriFormatType,
  100. @Value("${config.otp.keyuri.format.domain:MaxKey.top}")
  101. String keyuriFormatDomain,
  102. @Value("${config.otp.keyuri.format.issuer:MaxKey}")
  103. String keyuriFormatIssuer,
  104. @Value("${config.otp.keyuri.format.digits:6}")
  105. int keyuriFormatDigits,
  106. @Value("${config.otp.keyuri.format.period:30}")
  107. int keyuriFormatPeriod) {
  108. KeyUriFormat keyUriFormat=new KeyUriFormat();
  109. keyUriFormat.setType(keyuriFormatType);
  110. keyUriFormat.setDomain(keyuriFormatDomain);
  111. keyUriFormat.setIssuer(keyuriFormatIssuer);
  112. keyUriFormat.setDigits(keyuriFormatDigits);
  113. keyUriFormat.setPeriod(keyuriFormatPeriod);
  114. _logger.debug("KeyUri Format " + keyUriFormat);
  115. return keyUriFormat;
  116. }
  117. //可以在此实现其他的登陆认证方式,请实现AbstractAuthenticationRealm
  118. @Bean(name = "authenticationRealm")
  119. public JdbcAuthenticationRealm authenticationRealm(
  120. JdbcTemplate jdbcTemplate) {
  121. JdbcAuthenticationRealm authenticationRealm = jdbcAuthenticationRealm(jdbcTemplate);
  122. return authenticationRealm;
  123. }
  124. //JdbcAuthenticationRealm
  125. public JdbcAuthenticationRealm jdbcAuthenticationRealm(
  126. JdbcTemplate jdbcTemplate) {
  127. JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm(jdbcTemplate);
  128. _logger.debug("JdbcAuthenticationRealm inited.");
  129. return authenticationRealm;
  130. }
  131. //LdapAuthenticationRealm
  132. public LdapAuthenticationRealm ldapAuthenticationRealm(
  133. JdbcTemplate jdbcTemplate) {
  134. LdapAuthenticationRealm authenticationRealm = new LdapAuthenticationRealm(jdbcTemplate);
  135. LdapServer ldapServer=new LdapServer();
  136. String providerUrl = "ldap://localhost:389";
  137. String principal = "cn=root";
  138. String credentials = "maxkey";
  139. String baseDN = "dc=maxkey,dc=top";
  140. LdapUtils ldapUtils = new LdapUtils(providerUrl,principal,credentials,baseDN);
  141. ldapServer.setLdapUtils(ldapUtils);
  142. ldapServer.setFilterAttribute("uid");
  143. List<IAuthenticationServer> ldapServers = new ArrayList<IAuthenticationServer>();
  144. ldapServers.add(ldapServer);
  145. authenticationRealm.setLdapServers(ldapServers);
  146. _logger.debug("LdapAuthenticationRealm inited.");
  147. return authenticationRealm;
  148. }
  149. //ActiveDirectoryAuthenticationRealm
  150. public ActiveDirectoryAuthenticationRealm activeDirectoryAuthenticationRealm(
  151. JdbcTemplate jdbcTemplate) {
  152. ActiveDirectoryAuthenticationRealm authenticationRealm = new ActiveDirectoryAuthenticationRealm(jdbcTemplate);
  153. ActiveDirectoryServer ldapServer=new ActiveDirectoryServer();
  154. String providerUrl = "ldap://localhost:389";
  155. String principal = "cn=root";
  156. String credentials = "maxkey";
  157. String domain = "maxkey";
  158. ActiveDirectoryUtils ldapUtils = new ActiveDirectoryUtils(providerUrl,principal,credentials,domain);
  159. ldapServer.setActiveDirectoryUtils(ldapUtils);
  160. List<IAuthenticationServer> ldapServers = new ArrayList<IAuthenticationServer>();
  161. ldapServers.add(ldapServer);
  162. authenticationRealm.setActiveDirectoryServers(ldapServers);
  163. _logger.debug("LdapAuthenticationRealm inited.");
  164. return authenticationRealm;
  165. }
  166. @Bean(name = "tfaOptAuthn")
  167. public TimeBasedOtpAuthn tfaOptAuthn() {
  168. TimeBasedOtpAuthn tfaOptAuthn = new TimeBasedOtpAuthn();
  169. _logger.debug("TimeBasedOtpAuthn inited.");
  170. return tfaOptAuthn;
  171. }
  172. @Bean(name = "tfaMailOptAuthn")
  173. public MailOtpAuthn mailOtpAuthn() {
  174. MailOtpAuthn mailOtpAuthn = new MailOtpAuthn();
  175. _logger.debug("tfaMailOptAuthn inited.");
  176. return mailOtpAuthn;
  177. }
  178. @Bean(name = "tfaMobileOptAuthn")
  179. public SmsOtpAuthn smsOtpAuthn() {
  180. SmsOtpAuthnYunxin smsOtpAuthn = new SmsOtpAuthnYunxin();
  181. _logger.debug("SmsOtpAuthn inited.");
  182. return smsOtpAuthn;
  183. }
  184. @Bean(name = "kerberosService")
  185. public RemoteKerberosService kerberosService(
  186. @Value("${config.support.kerberos.default.userdomain}")
  187. String userDomain,
  188. @Value("${config.support.kerberos.default.fulluserdomain}")
  189. String fullUserDomain,
  190. @Value("${config.support.kerberos.default.crypto}")
  191. String crypto,
  192. @Value("${config.support.kerberos.default.redirecturi}")
  193. String redirectUri
  194. ) {
  195. RemoteKerberosService kerberosService = new RemoteKerberosService();
  196. KerberosProxy kerberosProxy = new KerberosProxy();
  197. kerberosProxy.setCrypto(crypto);
  198. kerberosProxy.setFullUserdomain(fullUserDomain);
  199. kerberosProxy.setUserdomain(userDomain);
  200. kerberosProxy.setRedirectUri(redirectUri);
  201. List<KerberosProxy> kerberosProxysList = new ArrayList<KerberosProxy>();
  202. kerberosProxysList.add(kerberosProxy);
  203. kerberosService.setKerberosProxys(kerberosProxysList);
  204. _logger.debug("RemoteKerberosService inited.");
  205. return kerberosService;
  206. }
  207. @Override
  208. public void afterPropertiesSet() throws Exception {
  209. // TODO Auto-generated method stub
  210. }
  211. }