自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Security 實戰(zhàn)干貨:如何獲取當(dāng)前用戶信息

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

[[402489]]

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

SecurityContext

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

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

當(dāng)然這種方式是不夠嚴謹?shù)模绻涌谠试S匿名訪問很可能返回一個匿名用戶,而匿名用戶并不能直接通過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"

其實我平常使用這種方式的最多,我喜歡使用一個抽象的父類控制器來封裝獲取當(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的實現(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來獲取當(dāng)前用戶的安全上下文,你可以:

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

當(dāng)然你還可以通過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()方法也可以,但是我沒有用過,感興趣的同學(xué)可以試試能不能在Spring Security框架中直接通過該方法獲取。

總結(jié)

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

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

 

責(zé)任編輯:武曉燕 來源: 碼農(nóng)小胖哥
相關(guān)推薦

2021-04-23 07:33:10

SpringSecurity單元

2021-04-19 07:33:04

WebSecuritySpringHttpSecurit

2021-01-28 09:50:29

分布式對象SharedObjec

2019-11-22 09:40:40

SpringJava編程語言

2022-03-22 08:03:22

Spring動態(tài)認證數(shù)據(jù)庫

2017-04-28 11:15:26

大數(shù)據(jù)用戶畫像技術(shù)

2021-07-06 23:48:45

.NET用戶信息

2020-09-16 08:07:54

權(quán)限粒度Spring Secu

2021-05-31 10:47:17

SpringSecuritySession

2021-12-28 11:13:05

安全認證 Spring Boot

2021-08-05 10:40:37

加密方案Spring

2022-05-19 11:29:14

計時攻擊SpringSecurity

2021-08-29 18:36:57

項目

2022-08-30 08:50:07

Spring權(quán)限控制

2009-07-21 16:49:41

整合iBatis和SpSqlMapClien

2020-05-18 10:52:10

集群SessionRedis

2022-08-15 08:42:46

權(quán)限控制Spring

2023-04-10 11:41:15

2022-08-30 08:43:11

Spring權(quán)限控制

2022-08-30 08:36:13

Spring權(quán)限控制
點贊
收藏

51CTO技術(shù)棧公眾號