詳解Spring Security中獲取當(dāng)前登錄用戶的詳細(xì)信息的幾種方法
在Bean中獲取用戶信息
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
String currentUserName = authentication.getName();
return currentUserName;
}Spring Security 框架提供了多種 AuthenticationToken 的派生類,根據(jù)自己的應(yīng)用場景,可以對 SecurityContextHolder 里面的 AuthenticationToken 進(jìn)行類型轉(zhuǎn)換,如下:
UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); // details里面存放了當(dāng)前登錄用戶的詳細(xì)信息 User userDetails = (User) authenticationToken.getDetails();
PS. AuthenticationToken的類型轉(zhuǎn)換同樣適用于下面提到的Principal類。
在Controller中獲取用戶信息
- 通過Principal參數(shù)獲?。?/li>
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;
@RestController
public class SecurityController {
@GetMapping(value = "/username")
public String currentUserName(Principal principal) {
return principal.getName();
}
}- 通過Authentication參數(shù)獲取:
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;
@RestController
public class SecurityController {
@GetMapping("/username")
@ResponseBody
public String currentUserName(Authentication authentication) {
return authentication.getName();
}
}- 通過HttpServletRequest獲取
import java.security.Principal;
import javax.servlet.http.HttpServletRequest;
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;
@RestController
public class SecurityController {
@GetMapping("/username")
public String currentUserNameSimple(HttpServletRequest request) {
Principal principal = request.getUserPrincipal();
return principal.getName();
}
}通過 Interface 獲取用戶信息
注意:IAuthenticationFacade 這個(gè)接口在springboot2.1.0中已不存在了
通過 Interface 獲取其實(shí)和第一種在 Bean 中獲取用戶信息是一樣的,都是訪問 SecurityContextHolder 獲取的,只是進(jìn)行了封裝。
public interface IAuthenticationFacade {
Authentication getAuthentication();
}
@Component
public class AuthenticationFacade implements IAuthenticationFacade {
@Override
public Authentication getAuthentication() {
return SecurityContextHolder.getContext().getAuthentication();
}
}下面是使用方法:
@RestController
public class SecurityController {
@Autowired
private IAuthenticationFacade authenticationFacade;
@GetMapping("/username")
public String currentUserNameSimple() {
Authentication authentication = authenticationFacade.getAuthentication();
return authentication.getName();
}
}在JSP頁面中獲取用戶信息
要使用 Spring Security 的標(biāo)簽特性,首先要在 JSP 頁面引入 Security 的 tag:
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
通過以下方式可以獲取到當(dāng)前登錄用戶:
<security:authorize access="isAuthenticated()">
authenticated as <security:authentication property="principal.username" />
</security:authorize>到此這篇關(guān)于詳解Spring Security中獲取當(dāng)前登錄用戶的詳細(xì)信息的幾種方法的文章就介紹到這了,更多相關(guān)Spring Securit獲取登錄用戶信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)時(shí)間與字符串互相轉(zhuǎn)換詳解
這篇文章主要為大家詳細(xì)介紹了Java中實(shí)現(xiàn)時(shí)間與字符串互相轉(zhuǎn)換的相關(guān)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
劍指Offer之Java算法習(xí)題精講數(shù)組與字符串
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化2022-03-03
深入探究Java線程的創(chuàng)建與構(gòu)造方法
這篇文章主要給大家分享的是java線程的創(chuàng)建以及構(gòu)造方法,想了解具體方式的小伙伴可以參考下面文章內(nèi)容,希望對你有所幫助2022-04-04
利用Java實(shí)現(xiàn)網(wǎng)站聚合工具
互聯(lián)網(wǎng)上有數(shù)以萬億計(jì)的網(wǎng)站,每個(gè)網(wǎng)站大都具有一定的功能。搜索引擎雖然對互聯(lián)網(wǎng)上的部分網(wǎng)站建立了索引,但是其作為一個(gè)大而全的搜索系統(tǒng),無法很好的定位到一些特殊的需求。因此本文將介紹一個(gè)用java實(shí)現(xiàn)的網(wǎng)站數(shù)據(jù)聚合工具,需要的可以參考一下2022-01-01
詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用
這篇文章主要介紹了詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用的相關(guān)資料,需要的朋友可以參考下2017-02-02
淺談Spring事務(wù)傳播行為實(shí)戰(zhàn)
這篇文章主要介紹了淺談Spring事務(wù)傳播行為實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
springboot+dubbo啟動項(xiàng)目時(shí)報(bào)錯(cuò) zookeeper not connect
這篇文章主要介紹了springboot+dubbo項(xiàng)目啟動項(xiàng)目時(shí)報(bào)錯(cuò) zookeeper not connected的問題,本文給大家定位問題及解決方案,結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2023-06-06

