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