Showing Spring Security Principal and Conditional Logic on JSP

If your app is setup with Spring Security, you can show currently logged in user in JSP by using:

${pageContext.request.userPrincipal.name}

You can also check if the user has enough right to access a resource. This example ensures user has enough rights to access /admin or else the link won’t be rendered:


  
  • Admin Home
  • Make sure you have spring-security-taglibs on your classpath:

    
      org.springframework.security
      spring-security-taglibs
      ${org.springframework.security-version}
    
    

    And the tag prefix declared on top of the JSP page:

    <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
    

    Leave a Reply