@Configuration
public class LdapConfig {
@Value("${spring.ldap.urls}")
private String ldapUrl;
@Value("${spring.ldap.base}")
private String base;
@Value("${spring.ldap.username}")
private String userName;
@Value("${spring.ldap.password}")
private String password;
@Bean
public LdapContextSource ldapContextSource() {
LdapContextSource source = new LdapContextSource();
source.setUrl(this.ldapUrl);
source.setUserDn(this.userName);
source.setPassword(this.password);
source.setBase(this.base);
return source;
}
@Bean
public LdapTemplate ldapTemplate() {
return new LdapTemplate(ldapContextSource());
}
public AuthenticationManager getLdapAuthenticationManager() {
LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(ldapContextSource());
factory.setUserDnPatterns(
"cn={0},ou=member,ou=user"
);
return factory.createAuthenticationManager();
}
}