spring security 获取当前用户信息

1.从页面上显示当前登陆的用户名

sec:authentication="name"

<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"><!-- Thymeleaf为我们提供的Spring Security的标签支持 -->
<head>
    <meta content="text/html;charset=UTF-8"/>
    <title sec:authentication="name"></title> <!-- 获得当前用户的用户名 -->

 

2.如果想在程序中获得当前登陆用户对应的对象。

UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
    .getAuthentication()
    .getPrincipal();

 

3.如果想获得当前登陆用户所拥有的所有权限。

GrantedAuthority[] authorities = userDetails.getAuthorities();

 

posted @ 2016-11-16 22:22  Mac.Manon  阅读(23356)  评论(0编辑  收藏  举报