Results 1 to 8 of 8

Thread: Accessing Extended UserDetails in JSP

  1. #1
    Join Date
    Jan 2010
    Posts
    28

    Question Accessing Extended UserDetails in JSP

    Hi,

    I have a class called AuthenticatedUser which is created when overiding the jdbcDaoImpl method loadUserByUsername, and which contains some additional information I require about the user such as email address, and first & last name etc. The class AuthenticatedUser is returned by the method loadUserByUsername.

    The is all working fine and in my Java src I can access the principal and call any methods of the class including my own, such as getFirstName(), without any problems.

    My question is, how do I access this inside my JSP pages? Say for example I want to welcome my user on the logged in index page 'Welcome X' where X is the first name of the user, not username.

    I am using SpringFramework 3 and Spring Security 2.05.

    Paul.

  2. #2
    Join Date
    Oct 2008
    Location
    Poland, Wrocław
    Posts
    429

    Default

    Hi

    You can access it with EL:
    Code:
    ${pageContext.request.userPrincipal.principal.yourCustomProperty}
    • userPrincipal is an org.springframework.security.core.Authentication object
    • userPrincipal.principal is an org.springframework.security.core.userdetails.User Details object


    regards
    Grzegorz Grzybek

  3. #3
    Join Date
    Jan 2010
    Posts
    28

    Default Solved

    Hi Grzegorz,

    Thanks for your reply.
    I have managed to solve this using the following:

    1. include the security taglib at the top of the JSP.
    2. <sec:authentication property="principal.username"/>

    Regards,
    Paul.

  4. #4
    Join Date
    Oct 2008
    Location
    Poland, Wrocław
    Posts
    429

    Default

    That's the second method

  5. #5
    Join Date
    Feb 2010
    Posts
    2

    Default

    Neither solution works for me. I'm using Spring Security 2.0.3, Icefaces 1.8.1 + Facelets. In backing beans SecurityContextHolder.getContext().getAuthenticati on().getPrincipal() returns the correct information.

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

    Default

    Make sure you have servlet-api-provision enabled.
    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.


  7. #7
    Join Date
    Feb 2010
    Posts
    2

    Default

    unfortunatly it does not work even with servlet-api-provision="true"

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

    Default

    Oh, I didn't see you are using facelets / JSF, so JSP EL may not work for you (nor is it really what you want). You'll probably want to use a backing bean to supply the data, then. The Spring Sec taglibs aren't JSF components.
    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
  •