spring-bootldapspring-ldap

Springboot LDAP integration


I am trying to integrate LDAP with my Spring-boot application. I can see the following DN for my account in my Active Directory Explorer.

CN=John, Doe,OU=ITS,OU=Users,OU=_OBDE,DC=demodemo,DC=net

We also have a LDAP Attribute called sAMAccountName. This is a username we use to login to our Windows machine as well as other apps. My user name is john-do.

I want to tell Spring-bootthat use this sAMAccountName to login instead of full name.

Currently I have the following in my application and I am able to login if I provide the full username like "John, Doe", but I want to use sAMAccountName (john-do) as a username.

applicaiton.properties: spring.ldap.urls=ldap://162.143.100.202 spring.ldap.base.dn=DC=demodemo,DC=net spring.ldap.base=OU=ITS,OU=Users,OU=_OBDE,DC=demodemo,DC=net

    @Bean
public LdapAuthenticator ldapAuthenticator(BaseLdapPathContextSource contextSource) {
    BindAuthenticator authenticator = new BindAuthenticator(contextSource);
    authenticator.setUserDnPatterns(new String[]{"cn={0},OU=ITS,OU=Users,OU=_OBDE,DC=demodemo,DC=net"});
    return authenticator;
}

@Bean
public AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
    LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
    factory.setUserDnPatterns("cn={0}");
    return factory.createAuthenticationManager();
}

Solution

  • Where you have cn={0}, change those to sAMAccountName={0}.