Spring Security Ldap,仅登录指定组中的用户

2024-04-30

就像标题一样,我希望只有规范的用户。这是我的验证码:

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

    auth.ldapAuthentication().userSearchFilter("(sAMAccountName={0})")
    .contextSource(contextSource());
}

我发现有类似的功能groupSearchFilter and groupSearchBase or groupRoleAttribute但我不知道如何使用它们


我对Megha的解决方案做了一些修改

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Configuration
    protected static class AuthenticationConfiguration extends  GlobalAuthenticationConfigurerAdapter {

        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {              
            DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://ip:port/DC=xxxx,DC=yyyy");
            contextSource.setUserDn("user_service_account");
            contextSource.setPassword("password_user_service_account");
            contextSource.setReferral("follow"); 
            contextSource.afterPropertiesSet();

            LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthenticationProviderConfigurer = auth.ldapAuthentication();

            ldapAuthenticationProviderConfigurer
                .userSearchBase("OU=Users,OU=Servers")
                .userSearchFilter("(&(cn={0})(memberOf=CN=GROUP_NAME,OU=Groups,OU=Servers,DC=xxxx,DC=yyyy))")
                .contextSource(contextSource);
        }
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

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

Spring Security Ldap,仅登录指定组中的用户 的相关文章

随机推荐