欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Spring Security中獲取當(dāng)前登錄用戶的詳細(xì)信息的幾種方法

 更新時(shí)間:2022年05月09日 10:08:25   作者:R先生專欄  
本文主要介紹了詳解Spring Security中獲取當(dāng)前登錄用戶的詳細(xì)信息的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

在Bean中獲取用戶信息

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
    String currentUserName = authentication.getName();
    return currentUserName;
}

Spring Security 框架提供了多種 AuthenticationToken 的派生類,根據(jù)自己的應(yīng)用場(chǎng)景,可以對(duì) 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中獲取用戶信息

  • 通過(guò)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();
    }
}
  • 通過(guò)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();
    }
}
  • 通過(guò)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();
    }
}

通過(guò) Interface 獲取用戶信息

注意:IAuthenticationFacade 這個(gè)接口在springboot2.1.0中已不存在了

通過(guò) Interface 獲取其實(shí)和第一種在 Bean 中獲取用戶信息是一樣的,都是訪問(wèn) 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頁(yè)面中獲取用戶信息

要使用 Spring Security 的標(biāo)簽特性,首先要在 JSP 頁(yè)面引入 Security 的 tag:

<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>

通過(guò)以下方式可以獲取到當(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 巧妙mybatis避免Where 空條件的尷尬

    巧妙mybatis避免Where 空條件的尷尬

    這篇文章主要介紹了巧妙mybatis避免Where 空條件的尷尬,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • Spring Boot Mysql 數(shù)據(jù)庫(kù)操作示例

    Spring Boot Mysql 數(shù)據(jù)庫(kù)操作示例

    本篇文章主要介紹了Spring Boot Mysql 數(shù)據(jù)庫(kù)操作示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • java——多線程基礎(chǔ)

    java——多線程基礎(chǔ)

    Java多線程實(shí)現(xiàn)方式有兩種,第一種是繼承Thread類,第二種是實(shí)現(xiàn)Runnable接口,兩種有很多差異,下面跟著本文一起學(xué)習(xí)吧,希望能給你帶來(lái)幫助
    2021-07-07
  • IDEA中設(shè)置Tab健為4個(gè)空格的方法

    IDEA中設(shè)置Tab健為4個(gè)空格的方法

    這篇文章給大家介紹了代碼縮進(jìn)用空格還是Tab?(IDEA中設(shè)置Tab健為4個(gè)空格)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-03-03
  • Java利用三目運(yùn)算符比較三個(gè)數(shù)字的大小

    Java利用三目運(yùn)算符比較三個(gè)數(shù)字的大小

    今天小編就為大家分享一篇關(guān)于Java利用三目運(yùn)算符比較三個(gè)數(shù)字的大小,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • Mybatis-Spring連接mysql 8.0配置步驟出錯(cuò)的解決方法

    Mybatis-Spring連接mysql 8.0配置步驟出錯(cuò)的解決方法

    這篇文章主要為大家詳細(xì)介紹了Mybatis-Spring連接mysql 8.0配置步驟出錯(cuò)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Java?詳解如何從尾到頭打印鏈表

    Java?詳解如何從尾到頭打印鏈表

    在我們平時(shí)的代碼過(guò)程中,鏈表是我們經(jīng)常遇到的一個(gè)數(shù)據(jù)結(jié)構(gòu),它非常的簡(jiǎn)單,但Java并不能直接將一個(gè)鏈表打印出來(lái),通過(guò)這篇文章我們來(lái)講解一下這個(gè)問(wèn)題
    2022-01-01
  • Java使用觀察者模式實(shí)現(xiàn)氣象局高溫預(yù)警功能示例

    Java使用觀察者模式實(shí)現(xiàn)氣象局高溫預(yù)警功能示例

    這篇文章主要介紹了Java使用觀察者模式實(shí)現(xiàn)氣象局高溫預(yù)警功能,結(jié)合完整實(shí)例形式分析了java觀察者模式實(shí)現(xiàn)氣象局高溫預(yù)警的相關(guān)接口定義、使用、功能操作技巧,并總結(jié)了其設(shè)計(jì)原則與適用場(chǎng)合,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2018-04-04
  • 利用Java編寫一個(gè)Java虛擬機(jī)

    利用Java編寫一個(gè)Java虛擬機(jī)

    這篇文章主要為大家詳細(xì)介紹了如何使用 Java17 編寫的 Java 虛擬機(jī),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解下
    2023-07-07
  • 詳解Java二叉排序樹(shù)

    詳解Java二叉排序樹(shù)

    這篇文章主要介紹了Java二叉排序樹(shù),包括二叉排序樹(shù)的定義、二叉排序樹(shù)的性質(zhì)、二叉排序樹(shù)的插入和查找等,感興趣的小伙伴們可以參考一下
    2015-12-12

最新評(píng)論