Get Spring Security Principal in JSP EL expression

后端 未结 11 2032
终归单人心
终归单人心 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 think will not always work because type returned by Authentication.getPrincipal() is Object, ie: it could be a UserDetail (for which the above will work), a String or anything else.

    For purpose of displaying username in JSP page what I find more reliable is using ${pageContext.request.userPrincipal.name}.

    This uses java.security.Principal.getName() which returns String.

提交回复
热议问题