Search code examples
springspring-securityldapldap-query

Unable to Authenticate a User with an LDAP Server using LDAP User Search Query with Spring Security


LDAP Server Hierarchy as retrieved via Apache Directory Studio:

Root DSE:

DC=company,DC=com

OU=Offices

OU=Region Offices

OU=Region1 Office

OU=Users

CN=Jayesh Mulwani

WebSecurity is enabled as a part of below class:

public class LDAPSecurityConfig extends WebSecurityConfigurerAdapter{
    protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {                  
            authenticationManagerBuilder.ldapAuthentication()
            .contextSource().url("ldap://server-url/CN=auth_support,OU=Misc,DC=company,DC=com")
            .managerDn("username").managerPassword("password")
            .and()
            .userSearchBase("OU=Offices,DC=company,DC=com")
            .userSearchFilter("(&(objectClass=user)(cn={0}))");
    }
}

Once the application is setup on tomcat, i enter the username along with its password but the authentication fails and no error is prompted.

Can someone please assist me with the approach or specify if i'm missing something here ?


Solution

  • I have fixed the problem by adding the below two conditions

    .groupSearchFilter("(&(objectClass=group)(AccountName={0}))")
    .groupSearchBase("CN=DEPT_All_Employees,OU=DepartmentSecurityGroups,OU=Resources,DC=company,DC=com")
    

    I referred this example to fix the issue http://www.jcombat.com/spring/spring-security-ldap-authentication