MaxKeyMgtConfig.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 javax.sql.DataSource;
  18. import org.maxkey.authz.oauth2.provider.client.JdbcClientDetailsService;
  19. import org.maxkey.authz.oauth2.provider.token.DefaultTokenServices;
  20. import org.maxkey.authz.oauth2.provider.token.TokenStore;
  21. import org.maxkey.authz.oauth2.provider.token.store.InMemoryTokenStore;
  22. import org.maxkey.authz.oauth2.provider.token.store.JdbcTokenStore;
  23. import org.maxkey.authz.oauth2.provider.token.store.RedisTokenStore;
  24. import org.maxkey.constants.ConstantsProperties;
  25. import org.maxkey.crypto.password.opt.impl.TimeBasedOtpAuthn;
  26. import org.maxkey.jobs.DynamicGroupsJob;
  27. import org.maxkey.persistence.redis.RedisConnectionFactory;
  28. import org.maxkey.persistence.service.GroupsService;
  29. import org.opensaml.xml.ConfigurationException;
  30. import org.quartz.CronScheduleBuilder;
  31. import org.quartz.CronTrigger;
  32. import org.quartz.JobBuilder;
  33. import org.quartz.JobDataMap;
  34. import org.quartz.JobDetail;
  35. import org.quartz.Scheduler;
  36. import org.quartz.SchedulerException;
  37. import org.quartz.TriggerBuilder;
  38. import org.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
  39. import org.slf4j.Logger;
  40. import org.slf4j.LoggerFactory;
  41. import org.springframework.beans.factory.InitializingBean;
  42. import org.springframework.beans.factory.annotation.Value;
  43. import org.springframework.context.annotation.Bean;
  44. import org.springframework.context.annotation.Configuration;
  45. import org.springframework.context.annotation.PropertySource;
  46. import org.springframework.jdbc.core.JdbcTemplate;
  47. import org.springframework.scheduling.quartz.SchedulerFactoryBean;
  48. import org.springframework.security.crypto.password.PasswordEncoder;
  49. @Configuration
  50. @PropertySource(ConstantsProperties.applicationPropertySource)
  51. public class MaxKeyMgtConfig implements InitializingBean {
  52. private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMgtConfig.class);
  53. @Bean(name = "oauth20JdbcClientDetailsService")
  54. public JdbcClientDetailsService JdbcClientDetailsService(
  55. DataSource dataSource,PasswordEncoder passwordReciprocal) {
  56. JdbcClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource);
  57. clientDetailsService.setPasswordEncoder(passwordReciprocal);
  58. _logger.debug("JdbcClientDetailsService inited.");
  59. return clientDetailsService;
  60. }
  61. /**
  62. * TokenStore.
  63. * @param persistence int
  64. * @return oauth20TokenStore
  65. */
  66. @Bean(name = "oauth20TokenStore")
  67. public TokenStore oauth20TokenStore(
  68. @Value("${config.server.persistence}") int persistence,
  69. JdbcTemplate jdbcTemplate,
  70. RedisConnectionFactory jedisConnectionFactory) {
  71. TokenStore tokenStore = null;
  72. if (persistence == 0) {
  73. tokenStore = new InMemoryTokenStore();
  74. _logger.debug("InMemoryTokenStore");
  75. } else if (persistence == 1) {
  76. tokenStore = new JdbcTokenStore(jdbcTemplate);
  77. _logger.debug("JdbcTokenStore");
  78. } else if (persistence == 2) {
  79. tokenStore = new RedisTokenStore(jedisConnectionFactory);
  80. _logger.debug("RedisTokenStore");
  81. }
  82. return tokenStore;
  83. }
  84. /**
  85. * clientDetailsUserDetailsService.
  86. * @return oauth20TokenServices
  87. */
  88. @Bean(name = "oauth20TokenServices")
  89. public DefaultTokenServices DefaultTokenServices(
  90. JdbcClientDetailsService oauth20JdbcClientDetailsService,
  91. TokenStore oauth20TokenStore) {
  92. DefaultTokenServices tokenServices = new DefaultTokenServices();
  93. tokenServices.setClientDetailsService(oauth20JdbcClientDetailsService);
  94. tokenServices.setTokenStore(oauth20TokenStore);
  95. tokenServices.setSupportRefreshToken(true);
  96. return tokenServices;
  97. }
  98. //以下内容可以注释掉后再xml中配置,xml引入在MaxKeyMgtApplication中
  99. @Bean(name = "authenticationRealm")
  100. public JdbcAuthenticationRealm JdbcAuthenticationRealm(
  101. JdbcTemplate jdbcTemplate) {
  102. JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm(jdbcTemplate);
  103. _logger.debug("JdbcAuthenticationRealm inited.");
  104. return authenticationRealm;
  105. }
  106. @Bean(name = "tfaOptAuthn")
  107. public TimeBasedOtpAuthn tfaOptAuthn() {
  108. TimeBasedOtpAuthn tfaOptAuthn = new TimeBasedOtpAuthn();
  109. _logger.debug("TimeBasedOtpAuthn inited.");
  110. return tfaOptAuthn;
  111. }
  112. /**
  113. * schedulerJobsInit.
  114. * @return schedulerJobsInit
  115. * @throws ConfigurationException
  116. * @throws SchedulerException
  117. */
  118. @Bean(name = "schedulerJobs")
  119. public Scheduler schedulerJobs(
  120. SchedulerFactoryBean schedulerFactoryBean,
  121. GroupsService groupsService,
  122. @Value("${config.job.cron.dynamicgroups}") String cronScheduleDynamicGroups
  123. ) throws SchedulerException {
  124. Scheduler scheduler = schedulerFactoryBean.getScheduler();
  125. dynamicGroupsJob(scheduler,cronScheduleDynamicGroups,groupsService);
  126. return scheduler;
  127. }
  128. private void dynamicGroupsJob(Scheduler scheduler ,
  129. String cronSchedule,
  130. GroupsService groupsService) throws SchedulerException {
  131. JobDetail jobDetail =
  132. JobBuilder.newJob(DynamicGroupsJob.class)
  133. .withIdentity("DynamicGroupsJob", "DynamicGroups")
  134. .build();
  135. JobDataMap jobDataMap = new JobDataMap();
  136. jobDataMap.put("groupsService", groupsService);
  137. CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronSchedule);
  138. CronTrigger cronTrigger =
  139. TriggerBuilder.newTrigger()
  140. .withIdentity("triggerDynamicGroups", "DynamicGroups")
  141. .usingJobData(jobDataMap)
  142. .withSchedule(scheduleBuilder)
  143. .build();
  144. scheduler.scheduleJob(jobDetail,cronTrigger);
  145. }
  146. @Override
  147. public void afterPropertiesSet() throws Exception {
  148. }
  149. }