Java Authentication with Spring LDAP Baeldung

I am trying to connect to AD from Spring LDAP Project

I have not found any method from DefaultSpringSecurityContextSource

to set CN for authentication.

public void init(AuthenticationManagerBuilder auth) throws Exception {
        DefaultSpringSecurityContextSource context = new DefaultSpringSecurityContextSource("ldaps://test.ldaps.com/DC=test,DC=ldaps,DC=com");
        context.setPassword("password");
        context.afterPropertiesSet();
        auth
                .ldapAuthentication()
                .userSearchFilter("(|(objectClass=person)(objectClass=user))")
                .userDnPatterns("uid={0},OU=people)")
                .contextSource(context);
}

      

I haven't found a method like contect.setUserCN()

.

+3


source to share


1 answer


No need to install CN. You just need to specify the dispatcherDN and managerPass as shown below in the context. The security Ldap will then use the context to locate the user, which matches the criteria, retrieves its DN, and then tries to bind with the retrieved DN and the given pass.

This is our configuration and it works fine:



@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityConfigProperties conf;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder authBuilder) throws Exception {
        authBuilder
            .ldapAuthentication()
            .userSearchFilter("(sAMAccountName={0})")
            .userSearchBase("dc=XXXX,dc=XXXXXX,dc=XXX")
            .groupSearchBase("ou=XXXXXXX,dc=XXXX,dc=XXXXXX,dc=XXX")
            .groupSearchFilter("member={0}")
            .contextSource()
                .url(conf.getLdapUrl())
                .port(conf.getLdapPort())
                .managerDn(conf.getBindCn()) 
                .managerPassword(conf.getBindPass());
    }

}

      

But after your example code, there context.setUserDN()

should be what you are looking for.

+3


source







All Articles