【发布时间】:2015-11-12 22:39:21
【问题描述】:
我一直在尝试配置 Spring Security 以使用 LDAP,但收效甚微。
我有以下配置bean:
@Bean
public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("go.com.mt", "LDAP://CORPORATE.INTRA");
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
provider.setUserDetailsContextMapper(userDetailsContextMapper());
return provider;
}
@Bean
public UserDetailsContextMapper userDetailsContextMapper() {
UserDetailsContextMapper contextMapper = new AttributesLDAPUserDetailsContextMapper();
return contextMapper;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
}
我尝试按照此处关于堆栈溢出的许多答案的建议创建自定义映射器,将每个权限设置为 ROLE_USER
public class AttributesLDAPUserDetailsContextMapper implements UserDetailsContextMapper {
@Override
public UserDetails mapUserFromContext(DirContextOperations dirContextOperations, String username, Collection<? extends GrantedAuthority> authority) {
List<GrantedAuthority> mappedAuthorities = new ArrayList<GrantedAuthority>();
for (GrantedAuthority granted : authority) {
if (true) {
mappedAuthorities.add(() -> "ROLE_USER");
} else if(granted.getAuthority().equalsIgnoreCase("MY ADMIN GROUP")) {
mappedAuthorities.add(() -> "ROLE_ADMIN");
}
}
return new User(username, "", mappedAuthorities);
}
@Override
public void mapUserToContext(UserDetails userDetails, DirContextAdapter dirContextAdapter) {
}
}
当我尝试使用现有用户和错误密码进行身份验证时,我收到以下消息:
[apr-8080-exec-6] ctiveDirectoryLdapAuthenticationProvider : Active Directory authentication failed: Supplied password was invalid
[apr-8080-exec-6] o.s.b.a.audit.listener.AuditListener : AuditEvent [timestamp=Thu Aug 20 07:31:59 CEST 2015, principal=samantha.catania, type=AUTHENTICATION_FAILURE, data={type=org.springframework.security.authentication.BadCredentialsException, message=Bad credentials}]
表示活动目录正在正常工作,但是当我尝试使用正确的凭据进行身份验证时,我收到以下消息:
[pr-8080-exec-10] o.s.s.ldap.SpringSecurityLdapTemplate : Ignoring PartialResultException
[pr-8080-exec-10] o.s.b.a.audit.listener.AuditListener : AuditEvent [timestamp=Thu Aug 20 07:32:05 CEST 2015, principal=samantha.catania, type=AUTHENTICATION_FAILURE, data={type=org.springframework.security.authentication.BadCredentialsException, message=Bad credentials}]
有什么办法可以解决这个问题吗?
【问题讨论】:
-
检查是否收藏 extends GrantedAuthority> 权限为空或为空。让我知道。
-
我在两种映射器方法中都添加了日志,但它们从未被打印出来。我还添加了断点,它永远不会停在那里@.@
-
在哪里以及使用哪些映射器方法.. 你能更精确一点... /跨度>
-
在我更新并设置了 DN 之后,映射器开始被调用。似乎错误是在映射发生之前发生的
标签: java spring spring-security active-directory ldap