日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

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

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

SecurityContext

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

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

當(dāng)然這種方式是不夠嚴(yán)謹(jǐn)?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") 

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

Principal

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

 
 
 
 
  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來獲取當(dāng)前用戶的自定義UserDetails對象。如果CustomUser是UserDetails的實(shí)現(xiàn),那么我們可以:

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

更簡單點(diǎn)的話:

 
 
 
 
  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來獲取當(dāng)前用戶的安全上下文,你可以:

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

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

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

HttpServletRequest

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

總結(jié)

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

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


當(dāng)前名稱:Spring Security 實(shí)戰(zhàn)干貨:如何獲取當(dāng)前用戶信息
分享路徑:http://m.5511xx.com/article/coedjed.html