Results 1 to 7 of 7

Thread: how to get user info from session?

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Posts
    10

    Default how to get user info from session?

    i select WebWork as my web framework, i want to know if i can get user detail info from session. I woder where can i process in this situation.thanks

  2. #2
    Join Date
    Dec 2004
    Posts
    3

    Default

    SecureContext.getAuthentication().getPrincipal().g etUserName()

  3. #3
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Don't forget to cast:

    Code:
    ((UserDetails) ((SecureContext) ContextHolder.getContext()).getAuthentication().getPrincipal()).getUsername();

  4. #4
    Join Date
    Jan 2005
    Posts
    16

    Default How can I know to Cast to UserDetails? Where to define it?

    Quote Originally Posted by Ben Alex
    Don't forget to cast:

    Code:
    ((UserDetails) ((SecureContext) ContextHolder.getContext()).getAuthentication().getPrincipal()).getUsername();

  5. #5
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Generally people use DaoAuthenticationProvider with Acegi Security, which puts the UserDetails returned by the AuthenticationDao into the Authentication object. If you have a look at the JavaDocs for the DaoAuthenticationProvider and AuthenticationDao, where the UserDetails comes from will make a lot more sense.

  6. #6

    Thumbs up

    Hi All,

    Ok, it's really easy to retrieve the UserDetails. But I want to write an app that isn't too much coupled with Spring.

    Is there another way to retrieve the UserDetail from the session ? Maybe with something like
    Code:
    HttpSession session = request.getSession();
    session.getAttribute("SPRING_USER_DETAILS");
    I have seen that the session has an attribute "SPRING_SECURITY_CONTEXT" of type org.springframework.security.context.SecurityConte xtImpl, is it bad to retrieve the context from the session ?

    If it is, why is it ? If it isn't is there a way to externalize that key ?


    Edit : Actually I have written a static method into a personnal AuthorizationManager
    Code:
    public static User getPrincipal(HttpSession session) {
      // Do nothing with the session because Spring give us a better way to retrieve the principal
      return (User) ((SecurityContext) 
        SecurityContextHolder.getContext()).getAuthentication().getPrincipal();
    }
    , so if later my boss tell me to remove Spring from the project I have juste to rewrite this method. But I don't really like that.
    Last edited by blaiseg; Oct 24th, 2008 at 10:02 AM. Reason: What I do actually

  7. #7
    Join Date
    Oct 2008
    Posts
    2

    Default

    public static User getPrincipal(HttpSession session) {
    // Do nothing with the session because Spring give us a better way to retrieve the principal
    return (User) ((SecurityContext)
    SecurityContextHolder.getContext()).getAuthenticat ion().getPrincipal();
    }
    thanks blaiseg. It works fine. you rescued me

Similar Threads

  1. JBoss 3.2.6 & Spring Deployment issues
    By difranr in forum Container
    Replies: 2
    Last Post: Sep 18th, 2005, 10:08 PM
  2. Replies: 2
    Last Post: Aug 26th, 2005, 12:54 AM
  3. DefaultAdvisorAutoProxyCreator skipping beans
    By youngm in forum Container
    Replies: 6
    Last Post: Apr 12th, 2005, 04:29 PM
  4. Replies: 13
    Last Post: Dec 7th, 2004, 10:00 AM
  5. Replies: 2
    Last Post: Sep 5th, 2004, 10:49 AM

Posting Permissions

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