How to authenticate a user using ldap while ignoring NameNotFoundException?

Viewed 1396

I am using Spring Security to create the login page for a web app. The users are registered in Active Directory.

Here is the code I am using:

public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception{
    authenticationManagerBuilder.ldapAuthentication()
            .contextSource().url("ldap:MyLdap")
            .root("ou=MyOu,dc=MyDC,dc=net")
            .port(389)
            .managerDn("cn=MyCN,ou=MyOU,ou=MyOU2,dc=MyDC,dc=net").managerPassword("MyPass")
            .and()
            .userSearchBase("dc=MyDC,dc=net")
            .userSearchFilter("(sAMAccountName={0})");
}

The user search filter is set to sAMAccountName because this is the unique identifier in the Active Directory of my company.

I am able to successfully retrieve the correct user from Active Directory but I get a NameNotFoundException thrown in the LdapTemplate class. The class cas an attribute IgnoreNameNotFoundException. If I set this attribute to true while debugging, the login happens correctly, otherwise I get the error caught and the login fails.

How can I set this attribute from the code? Or is it something I am doing wrong in my configuration?

EDIT:

Here is the stack trace:

org.springframework.ldap.NameNotFoundException: [LDAP: error code 32 - 0000208D: NameErr: DSID-03100213, problem 2001 (NO_OBJECT), data 0, best match of:
""
]; nested exception is javax.naming.NameNotFoundException: [LDAP: error code 32 - 0000208D: NameErr: DSID-03100213, problem 2001 (NO_OBJECT), data 0, best match of:
""
]; remaining name ""
at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:183) ~[spring-ldap-core-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:376) ~[spring-ldap-core-2.3.1.RELEASE.jar:2.3.1.RELEASE]
2 Answers
Related