赞
踩
有三种情况我们需要配置多个AuthenticationProvider,甚至是自定义AuthenticationProvider:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("primaryDataSource")
private DataSource dataSource1;
@Autowired
@Qualifier("secondaryDataSource")
private DataSource dataSource2;
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/favicon.ico");
}
/**
* 只有这种方法可以配置多个DaoAuthenticationProvider,不然AuthenticationManagerConfiguration就会起作用,详细原因看AuthenticationManagerConfiguration类上的文档。
*
*/
@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
JdbcUserDetailsManager jdbcUserDetailsManager1 = new JdbcUserDetailsManager();
jdbcUserDetailsManager1.setDataSource(dataSource1);
DaoAuthenticationProvider authenticationProvider1 = new DaoAuthenticationProvider();
authenticationProvider1.setUserDetailsService(jdbcUserDetailsManager1);
JdbcUserDetailsManager jdbcUserDetailsManager2 = new JdbcUserDetailsManager();
jdbcUserDetailsManager2.setDataSource(dataSource2);
DaoAuthenticationProvider authenticationProvider2 = new DaoAuthenticationProvider();
authenticationProvider2.setUserDetailsService(jdbcUserDetailsManager2);
ProviderManager authenticationManager = new ProviderManager(
Arrays.asList(authenticationProvider1, authenticationProvider2));
return authenticationManager;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().and().httpBasic().disable().anonymous().disable().authorizeRequests().anyRequest()
.authenticated();
}
}
经测试在AuthenticationManagerBuilder 进行如下的设置是不行的:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource1).and().jdbcAuthentication().dataSource(dataSource2);
}
原因参考AuthenticationManagerConfiguration这个类的文档。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。