* @param authentication
* @return
*/
protected DirContextOperations authenticateUsingFilter(
Authentication authentication) {
DirContextOperations user = null;
Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class,
authentication,
"Can only process UsernamePasswordAuthenticationToken objects");
String username = authentication.getName();
String originalUser = username;
String password = (String) authentication.getCredentials();
// format given username if required
if (userFormat != null && !userFormat.equals("")) {
username = MessageFormat.format(userFormat, username);
}
if (!StringUtils.hasLength(password)) {
logger.debug("Rejecting empty password for user " + username);
throw new BadCredentialsException(messages.getMessage(
"BindAuthenticator.emptyPassword", "Empty Password"));
}
DirContext ctx = null;
String userDnStr = "";
try {
ctx = getContextSource().getContext(username, password);
// Check for password policy control
PasswordPolicyControl ppolicy = PasswordPolicyControlExtractor
.extractControl(ctx);
logger.debug("Retrieving user object using filter...");
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
user = SpringSecurityLdapTemplate.searchForSingleEntryInternal(ctx,
searchCtls, "", userFilter, new Object[] { username, originalUser });
userDnStr = user.getDn().toString();
if (ppolicy != null) {
user.setAttributeValue(ppolicy.getID(), ppolicy);
}
} catch (NamingException e) {
// This will be thrown if an invalid user name is used and the
// method may