Results 1 to 9 of 9

Thread: Reload/Update custom UserDetails without logging out

  1. #1
    Join Date
    Mar 2010
    Posts
    7

    Default Reload/Update custom UserDetails without logging out

    I'm using Spring Security 3 with CAS to manage my authentication.

    I have a requirement where I need to update my custom UserDetails object. This currently holds the authorities and user information like name, address etc.

    Is there a way to modify the UserDetails object and reload it in the context without forcing the user to log in again?

  2. #2
    Join Date
    Jul 2008
    Location
    Washington DC
    Posts
    67

    Default

    I would think all you'd have to do is call loadUserDetails from user UserDetailsService. Maybe the only other piece to it is to update the Authentication object that's in the security context holder. But I'm not quite clear on that part of it. Luke, et al, am I on the right track?

  3. #3
    Join Date
    Jul 2008
    Location
    Washington DC
    Posts
    67

    Default

    Here's a blog about doing this:

    http://blog.lourish.com/2010/03/10/u...ity-in-grails/

    Following on from my post on how to log in a user using the Grails Acegi/Spring Security plugin I stumbled into a new use for the same code when I tried to update a user’s own details while logged in. The security plugin caches the user’s domain object so any changes are not seen until the next login (wholly unhelpful when you’re trying to implement account management on a Website!).
    it's from a grails perspective, but the concept should be the same.

  4. #4
    Join Date
    Mar 2010
    Posts
    7

    Default

    Quote Originally Posted by djKianoosh View Post
    I would think all you'd have to do is call loadUserDetails from user UserDetailsService. Maybe the only other piece to it is to update the Authentication object that's in the security context holder. But I'm not quite clear on that part of it. Luke, et al, am I on the right track?
    Thanks djKianoosh.

    I did try calling loadUserDetails(), but it does not update the userdetails object in the securitycontext. Will look into the Grails implementation and get back...

  5. #5
    Join Date
    Mar 2010
    Posts
    7

    Default

    Quote Originally Posted by djKianoosh View Post
    Here's a blog about doing this:

    http://blog.lourish.com/2010/03/10/u...ity-in-grails/



    it's from a grails perspective, but the concept should be the same.
    The blog uses a simple new UsernamePasswordAuthenticationToken() to update the user. But in my case, I use CAS and I'm I do not think I can get/modify a CasAuthenticationToken to hold the updated userdetails.

    Any pointers on how I can go about?

  6. #6
    Luke Taylor is offline Senior Member Acegi Security System TeamSpring Team
    Join Date
    Aug 2004
    Location
    Glasgow, Scotland
    Posts
    3,449

    Default

    You can store any implementation of the Authentication interface you want in the SecurityContext. It doesn't have to be a CasAuthenticationToken.
    Spring - by Pivotal
    twitter @tekul

  7. #7
    Join Date
    Jul 2008
    Location
    Washington DC
    Posts
    67

    Default

    I dont use CAS, but if it's like any other PreAuth scenario, you can use PreAuthenticatedAuthenticationToken instead of UsernamePasswordAuthenticationToken

    something like..
    Code:
    @Override
    public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException {
    	final PreAuthenticatedAuthenticationToken userToken = (PreAuthenticatedAuthenticationToken) token;
    	logger.debug("Loading userDetails for " + username);
    	String username = userToken.getName();
    	String password = (String) token.getCredentials();
    	return new User(username,password,true,true,true,true,getRoles(username));
    }

  8. #8
    Join Date
    Mar 2010
    Posts
    7

    Default

    Quote Originally Posted by Luke Taylor View Post
    You can store any implementation of the Authentication interface you want in the SecurityContext. It doesn't have to be a CasAuthenticationToken.
    Hi Luke,

    I tried to set my updated userdetails in the authentication object to do something like this..

    SecurityContextHolder.getContext().setAuthenticati on( new someAuthentication());

    Since I use Cas, I thought I would need to use a CasAuthenticationToken.

    Let me know if I'm on the right path to add the updated userDetails to securityContext.
    Last edited by hprasanna84; Apr 14th, 2010 at 04:43 PM.

  9. #9
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    That should be fine. Are you replacing the Authentication object that is already there? Or just updating the UserDetails attached to it?

    Take care when replacing that you don't inadvertently introduce a security risk.
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •