Results 1 to 3 of 3

Thread: Security Context Holder Jspx

  1. #1

    Default Security Context Holder Jspx

    How can i get already logged in user authority in jspx page? like SecurityContextHolder.getContext().getAuthenticati on().getName()

  2. #2
    Join Date
    Jan 2008
    Posts
    1,826

    Default

    I assume you mean the name from the principal (there is not getName() on Authentication). You can use the Spring Security authentication tag to access properties on the Authentication object. For example the following will be the equivalent of SecurityContextHolder.getAuthentication().getPrinc ipal().getName()

    Code:
    <jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:spring="http://www.springframework.org/tags"
        xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
    
       ...
        <sec:authentication property="principal.name"/>
       ...
    </jsp:root>
    Rob Winch - @rob_winch
    Spring Security Lead
    Pivotal

  3. #3

    Default Thaks for reply

    Quote Originally Posted by rwinch View Post
    I assume you mean the name from the principal (there is not getName() on Authentication). You can use the Spring Security authentication tag to access properties on the Authentication object. For example the following will be the equivalent of SecurityContextHolder.getAuthentication().getPrinc ipal().getName()

    Code:
    <jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:spring="http://www.springframework.org/tags"
        xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
    
       ...
        <sec:authentication property="principal.name"/>
       ...
    </jsp:root>
    According to your reply i got the answer i was loookinf for exactly
    code below .Thanks very much
    <sec:authorize access="hasRole('ROLE_SUPERVISOR')">
    <td>
    <spring:url value="{ownerId}/pets/{petId}/visits/new" var="visitUrl">
    <springaram name="ownerId" value="${owner.id}"/>
    <springaram name="petId" value="${pet.id}"/>
    </spring:url>
    <a href="${fn:escapeXml(visitUrl)}">Add Visit</a>
    </td>
    </sec:authorize>

Tags for this Thread

Posting Permissions

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