So far, the methods have only been able to tell the user whether or
not the authentication succeeded. There has been no way of retrieving the
actual exception. Spring LDAP 1.3.1 introduced the
AuthenticationErrorCallback
and a few more
authenticate
methods:
boolean authenticate(Name base, String filter, String
password, AuthenticationErrorCallback errorCallback);
boolean authenticate(String base, String filter, String
password, AuthenticationErrorCallback errorCallback);
boolean authenticate(Name base, String filter, String
password, AuthenticatedLdapEntryContextCallback callback,
AuthenticationErrorCallback errorCallback);
boolean authenticate(String base, String filter, String
password, AuthenticatedLdapEntryContextCallback callback,
AuthenticationErrorCallback errorCallback);
A convenient collecting implementation of the error callback interface is also provided:
public final class CollectingAuthenticationErrorCallback implements AuthenticationErrorCallback { private Exception error; public void execute(Exception e) { this.error = e; } public Exception getError() { return error; } }
The code needed for authenticating a user and retrieving the authentication exception in case of an error boils down to this:
Example 10.3. Authenticating a user and retrieving the authentication exception.
import org.springframework.ldap.core.support.CollectingAuthenticationErrorCallback; ... CollectingAuthenticationErrorCallback errorCallback = new CollectingAuthenticationErrorCallback(); boolean result = ldapTemplate.authenticate("", filter.toString(), "invalidpassword", errorCallback); if (!result) { Exception error = errorCallback.getError(); // error is likely of type org.springframework.ldap.AuthenticationException }