diff --git a/src/main/java/com/monkeyk/sos/config/ContextConfigurer.java b/src/main/java/com/monkeyk/sos/config/ContextConfigurer.java new file mode 100644 index 0000000..35ff483 --- /dev/null +++ b/src/main/java/com/monkeyk/sos/config/ContextConfigurer.java @@ -0,0 +1,82 @@ +package com.monkeyk.sos.config; + +import org.apache.commons.dbcp.BasicDataSource; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; + +/** + * 2016/4/4 + *

+ * Replace context.xml, transaction.xml + * + * @author Shengzhao Li + */ +@Configuration +@ComponentScan(basePackages = "com.monkeyk.sos") +@PropertySource(value = {"classpath:spring-oauth-server.properties"}) +@EnableTransactionManagement() +public class ContextConfigurer { + + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + + + @Value("${jdbc.driverClassName}") + private String driverClassName; + + @Value("${jdbc.url}") + private String jdbcUrl; + + @Value("${jdbc.username}") + private String jdbcUsername; + + @Value("${jdbc.password}") + private String jdbcPassword; + + @Bean(name = "dataSource") + public DataSource dataSource() { + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName(driverClassName); + dataSource.setUrl(jdbcUrl); + dataSource.setUsername(jdbcUsername); + dataSource.setPassword(jdbcPassword); + + dataSource.setValidationQuery("SELECT 1"); + dataSource.setTestOnReturn(false); + dataSource.setTestOnBorrow(true); + + dataSource.setMaxActive(20); + dataSource.setMaxIdle(5); + + return dataSource; + } + + + @Bean(name = "transactionManager") + public PlatformTransactionManager transactionManager(DataSource dataSource) { + DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); + transactionManager.setDataSource(dataSource); + return transactionManager; + } + + + @Bean(name = "jdbcTemplate") + public JdbcTemplate jdbcTemplate(DataSource dataSource) { + return new JdbcTemplate(dataSource); + } + + +} diff --git a/src/main/java/com/monkeyk/sos/config/ServletInitializer.java b/src/main/java/com/monkeyk/sos/config/ServletInitializer.java index 52227e0..42619c8 100644 --- a/src/main/java/com/monkeyk/sos/config/ServletInitializer.java +++ b/src/main/java/com/monkeyk/sos/config/ServletInitializer.java @@ -23,7 +23,7 @@ public class ServletInitializer extends AbstractAnnotationConfigDispatcherServle @Override protected Class[] getRootConfigClasses() { - return new Class[]{WebSecurityConfigurer.class}; + return new Class[]{ContextConfigurer.class, WebSecurityConfigurer.class}; } @Override diff --git a/src/main/java/com/monkeyk/sos/config/WebSecurityConfigurer.java b/src/main/java/com/monkeyk/sos/config/WebSecurityConfigurer.java index 122f350..f8fafce 100644 --- a/src/main/java/com/monkeyk/sos/config/WebSecurityConfigurer.java +++ b/src/main/java/com/monkeyk/sos/config/WebSecurityConfigurer.java @@ -1,8 +1,12 @@ package com.monkeyk.sos.config; +import com.monkeyk.sos.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.encoding.Md5PasswordEncoder; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @@ -21,6 +25,9 @@ import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurity public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { + @Autowired + private UserService userService; + @Override public void configure(WebSecurity web) throws Exception { web.expressionHandler(new OAuth2WebSecurityExpressionHandler()); @@ -28,7 +35,7 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { } @Override - @Bean + @Bean(name = "authenticationManager") public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @@ -57,4 +64,11 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(userService) + .passwordEncoder(new Md5PasswordEncoder()); + } + }