偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

[[402489]]

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

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)系碼農小胖哥公眾號。

 

責任編輯:武曉燕 來源: 碼農小胖哥
相關推薦

2021-04-23 07:33:10

SpringSecurity單元

2021-01-28 09:50:29

分布式對象SharedObjec

2021-04-19 07:33:04

WebSecuritySpringHttpSecurit

2019-11-22 09:40:40

SpringJava編程語言

2022-03-22 08:03:22

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

2025-06-30 01:33:00

2017-04-28 11:15:26

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

2021-07-06 23:48:45

.NET用戶信息

2020-09-16 08:07:54

權限粒度Spring Secu

2021-05-31 10:47:17

SpringSecuritySession

2021-12-28 11:13:05

安全認證 Spring Boot

2022-05-19 11:29:14

計時攻擊SpringSecurity

2021-08-05 10:40:37

加密方案Spring

2021-08-29 18:36:57

項目

2022-08-30 08:50:07

Spring權限控制

2025-02-26 09:03:24

2020-06-17 14:40:49

用戶頁面前端

2009-07-21 16:49:41

整合iBatis和SpSqlMapClien

2020-05-18 10:52:10

集群SessionRedis

2022-09-15 07:44:32

Git全局配置本地配置
點贊
收藏

51CTO技術棧公眾號