有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring安全Ldap,仅登录指定组中的用户

就像在标题中一样,我希望只有spec的用户。这是我的身份验证码:

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

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

我发现有groupSearchFiltergroupSearchBasegroupRoleAttribute之类的函数,但我不知道如何使用它们


共 (3) 个答案

  1. # 1 楼答案

    "(sAMAccountName={0})"
    

    应替换为以下内容

    "(&(objectCategory=Person)(sAMAccountName=*)(memberOf=cn=entergroup,ou=users,dc=company,dc=com))"
    

    其中cn、ou、dc是目录中组的规格

  2. # 2 楼答案

    我对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();
        }
    }
    
  3. # 3 楼答案

    这取决于你的团队成员是如何设置的。类似于以下的操作可能会起作用,根据需要替换组dn和对象类:

    groupSearchBase("cn=yourgroup,ou=groups")
    groupSearchFilter("(uniqueMember={0})")