Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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().

like image 916
mk2015 Avatar asked Dec 13 '25 06:12

mk2015


1 Answers

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.

like image 66
daniel.eichten Avatar answered Dec 15 '25 20:12

daniel.eichten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!