【发布时间】:2015-04-10 16:09:13
【问题描述】:
我的问题是我想要两个身份验证提供程序
之前: 我有我的 UserDetailServiceImpl 并且我针对 DB 验证了用户(不确定它是什么提供者)用户根据 DB 中的数据获得了角色
现在: 我使用 ActiveDirectoryLdapAuthentication 提供程序如下
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
super.configure(auth);
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailService);
}
@Bean
public AuthenticationManager authenticationManager() {
return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
}
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
MyActiveDirectoryLdapAuthenticationProvider provider = new MyActiveDirectoryLdapAuthenticationProvider(domain, url, rootDN);
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
return provider;
}
我让它工作了,所以我能够进行身份验证。
问题:
- 我现在无法使用数据库用户登录,现在只能使用 LDAP。
- 没有使用UserDetailsService,那么用户有什么角色呢?
- 有没有办法向 LDAP 认证用户添加一些默认角色?
那么如何启用这两个提供者呢? 如何向用户添加角色,通过 LDAP auth.provider 进行身份验证?
我也非常感谢对这里发生的事情的“大图”描述(引擎盖下)。这里使用的每个类的作用是什么,真的不清楚它是如何工作的(AuthenticationManager、AuthenticationManagerBuilder、AuthenticationProvider 等)。也许它只是被自动配置隐藏了等等,但即使直接看 Spring Security 也不会让我更聪明。
感谢任何提示
更新 这段代码似乎工作正常
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailService);
}
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
MyActiveDirectoryLdapAuthenticationProvider provider = new MyActiveDirectoryLdapAuthenticationProvider(domain, url, rootDN);
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
provider.setAuthoritiesMapper(new SimpleAuthorityMapper());
return provider;
}
【问题讨论】:
标签: spring security authentication spring-security spring-boot