I was just trying to do the same thing, using user credentials to bind to LDAP directory.
Here is the code of my AuthenticationSource implementation :
Code:
package fr.nancyu.web.sesame.dao;
import org.springframework.ldap.core.AuthenticationSource;
public class LdapAuthenticationSource implements AuthenticationSource {
private static ThreadLocal<String> login = new ThreadLocal<String>();
private static ThreadLocal<String> password = new ThreadLocal<String>();
public static void setLogin(String login) {
LdapAuthenticationSource.login.set(login);
}
public static void setPassword(String password) {
LdapAuthenticationSource.password.set(password);
}
public String getPrincipal() {
String login = LdapAuthenticationSource.login.get();
if(login == null || "".equals(login)) {
return null;
}
return login;
}
public String getCredentials() {
String password = LdapAuthenticationSource.password.get();
if(password == null || "".equals(password)) {
return null;
}
return password;
}
}
Here is the way I'm setting user CN and password before using LdapTemplate :
Code:
LdapAuthenticationSource.setLogin(dn.encode());
LdapAuthenticationSource.setPassword(password);
...
ldapTemplate.search(...);