简体   繁体   English

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

[英]Spring Boot LDAP check if a user belongs to a specific group

I'm building an application which will allow only a specific set of users in my org.我正在构建一个应用程序,它只允许我组织中的一组特定用户。 to login.登录。 Only those users that belong to a particular AD Group can login.只有属于特定 AD 组的用户才能登录。 Eg: GDL - MyTeam is a GDL, only who's members I want to allow to get in. I checked out Atlassian's tutorial , and confluent's tutorial as well as Megha's answer here .例如:GDL - MyTeam 是一个 GDL,只有我想允许的成员才能进入。我在这里查看了 Atlassian 的教程、confluent 的教程以及 Megha 的回答。

What is different in my case, compared to other stack overflow questions is that I'm using ActiveDirectoryLdapAuthenticationProvider as can be seen in my code snippet below.在我的案例中,与其他堆栈溢出问题相比,不同之处在于我使用的是ActiveDirectoryLdapAuthenticationProvider ,如下面的代码片段所示。 That is the one to be dictating the terms.那就是口述条款的人。

However, My application would still allow any user in the org to get in to my application.但是,我的应用程序仍然允许组织中的任何用户进入我的应用程序。 I'm really not able to understand what criteria is it using to allow anyone.我真的无法理解它使用什么标准来允许任何人。 I'm a totally newbie to ldiff syntax and filtering ldap using Java. Combine with springboot, I really don't know if I should use group search base or user search base.我是 ldiff 语法和使用 Java 过滤 ldap 的新手。结合 springboot,我真的不知道我应该使用组搜索库还是用户搜索库。 I just want people of my GDL to be able to get in. Rest should receive an authentication failure.我只希望我的 GDL 的人能够进入。Rest 应该收到身份验证失败。

Here is my code file for reference:这是我的代码文件供参考:

    @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;
    }


I believe this filter is the place where I have specified the correct matching criteria, but for some reason it's allowing everyone and not just My team's specific GDL.我相信这个过滤器是我指定正确匹配标准的地方,但出于某种原因,它允许所有人,而不仅仅是我团队的特定 GDL。

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

Can anyone please provide guidance as to where I am going wrong.任何人都可以就我要去哪里出错提供指导。 Thanks a ton!万分感谢!

EDIT: I figured out that ActiveDirectoryLdapAuthenticationProvider is dictating the terms.编辑:我发现ActiveDirectoryLdapAuthenticationProvider正在规定条款。 I believe this is the place where I need to put in the search filter.我相信这是我需要放入搜索过滤器的地方。 If I put in the exact same filter as the other answers如果我放入与其他答案完全相同的过滤器

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

But I really don't understand what to put in here.但我真的不明白要放在这里什么。 Suggestions please?请提出建议?

I wonder if this might be the issue:我想知道这是否可能是问题所在:

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

You use sAMAccountName=* (with the *).您使用sAMAccountName=* (带有 *)。 Looks like wild card to me, meaning anyone?.对我来说看起来像通配符,意思是任何人? What if you replace that with {1} like in如果您将其替换为{1}会怎么样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM