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();

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM