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

时间:2017-06-02 11:13:21

标签: java spring ldap spring-ldap

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

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

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

我发现有groupSearchFiltergroupSearchBasegroupRoleAttribute等功能,但我不知道如何使用它们

3 个答案:

答案 0 :(得分:1)

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/solitude"
    android:minHeight="120dp">
    <GridView
        android:id="@+id/gvBlock0"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        tools:layout_height="120dp"
        tools:listitem="@layout/list_item_block"/>
</FrameLayout>

应替换为以下

"(sAMAccountName={0})"

其中cn,ou,dc是目录

中组的规范

答案 1 :(得分:0)

这取决于您的群组成员资格的设置方式。类似下面的内容可能会起作用,必要时替换你的组dn和对象类:

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

答案 2 :(得分:0)

我对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();
    }
}