shimingxy 5 роки тому
батько
коміт
5caaa55160

+ 89 - 0
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
+        
+    }
+}

+ 9 - 2
maxkey-core/src/main/java/org/maxkey/config/KaptchaAutoConfiguration.java → 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
+        
+    }
 }

+ 1 - 24
maxkey-core/src/main/java/org/maxkey/config/MvcAutoConfiguration.java → 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;
@@ -33,29 +33,6 @@ 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
      */

+ 71 - 0
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
+        
+    }
+}

+ 8 - 60
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<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
-     return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
+      * 配置默认错误页面(仅用于内嵌tomcat启动时)
+      * 使用这种方式,在打包为war后不起作用
+      *
+      * @return
+      */  
+     @Bean
+     public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
+         return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
          @Override
          public void customize(ConfigurableWebServerFactory factory) {
              _logger.debug("WebServerFactoryCustomizer ... ");

+ 4 - 3
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

+ 3 - 35
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> 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);
-    }
+
     
 }

+ 0 - 50
maxkey-web-maxkey/src/main/java/org/maxkey/RedisAutoConfiguration.java

@@ -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;
-    }
-}

+ 4 - 3
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