(118) - Add java-config(零配置) 的支持
parent
233492a660
commit
2593161415
|
@ -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
|
||||||
|
* <p/>
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -23,7 +23,7 @@ public class ServletInitializer extends AbstractAnnotationConfigDispatcherServle
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<?>[] getRootConfigClasses() {
|
protected Class<?>[] getRootConfigClasses() {
|
||||||
return new Class[]{WebSecurityConfigurer.class};
|
return new Class[]{ContextConfigurer.class, WebSecurityConfigurer.class};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
package com.monkeyk.sos.config;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
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.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
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 {
|
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configure(WebSecurity web) throws Exception {
|
public void configure(WebSecurity web) throws Exception {
|
||||||
web.expressionHandler(new OAuth2WebSecurityExpressionHandler());
|
web.expressionHandler(new OAuth2WebSecurityExpressionHandler());
|
||||||
|
@ -28,7 +35,7 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Bean
|
@Bean(name = "authenticationManager")
|
||||||
public AuthenticationManager authenticationManagerBean() throws Exception {
|
public AuthenticationManager authenticationManagerBean() throws Exception {
|
||||||
return super.authenticationManagerBean();
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue