Spring Boot LDAP 检查用户是否属于特定组

时间:2021-04-29 06:22:05

标签: java spring spring-boot active-directory ldap

我正在构建一个应用程序,它只允许我的组织中的一组特定用户。登录。只有属于特定 AD 组的用户才能登录。例如:GDL - MyTeam 是 GDL,只有我想允许的成员才能进入。 我查看了 Atlassian 的 tutorial、confluent 的 tutorial 以及 Megha 的回答 here

与其他堆栈溢出问题相比,我的情况不同的是,我使用的是 ActiveDirectoryLdapAuthenticationProvider,如下面的代码片段所示。那就是决定条款的人。

但是,我的应用程序仍然允许组织中的任何用户进入我的应用程序。我真的无法理解它使用什么标准来允许任何人。 我是 ldiff 语法和使用 Java 过滤 ldap 的新手。结合springboot,真不知道该用群组搜索库还是用户搜索库。我只是希望我的 GDL 的人能够进入。Rest 应该会收到身份验证失败。

这是我的代码文件以供参考:

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {

        configureLdap(auth);
        configureActiveDirectory(auth);

    }

    private void configureLdap(AuthenticationManagerBuilder auth) throws Exception {

        auth
            .ldapAuthentication()
            .contextSource(contextSource())
            .userSearchFilter("(&(objectClass=user)(sAMAccountName=*)(memberOf=cn=GDL-MyTeam,ou=users,dc=myCompany,dc=com)))")
            .passwordCompare()
            .passwordEncoder(passwordEncoder())
            .passwordAttribute("userPassword");
    }

    private void configureActiveDirectory(AuthenticationManagerBuilder auth) {
        ActiveDirectoryLdapAuthenticationProvider adProvider = activeDirectoryLdapAuthenticationProvider();
        if (adProvider != null) {
            auth.authenticationProvider(adProvider);
            auth.eraseCredentials(false);
        }
    }

    @Bean(BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public LdapContextSource contextSource() {

        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl(ldapUrls); //mycompany.com:389
        contextSource.setBase(ldapBaseDn); //dc=myCompany,dc=com
        contextSource.setUserDn(env.getProperty(ldapSecurityPrincipal));
        contextSource.setPassword(env.getProperty(ldapPrincipalPassword));
        contextSource.setReferral("follow");

        contextSource.afterPropertiesSet();

        return contextSource;
    }


    @Bean
    protected ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {


        ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("myCompany.com", ldapUrls,
            ldapBaseDn);
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);
        provider.setUserDetailsContextMapper(new CustomUserDetailsContextMapper());

        return provider;
    }

    @Bean
    public LdapTemplate ldapTemplate() {
        LdapTemplate template = new LdapTemplate();
        template.setContextSource(contextSource());
        template.setIgnoreNameNotFoundException(true);
        template.setIgnorePartialResultException(true);
        return template;
    }


我相信这个过滤器是我指定正确匹配标准的地方,但出于某种原因,它允许所有人,而不仅仅是我团队的特定 GDL。

.userSearchFilter("(&(objectClass=user)(sAMAccountName=*)(memberOf=cn=GDL-MyTeam,ou=users,dc=myCompany,dc=com)))")

任何人都可以就我出错的地方提供指导。 非常感谢!

编辑: 我发现 ActiveDirectoryLdapAuthenticationProvider 决定了条款。我相信这是我需要放入搜索过滤器的地方。如果我放入与其他答案完全相同的过滤器

In order to perform this operation a successful bind must be completed on the connection., data 0, v3839]; remaining name '/'

但是我真的不明白要在这里放什么。请给点建议?

0 个答案:

没有答案