From 5caaa5516064591a5bfeff601b61161c632af7bb Mon Sep 17 00:00:00 2001 From: shimingxy Date: Sat, 16 May 2020 14:14:46 +0800 Subject: [PATCH] v 1.5.0 RC2 v 1.5.0 RC2 --- .../ApplicationAutoConfiguration.java | 89 +++++++++++++++++++ .../KaptchaAutoConfiguration.java | 11 ++- .../MvcAutoConfiguration.java | 25 +----- .../autoconfigure/RedisAutoConfiguration.java | 71 +++++++++++++++ .../main/java/org/maxkey/MaxKeyMgtConfig.java | 68 ++------------ .../main/resources/META-INF/spring.factories | 7 +- .../main/java/org/maxkey/MaxKeyConfig.java | 38 +------- .../org/maxkey/RedisAutoConfiguration.java | 50 ----------- .../main/resources/META-INF/spring.factories | 7 +- 9 files changed, 189 insertions(+), 177 deletions(-) create mode 100644 maxkey-core/src/main/java/org/maxkey/autoconfigure/ApplicationAutoConfiguration.java rename maxkey-core/src/main/java/org/maxkey/{config => autoconfigure}/KaptchaAutoConfiguration.java (80%) rename maxkey-core/src/main/java/org/maxkey/{config => autoconfigure}/MvcAutoConfiguration.java (90%) create mode 100644 maxkey-core/src/main/java/org/maxkey/autoconfigure/RedisAutoConfiguration.java delete mode 100644 maxkey-web-maxkey/src/main/java/org/maxkey/RedisAutoConfiguration.java diff --git a/maxkey-core/src/main/java/org/maxkey/autoconfigure/ApplicationAutoConfiguration.java b/maxkey-core/src/main/java/org/maxkey/autoconfigure/ApplicationAutoConfiguration.java new file mode 100644 index 000000000..0c701e31c --- /dev/null +++ b/maxkey-core/src/main/java/org/maxkey/autoconfigure/ApplicationAutoConfiguration.java @@ -0,0 +1,89 @@ +package org.maxkey.autoconfigure; + +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; +import java.io.IOException; +import javax.sql.DataSource; +import org.maxkey.authn.RealmAuthenticationProvider; +import org.maxkey.authn.SavedRequestAwareAuthenticationSuccessHandler; +import org.maxkey.crypto.password.PasswordReciprocal; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.core.io.ClassPathResource; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; + +@Configuration +@PropertySource("classpath:/application.properties") +public class ApplicationAutoConfiguration implements InitializingBean { + private static final Logger _logger = + LoggerFactory.getLogger(ApplicationAutoConfiguration.class); + + @Bean + @Primary + @ConfigurationProperties("spring.datasource") + public DataSource dataSource() { + return DruidDataSourceBuilder.create().build(); + } + + /** + * propertySourcesPlaceholderConfigurer . + * @return propertySourcesPlaceholderConfigurer + * @throws IOException null + */ + @Bean (name = "propertySourcesPlaceholderConfigurer") + public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() + throws IOException { + ClassPathResource classPathResource1 = + new ClassPathResource("/config/applicationConfig.properties"); + ClassPathResource classPathResource2 = new ClassPathResource("/application.properties"); + + PropertySourcesPlaceholderConfigurer configurer = + new PropertySourcesPlaceholderConfigurer(); + configurer.setLocations( + classPathResource1, + classPathResource2 + ); + configurer.setIgnoreUnresolvablePlaceholders(true); + _logger.debug("PropertySourcesPlaceholderConfigurer init"); + return configurer; + } + + @Bean(name = "passwordReciprocal") + public PasswordReciprocal passwordReciprocal() { + return new PasswordReciprocal(); + } + + @Bean(name = "savedRequestSuccessHandler") + public SavedRequestAwareAuthenticationSuccessHandler + savedRequestAwareAuthenticationSuccessHandler() { + return new SavedRequestAwareAuthenticationSuccessHandler(); + } + + @Bean(name = "authenticationProvider") + public RealmAuthenticationProvider authenticationProvider() { + return new RealmAuthenticationProvider(); + } + + @Bean(name = "jdbcTemplate") + public JdbcTemplate jdbcTemplate(DataSource dataSource) { + return new JdbcTemplate(dataSource); + } + + @Bean(name = "transactionManager") + DataSourceTransactionManager transactionManager(DataSource dataSource) { + return new DataSourceTransactionManager(dataSource); + } + + @Override + public void afterPropertiesSet() throws Exception { + // TODO Auto-generated method stub + + } +} diff --git a/maxkey-core/src/main/java/org/maxkey/config/KaptchaAutoConfiguration.java b/maxkey-core/src/main/java/org/maxkey/autoconfigure/KaptchaAutoConfiguration.java similarity index 80% rename from maxkey-core/src/main/java/org/maxkey/config/KaptchaAutoConfiguration.java rename to maxkey-core/src/main/java/org/maxkey/autoconfigure/KaptchaAutoConfiguration.java index 993a22249..14d7ebe2d 100644 --- a/maxkey-core/src/main/java/org/maxkey/config/KaptchaAutoConfiguration.java +++ b/maxkey-core/src/main/java/org/maxkey/autoconfigure/KaptchaAutoConfiguration.java @@ -1,4 +1,4 @@ -package org.maxkey.config; +package org.maxkey.autoconfigure; import com.google.code.kaptcha.Producer; import com.google.code.kaptcha.impl.DefaultKaptcha; @@ -7,6 +7,7 @@ import java.io.IOException; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; @@ -14,7 +15,7 @@ import org.springframework.core.io.Resource; @Configuration -public class KaptchaAutoConfiguration { +public class KaptchaAutoConfiguration implements InitializingBean { private static final Logger _logger = LoggerFactory.getLogger(KaptchaAutoConfiguration.class); /** @@ -33,4 +34,10 @@ public class KaptchaAutoConfiguration { kaptcha.setConfig(config); return kaptcha; } + + @Override + public void afterPropertiesSet() throws Exception { + // TODO Auto-generated method stub + + } } diff --git a/maxkey-core/src/main/java/org/maxkey/config/MvcAutoConfiguration.java b/maxkey-core/src/main/java/org/maxkey/autoconfigure/MvcAutoConfiguration.java similarity index 90% rename from maxkey-core/src/main/java/org/maxkey/config/MvcAutoConfiguration.java rename to maxkey-core/src/main/java/org/maxkey/autoconfigure/MvcAutoConfiguration.java index 165da1d20..09ce1d59f 100644 --- a/maxkey-core/src/main/java/org/maxkey/config/MvcAutoConfiguration.java +++ b/maxkey-core/src/main/java/org/maxkey/autoconfigure/MvcAutoConfiguration.java @@ -1,4 +1,4 @@ -package org.maxkey.config; +package org.maxkey.autoconfigure; import java.io.IOException; import java.util.ArrayList; @@ -32,29 +32,6 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl public class MvcAutoConfiguration implements InitializingBean { private static final Logger _logger = LoggerFactory.getLogger(MvcAutoConfiguration.class); - /** - * propertySourcesPlaceholderConfigurer . - * @return propertySourcesPlaceholderConfigurer - * @throws IOException null - */ - @Bean (name = "propertySourcesPlaceholderConfigurer") - public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() - throws IOException { - ClassPathResource classPathResource1 = - new ClassPathResource("/config/applicationConfig.properties"); - ClassPathResource classPathResource2 = new ClassPathResource("/application.properties"); - - PropertySourcesPlaceholderConfigurer configurer = - new PropertySourcesPlaceholderConfigurer(); - configurer.setLocations( - classPathResource1, - classPathResource2 - ); - configurer.setIgnoreUnresolvablePlaceholders(true); - - return configurer; - } - /** * cookieLocaleResolver . * @return cookieLocaleResolver diff --git a/maxkey-core/src/main/java/org/maxkey/autoconfigure/RedisAutoConfiguration.java b/maxkey-core/src/main/java/org/maxkey/autoconfigure/RedisAutoConfiguration.java new file mode 100644 index 000000000..8bfc32e6b --- /dev/null +++ b/maxkey-core/src/main/java/org/maxkey/autoconfigure/RedisAutoConfiguration.java @@ -0,0 +1,71 @@ +package org.maxkey.autoconfigure; + +import org.maxkey.persistence.redis.RedisConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import redis.clients.jedis.JedisPoolConfig; + +@Configuration +@PropertySource("classpath:/application.properties") +public class RedisAutoConfiguration implements InitializingBean { + private static final Logger _logger = LoggerFactory.getLogger(RedisAutoConfiguration.class); + + /** + * RedisConnectionFactory. + * @param host String + * @param port int + * @param timeout int + * @param password String + * @param maxActive int + * @param maxWait int + * @param maxIdle int + * @param minIdle int + * @return RedisConnectionFactory + */ + @Bean + public RedisConnectionFactory redisConnectionFactory( + @Value("${spring.redis.host}") + String host, + @Value("${spring.redis.port}") + int port, + @Value("${spring.redis.timeout}") + int timeout, + @Value("${spring.redis.password}") + String password, + @Value("${spring.redis.lettuce.pool.max-active}") + int maxActive, + @Value("${spring.redis.jedis.pool.max-wait}") + int maxWait, + @Value("${spring.redis.jedis.pool.max-idle}") + int maxIdle, + @Value("${spring.redis.lettuce.pool.min-idle}") + int minIdle) { + _logger.debug("RedisConnectionFactory init ."); + RedisConnectionFactory factory = new RedisConnectionFactory(); + factory.setHostName(host); + factory.setPort(port); + factory.setTimeOut(timeout); + factory.setPassword(password); + + JedisPoolConfig poolConfig = new JedisPoolConfig(); + poolConfig.setMaxIdle(maxIdle); + poolConfig.setMinIdle(minIdle); + poolConfig.setMaxTotal(maxActive); + poolConfig.setMaxWaitMillis(maxWait); + + factory.setPoolConfig(poolConfig); + + return factory; + } + + @Override + public void afterPropertiesSet() throws Exception { + // TODO Auto-generated method stub + + } +} diff --git a/maxkey-web-manage/src/main/java/org/maxkey/MaxKeyMgtConfig.java b/maxkey-web-manage/src/main/java/org/maxkey/MaxKeyMgtConfig.java index 974b7d394..a5c4e3daa 100644 --- a/maxkey-web-manage/src/main/java/org/maxkey/MaxKeyMgtConfig.java +++ b/maxkey-web-manage/src/main/java/org/maxkey/MaxKeyMgtConfig.java @@ -1,27 +1,16 @@ package org.maxkey; -import javax.sql.DataSource; -import org.apache.ibatis.session.SqlSessionFactory; -import org.maxkey.authn.SavedRequestAwareAuthenticationSuccessHandler; -import org.maxkey.crypto.password.PasswordReciprocal; import org.mybatis.spring.annotation.MapperScan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.server.ConfigurableWebServerFactory; import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.PropertySource; import org.springframework.http.HttpStatus; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; @Configuration @PropertySource("classpath:/application.properties") @@ -29,14 +18,6 @@ import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; public class MaxKeyMgtConfig { private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMgtConfig.class); - @Autowired - @Qualifier("dataSource") - DataSource dataSource; - - @Autowired - @Qualifier("sqlSessionFactory") - SqlSessionFactory sqlSessionFactory; - @Value("${server.port:8080}") private int port; @@ -47,49 +28,16 @@ public class MaxKeyMgtConfig { public void setPort(int port) { this.port = port; } - - @Bean - @Primary - @ConfigurationProperties("spring.datasource") - public DataSource dataSource() { - return DruidDataSourceBuilder.create().build(); - } - - @Bean(name = "passwordReciprocal") - public PasswordReciprocal passwordReciprocal() { - return new PasswordReciprocal(); - } - - @Bean(name = "savedRequestSuccessHandler") - public SavedRequestAwareAuthenticationSuccessHandler SavedRequestAwareAuthenticationSuccessHandler() { - return new SavedRequestAwareAuthenticationSuccessHandler(); - } - - @Bean(name = "jdbcTemplate") - public JdbcTemplate jdbcTemplate() { - return new JdbcTemplate(dataSource); - } - /* - @Bean(name = "sqlSession") - public SqlSessionTemplate sqlSession() throws Exception { - return new SqlSessionTemplate(sqlSessionFactory); - }*/ - - @Bean(name = "transactionManager") - DataSourceTransactionManager transactionManager() { - return new DataSourceTransactionManager(dataSource); - } - /** - * 配置默认错误页面(仅用于内嵌tomcat启动时) - * 使用这种方式,在打包为war后不起作用 - * - * @return - */ - @Bean - public WebServerFactoryCustomizer webServerFactoryCustomizer() { - return new WebServerFactoryCustomizer() { + * 配置默认错误页面(仅用于内嵌tomcat启动时) + * 使用这种方式,在打包为war后不起作用 + * + * @return + */ + @Bean + public WebServerFactoryCustomizer webServerFactoryCustomizer() { + return new WebServerFactoryCustomizer() { @Override public void customize(ConfigurableWebServerFactory factory) { _logger.debug("WebServerFactoryCustomizer ... "); diff --git a/maxkey-web-manage/src/main/resources/META-INF/spring.factories b/maxkey-web-manage/src/main/resources/META-INF/spring.factories index eba608e3d..d73316fea 100644 --- a/maxkey-web-manage/src/main/resources/META-INF/spring.factories +++ b/maxkey-web-manage/src/main/resources/META-INF/spring.factories @@ -1,5 +1,6 @@ # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.maxkey.MaxKeyMgtConfig,\ -org.maxkey.config.KaptchaAutoConfiguration,\ -org.maxkey.config.MvcAutoConfiguration +org.maxkey.autoconfigure.ApplicationAutoConfiguration,\ +org.maxkey.autoconfigure.KaptchaAutoConfiguration,\ +org.maxkey.autoconfigure.MvcAutoConfiguration,\ +org.maxkey.MaxKeyMgtConfig diff --git a/maxkey-web-maxkey/src/main/java/org/maxkey/MaxKeyConfig.java b/maxkey-web-maxkey/src/main/java/org/maxkey/MaxKeyConfig.java index a3d229020..8580adc20 100644 --- a/maxkey-web-maxkey/src/main/java/org/maxkey/MaxKeyConfig.java +++ b/maxkey-web-maxkey/src/main/java/org/maxkey/MaxKeyConfig.java @@ -47,12 +47,7 @@ public class MaxKeyConfig { return port; } - @Bean - @Primary - @ConfigurationProperties("spring.datasource") - public DataSource dataSource() { - return DruidDataSourceBuilder.create().build(); - } + @Bean public FilterRegistrationBean TokenEndpointAuthenticationFilter() { @@ -109,16 +104,7 @@ public class MaxKeyConfig { tomcat.addAdditionalTomcatConnectors(connector); return tomcat; } - - @Bean(name = "passwordReciprocal") - public PasswordReciprocal passwordReciprocal() { - return new PasswordReciprocal(); - } - - @Bean(name = "savedRequestSuccessHandler") - public SavedRequestAwareAuthenticationSuccessHandler SavedRequestAwareAuthenticationSuccessHandler() { - return new SavedRequestAwareAuthenticationSuccessHandler(); - } + @Bean(name = "keyUriFormat") public KeyUriFormat keyUriFormat( @@ -143,24 +129,6 @@ public class MaxKeyConfig { return keyUriFormat; } - @Bean(name = "authenticationProvider") - public RealmAuthenticationProvider authenticationProvider() { - return new RealmAuthenticationProvider(); - } - - @Bean(name = "jdbcTemplate") - public JdbcTemplate jdbcTemplate(DataSource dataSource) { - return new JdbcTemplate(dataSource); - } - - @Bean(name = "sqlSession") - public SqlSessionTemplate sqlSession(SqlSessionFactory sqlSessionFactory) throws Exception { - return new SqlSessionTemplate(sqlSessionFactory); - } - - @Bean(name = "transactionManager") - DataSourceTransactionManager transactionManager(DataSource dataSource) { - return new DataSourceTransactionManager(dataSource); - } + } diff --git a/maxkey-web-maxkey/src/main/java/org/maxkey/RedisAutoConfiguration.java b/maxkey-web-maxkey/src/main/java/org/maxkey/RedisAutoConfiguration.java deleted file mode 100644 index 6e256d6ff..000000000 --- a/maxkey-web-maxkey/src/main/java/org/maxkey/RedisAutoConfiguration.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.maxkey; - -import org.maxkey.persistence.redis.RedisConnectionFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; - -import redis.clients.jedis.JedisPoolConfig; - -@Configuration -@PropertySource("classpath:/application.properties") -public class RedisAutoConfiguration { - - @Value("${spring.redis.host}") - private String host; - @Value("${spring.redis.port}") - private int port; - @Value("${spring.redis.timeout}") - private int timeout; - @Value("${spring.redis.password}") - private String password; - @Value("${spring.redis.lettuce.pool.max-active}") - private int maxActive; - @Value("${spring.redis.jedis.pool.max-wait}") - private int maxWait; - @Value("${spring.redis.jedis.pool.max-idle}") - private int maxIdle; - @Value("${spring.redis.lettuce.pool.min-idle}") - private int minIdle; - - @Bean - public RedisConnectionFactory redisConnectionFactory() { - RedisConnectionFactory factory = new RedisConnectionFactory(); - factory.setHostName(host); - factory.setPort(port); - factory.setTimeOut(timeout); - factory.setPassword(password); - - JedisPoolConfig poolConfig = new JedisPoolConfig(); - poolConfig.setMaxIdle(maxIdle); - poolConfig.setMinIdle(minIdle); - poolConfig.setMaxTotal(maxActive); - poolConfig.setMaxWaitMillis(maxWait); - - factory.setPoolConfig(poolConfig); - - return factory; - } -} diff --git a/maxkey-web-maxkey/src/main/resources/META-INF/spring.factories b/maxkey-web-maxkey/src/main/resources/META-INF/spring.factories index d2b16c9d4..b375262a3 100644 --- a/maxkey-web-maxkey/src/main/resources/META-INF/spring.factories +++ b/maxkey-web-maxkey/src/main/resources/META-INF/spring.factories @@ -1,5 +1,6 @@ # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.maxkey.RedisAutoConfiguration,\ -org.maxkey.config.KaptchaAutoConfiguration,\ -org.maxkey.config.MvcAutoConfiguration +org.maxkey.autoconfigure.ApplicationAutoConfiguration,\ +org.maxkey.autoconfigure.MvcAutoConfiguration,\ +org.maxkey.autoconfigure.KaptchaAutoConfiguration,\ +org.maxkey.autoconfigure.RedisAutoConfiguration