spring security java配置-多重身份验证管理器
我正在开发一个JavaSpringMVC应用程序。下面是我的setspring security java配置-多重身份验证管理器,java,spring-mvc,spring-security,Java,Spring Mvc,Spring Security,我正在开发一个JavaSpringMVC应用程序。下面是我的setAuthenticationManager的SecurityConfig类的一部分: ... @Autowired private SecurityDAO securityDAO; ... @Override protected void configure(AuthenticationManagerBuilder registry) throws Exception { registry.userDetailsServi
AuthenticationManager
的SecurityConfig
类的一部分:
...
@Autowired
private SecurityDAO securityDAO;
...
@Override
protected void configure(AuthenticationManagerBuilder registry) throws Exception {
registry.userDetailsService(securityDAO).passwordEncoder(new BCryptPasswordEncoder());
}
SecurityDAO
是一个实现UserDetailsService
接口的类
现在,我需要有两种不同的UserDetailsService
接口实现。一个用于admin
用户的url/admin/login
,另一个用于customer
用户的url/customer/login
我找到了一些使用spring实现多身份验证管理器的示例,但它们都使用XMLConfig,我找不到使用JavaConfig的示例
事实上,我想将此配置转换为java配置。扩展AbstractAutowiringFactoryBean并在该类中添加您的实现 它有两个方法,doCreateInstance()知道需要实例化谁的对象,getObjectType()知道这是哪个接口实现 比如说
@Configuration
public class CLass extends AbstractAutowiringFactoryBean<Object> {
public enum Env {
devloper, production
}
Env envType = Env.devloper;
@Override
protected Object doCreateInstance() {
switch (envType) {
case devloper:
return new ClassImpl1();
case production:
rreturn new ClassImpl2();
}
throw new RuntimeException("Unsupported implementation type");
}
@Override
public Class<?> getObjectType() {
return SecurityDAO.class;
}
}
@配置
公共类扩展了AbstractAutowiringFactoryBean{
公共枚举环境{
开发、生产
}
Env envType=Env.devloper;
@凌驾
受保护对象doCreateInstance(){
开关(ENV型){
案例开发者:
返回新的ClassImpl1();
案例制作:
r返回新的ClassImpl2();
}
抛出新的RuntimeException(“不支持的实现类型”);
}
@凌驾
公共类getObjectType(){
返回SecurityDAO.class;
}
}