日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Spring Security 實戰(zhàn)干貨:如何獲取當前用戶信息

在某些場景中我們需要獲取當前的用戶是誰?如果你使用了Spring Secrity作為安全框架你可以通過以下手段獲取當前用戶。

創(chuàng)新互聯(lián)成都網(wǎng)站建設專業(yè)公司,是成都網(wǎng)站開發(fā)公司,為服務器租用提供網(wǎng)站建設服務,有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設計服務:原型圖制作、網(wǎng)站創(chuàng)意設計、前端HTML5制作、后臺程序開發(fā)等。成都網(wǎng)站改版熱線:028-86922220

SecurityContext

無論是有狀態(tài)的Session模式還是流行的JWT模式你都可以通過SecurityContext來獲取當前的用戶:

 
 
 
 
  1. Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
  2. String currentPrincipalName = authentication.getName(); 

當然這種方式是不夠嚴謹?shù)?,如果接口允許匿名訪問很可能返回一個匿名用戶,而匿名用戶并不能直接通過getName獲取,所以我們需要優(yōu)化上面的邏輯為:

 
 
 
 
  1. Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
  2. if (!(authentication instanceof AnonymousAuthenticationToken)) { 
  3.     String currentUserName = authentication.getName(); 
  4.     return currentUserName; 
  5. }else{ 
  6.     throw RuntimeException("No User") 

其實我平常使用這種方式的最多,我喜歡使用一個抽象的父類控制器來封裝獲取當前用戶的方法。

Principal

java.security.Principal對象也可以獲取當前的用戶信息,在Spring Security中該對象表現(xiàn)為Authentication對象,如果我們在Spring MVC接口中定義Principal對象也可以獲取當前用戶:

 
 
 
 
  1. @GetMapping("/currentusername") 
  2. public String currentUserName(Principal principal) { 
  3.         return principal.getName(); 

同理Authentication對象也是可以的:

 
 
 
 
  1. @GetMapping("/currentusername") 
  2. public String currentUserName(Authentication authentication) { 
  3.        return authentication.getName(); 

AuthenticationPrincipal

很多時候我們自定義了用戶對象UserDetails, 我們可以通過Spring Security 4.0提供的注解@AuthenticationPrincipal來獲取當前用戶的自定義UserDetails對象。如果CustomUser是UserDetails的實現(xiàn),那么我們可以:

 
 
 
 
  1. @GetMapping("/currentusername") 
  2.  public String currentUserName(@AuthenticationPrincipal CustomUser customUser) { 
  3.    return customUser.getUsername(); 

更簡單點的話:

 
 
 
 
  1. @GetMapping("/currentusername") 
  2.  public String currentUserName(@AuthenticationPrincipal(expression = "username") String username) { 
  3.    return username; 

這需要CustomUser包含一個getUsername方法。

甚至我們自定義一個注解也是可以的:

 
 
 
 
  1. @Target({ElementType.PARAMETER, ElementType.TYPE}) 
  2. @Retention(RetentionPolicy.RUNTIME) 
  3. @Documented 
  4. @AuthenticationPrincipal 
  5. public @interface CurrentUser {} 

CurrentSecurityContext

Spring Security 5 提供了一個新的注解@CurrentSecurityContext來獲取當前用戶的安全上下文,你可以:

 
 
 
 
  1. @GetMapping("/currentusername") 
  2. public String currentUserName(@CurrentSecurityContext(expression = "authentication")  
  3.   Authentication authentication) { 
  4.         return authentication.getName(); 

當然你還可以通過expression參數(shù)聲明SpEL表達式來獲取其它屬性,例如獲取Principal對象:

 
 
 
 
  1. @GetMapping("/principal") 
  2. public String getPrincipal(@CurrentSecurityContext(expression = "authentication.principal")  
  3.   Principal principal) {  
  4.     return principal.getName();  

HttpServletRequest

據(jù)說HttpServletRequest的getUserPrincipal()方法也可以,但是我沒有用過,感興趣的同學可以試試能不能在Spring Security框架中直接通過該方法獲取。

總結

今天總結了如何在Spring Security獲取當前用戶的各種方法,它們的各自場景都略有不同,你可以根據(jù)這些羅列選擇最適合你的應用場景。

本文轉載自微信公眾號「碼農小胖哥」,可以通過以下二維碼關注。轉載本文請聯(lián)系碼農小胖哥公眾號。


分享文章:Spring Security 實戰(zhàn)干貨:如何獲取當前用戶信息
分享URL:http://www.dlmjj.cn/article/coedjed.html