我的问题是我想要有两个认证提供者。
之前: 我有我的UserDetailServiceImpl并且对用户进行了验证,根据数据库中的数据分配角色,不确定这是什么提供程序。
现在: 我已经使用了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;
}
@Bean public AuthenticationManager authenticationManager()
,它完全停止工作了 :-/ - ZveratkoAuthenticationManagers
。它们以不同的方式和在不同的代码路径中使用。您正在配置的是由您提供的WebSecurityConfigurer
创建的过滤器链的本地AuthenticationManager
。 - Dave Syer