Fork me on GitHub

Spring Security日常问题

页面获取Spring Security登录用户

简介: spring security 把SPRING_SECURITY_CONTEXT 放入了session 没有直接把username 放进去。

  • 前台取值方式一:

    1
    ${session.SPRING_SECURITY_CONTEXT.authentication.principal.username}
  • 前台取值方式二: 在页面端用tag获取

    1
    2
    <%@ taglib prefix='security' uri='http://www.springframework.org/security/tags'%>
    <security:authentication property="principal.username"></security:authentication>

或者

1
2
3
<security:authorize ifAllGranted="ROLE_ADMIN">  
<security:authentication property="principal.username"></security:authentication>
</security:authorize>

  • 后台取值:
    1
    2
    3
    4
    5
    UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()  
    .getAuthentication()
    .getPrincipal();

    userDetails.getUsername()
I'm not rich, but still hold the dream.
显示 Gitment 评论