mk2015
mk2015

Reputation: 213

Authentication in Java using Spring LDAP

I'm trying to connect to an AD from Spring LDAP Project

I don't find any method from the DefaultSpringSecurityContextSource to set the 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 didn't found a method like contect.setUserCN().

Upvotes: 3

Views: 3575

Answers (1)

daniel.eichten
daniel.eichten

Reputation: 2555

There should not be a need to set a CN. You just have to specify a managerDN and managerPass as below in context. Security Ldap will then use the context to look for a user which is matching then criteria, retrieving its DN and afterwards trying to issue a bind with the retrieved DN and the given pass.

This is our configuration which is working 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 following your code example context.setUserDN() should be what you are looking for.

Upvotes: 3

Related Questions