javascript
Spring Security中的SecurityContext和SecurityContextHolder是什么?
SecurityContext和SecurityContextHolder是Spring Security的兩個基本類。 SecurityContext用于存儲當前經過身份驗證的用戶的詳細信息,也稱為原理。 因此,如果必須獲取用戶名或任何其他用戶詳細信息,則需要首先獲取此SecurityContext 。 SecurityContextHolder是一個幫助程序類,它提供對安全上下文的訪問。 默認情況下,它使用ThreadLocal對象存儲安全性上下文,這意味著即使不傳遞SecurityContext對象,該安全性上下文也始終可用于同一執行線程中的方法。 不過,不必擔心Web應用程序中的ThreadLocal內存泄漏 ,Spring Security會負責清理ThreadLocal。
順便說一句,這不是SecurityContextHolder可以存儲當前SecurityContext的唯一方法,可以在啟動時為其配置策略,以指定如何存儲上下文。 例如,可以將SecurityContextHolder.MODE_GLOBAL策略用于獨立的應用程序。
要學習的關鍵是, 如何從SecurityContextHolder獲得SecurityContext? 然后從中檢索當前的用戶詳細信息? 例如,如果您想知道當前登錄用戶的用戶名,那么如何在Spring security中獲得該用戶名?
為了獲取當前的用戶名,首先需要一個SecurityContext ,它是從SecurityContextHolder獲得的。 此SecurityContext將用戶詳細信息保留在Authentication對象中,該對象可以通過調用getAuthentication()方法獲得。
一旦獲得身份驗證對象,就可以轉換為UserDetails或按原樣使用它。 UserDetails對象是Spring Security用于保留用戶相關信息的對象。
如何在Spring Security中獲取當前的登錄用戶名
這是獲取Spring安全性中的安全性上下文并獲取當前登錄用戶的名稱的代碼:
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();if (principal instanceof UserDetails) {String username = ((UserDetails)principal).getUsername(); } else {String username = principal.toString(); }getContext()返回的對象是SecurityContext接口的實例。 這是存儲在線程本地存儲中的對象。
getPrincipal()方法通常在Spring Security中返回UserDetails對象,該對象包含當前登錄用戶的所有詳細信息。
無論如何,如果您仔細觀察,您會發現在考慮Spring和依賴注入時,這并不是一個很好的代碼。 因此,如果您需要了解當前登錄的用戶詳細信息(例如在Spring MVC控制器中),建議您聲明一個依賴項,然后讓Spring為您提供Principal對象,而不是查詢它們并創建一個緊密耦合的系統。
這是一個例子
import java.security.Principal; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody;@Controller public class MVCController {@RequestMapping(value = "/username", method = RequestMethod.GET)@ResponseBodypublic String currentUserName(Principal principal) {return principal.getName();}}另外,您也可以要求提供Authentication對象而不是Principal對象,如下所示:
import org.springframework.security.core.Authentication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody;@Controller public class SpringMVCController {@RequestMapping(value = "/username", method = RequestMethod.GET)@ResponseBodypublic String currentUserName(Authentication authentication) {return authentication.getName();} }如果您想了解更多方法,還可以參閱我的文章有關在Spring Security中獲取當前用戶名的3種方法 ,在此我討論了幾種在Spring MVC控制器中獲取當前用戶名的更多方法。
這就是Spring安全性中的安全性上下文,以及如何從SecurityContextHolder類獲取SecurityContext的全部內容。 這些是一些基本類,因此您必須熟悉它們。
存儲部分(即SecurityContext存儲在ThreadLocal是可選的,但最好了解詳細信息。 請記住,如果您需要用戶詳細信息(例如用戶名等),則最好在Spring MVC控制器中請求Principal或Authentication對象,而不要使用SecurityContextHolder來獲取它們。
感謝您到目前為止閱讀本文。 如果您喜歡此Spring Security教程,請與您的朋友和同事分享。 如果您有任何疑問或反饋,請留言。
翻譯自: https://www.javacodegeeks.com/2018/02/securitycontext-securitycontextholder-spring-security.html
總結
以上是生活随笔為你收集整理的Spring Security中的SecurityContext和SecurityContextHolder是什么?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 局域网路由器设置(局域网怎样设置路由器)
- 下一篇: 在Spring WebFlux中创建多个