String oldCredential = (String)env.get(Context.SECURITY_CREDENTIALS);
String oldUsername = (String)env.get(Context.SECURITY_PRINCIPAL);
String dn = lookupByUid(uid);
if ( dn == null )
throw new SecurityException(new KeyedMessage("User " + uid + " not found"));
// Build user dn using lookup value, just appending the user filter after the uid won't work when users
// are/can be stored in a subtree (searchScope sub-tree)
// The looked up dn though is/should always be correct, just need to append the root context.
if (!StringUtils.isEmpty(getRootContext()))
dn +="," + getRootContext();
env.put(Context.SECURITY_PRINCIPAL,dn);
env.put(Context.SECURITY_CREDENTIALS,password);
new InitialContext(env);
env.put(Context.SECURITY_PRINCIPAL,oldUsername);
env.put(Context.SECURITY_CREDENTIALS,oldCredential);
return true;
}
catch (AuthenticationException e)
{
return false;
}
catch (NamingException e)
{
throw new SecurityException(e);
}
}