我很抱歉迟到了 5 年,但我在 Spring Boot 中实现的非常简单的 LDAP 身份验证遇到了完全相同的问题。
我只想要这个:
- 这是正确的用户名吗?
- 密码正确吗?
- 如果是,该 usr 是否在 MYGROUP 组中?
所以我的配置方法现在看起来非常小。我在一个单独的 bean 中添加了填充器,只是意识到我需要将其添加到“auth.ldapAuthentication”中,以便调用它。
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication()
.userSearchFilter("uid={0}")
.ldapAuthoritiesPopulator(ldapAuthoritiesPopulator())
.groupSearchFilter("(member={0})")
.contextSource(contextSource());
}
@Bean
public LdapAuthoritiesPopulator ldapAuthoritiesPopulator() {
DefaultLdapAuthoritiesPopulator populi = new DefaultLdapAuthoritiesPopulator(contextSource(), "") {
@Override
public Set<GrantedAuthority> getGroupMembershipRoles(String userDn, String username) {
Set<GrantedAuthority> groupMembershipRoles = super.getGroupMembershipRoles(userDn, username);
boolean isMemberOfSpecificAdGroup = false;
for (GrantedAuthority grantedAuthority : groupMembershipRoles) {
if ("ROLE_MYGROUP".equals(grantedAuthority.toString())) {
isMemberOfSpecificAdGroup = true;
break;
}
}
if (!isMemberOfSpecificAdGroup) {
throw new BadCredentialsException("User must be a member of " + "ROLE_MYGROUP");
}
return groupMembershipRoles;
}
};
return populi;
}
@Bean
public DefaultSpringSecurityContextSource contextSource() {
return new DefaultSpringSecurityContextSource("ldap://blabla-some-url:389/dc=something,dc=something,dc=ch");
}
顺便说一句:该网址不像中提到的那样工作Spring引导指南它只能这样工作,就像一行中的所有内容一样:
return new DefaultSpringSecurityContextSource("ldap://blabla-some-url:389/dc=something,dc=something,dc=ch");
顺便说一句,对于遵循该指南的每个人来说:如果您连接到现有的 LDAP 服务器,则不需要所有这些“spring.ldap.embedded”应用程序属性。
所以非常感谢您的帮助!