Get Spring Security Principal in JSP EL expression

后端 未结 11 2058
终归单人心
终归单人心 2020-12-13 04:07

I am using Spring MVC and Spring Security version 3.0.6.RELEASE. What is the easiest way to get the user name in my JSP? Or even just whether or not the user is logged in? I

11条回答
  •  我在风中等你
    2020-12-13 05:08

    I know there are other answers in the thread, but none have answered how you can check if user is authenticated. So I'm sharing what my code look likes.

    Include the tag lib in your project:

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

    Then create a user object in current scope by adding:

    
    

    Then you can easily show the username by adding. Remember the 'principal' object is generally of type string unless you have implemented the spring security in a way to change it to another Class in your project:

    
    ${user}
    
    

    I hope this helps somebody looking to check user roles.

    If you are using Maven, then add the dependency tag as mentioned by Christian Vielma in this thread.

    Thanks!

提交回复
热议问题