Spring Security LDAP 身份验证用户必须是 AD 组的成员

2023-11-26

我已经按照以下方式配置了 Spring Boot Security:https://spring.io/guides/gs/secure-web/

我可以完美地使用我的凭据登录。但是,我需要添加一个检查,以确保 AD 用户也必须属于特定的 AD 组(即AD-这是一个特定组). 登录时,如果用户不属于特定的 AD 组,则应返回登录错误。

我已经搜索了几个小时了,似乎找不到一个明确的方法来做到这一点WebSecurityConfigurerAdapter,我使用的是auth.groupSearchFilter正确吗?

这是我的代码:

@Configuration 
@EnableWebSecurity    
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
Environment env;

public LdapContextSource contextSource () {
    LdapContextSource contextSource= new LdapContextSource();

    contextSource.setUrl(env.getRequiredProperty("ldap.url"));
    contextSource.setBase(env.getRequiredProperty("ldap.baseDn"));
    contextSource.setUserDn(env.getRequiredProperty("ldap.bindDn"));
    contextSource.setPassword(env.getRequiredProperty("ldap.batchPassword"));
    contextSource.afterPropertiesSet();
    return contextSource;
}

@Override
protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {
     auth.ldapAuthentication()
        .userSearchFilter("(cn={0})")           
        .groupSearchBase("OU=Account Groups,OU=ITS Security")
        .groupSearchFilter("(cn=AD-this-is-a-specific-group)") 
        .contextSource(contextSource()); 
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().fullyAuthenticated()
        .and()
        .formLogin();
}

我很抱歉迟到了 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”应用程序属性。

所以非常感谢您的帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Security LDAP 身份验证用户必须是 AD 组的成员 的相关文章

随机推荐