PDA

View Full Version : Retrieving Password from Security Context



sagi_kiran
Apr 15th, 2010, 04:26 AM
Hi,

I am trying to retrieve the password of the user from the SecurityContext using the below code, but I always seem to get null. Can you please let me know if I am doing something wrong


-----------------------------------------
public UserDetails getUser() {
SecurityContext ctx = SecurityContextHolder.getContext();
UserDetails user = null;
if (ctx != null) {
Authentication auth = ctx.getAuthentication();
if (auth != null) {
Object obj = auth.getPrincipal();
if (obj != null) {
if (UserDetails.class.isAssignableFrom(obj.getClass() )) {
user = (UserDetails) auth.getPrincipal();
}
System.out.println(user.getPassword());

}
}
}
return user;
}

Regards,
Kiran C Sagi.

Luke Taylor
Apr 15th, 2010, 06:31 AM
Have you debugged it to work out why it is null? There are four "if" clauses in there...

Note that SecurityContextHolder.getContext() will not return null, so you don't need a null check there.