最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2022年05月09日 10:08:25   作者:R先生專欄  
本文主要介紹了詳解Spring Security中獲取當(dāng)前登錄用戶的詳細(xì)信息的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(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)用場景,可以對 SecurityContextHolder 里面的 AuthenticationToken 進行類型轉(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ù)獲?。?/li>
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 這個接口在springboot2.1.0中已不存在了

通過 Interface 獲取其實和第一種在 Bean 中獲取用戶信息是一樣的,都是訪問 SecurityContextHolder 獲取的,只是進行了封裝。

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實現(xiàn)時間與字符串互相轉(zhuǎn)換詳解

    Java實現(xiàn)時間與字符串互相轉(zhuǎn)換詳解

    這篇文章主要為大家詳細(xì)介紹了Java中實現(xiàn)時間與字符串互相轉(zhuǎn)換的相關(guān)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-04-04
  • 劍指Offer之Java算法習(xí)題精講數(shù)組與字符串

    劍指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)造方法

    這篇文章主要給大家分享的是java線程的創(chuàng)建以及構(gòu)造方法,想了解具體方式的小伙伴可以參考下面文章內(nèi)容,希望對你有所幫助
    2022-04-04
  • 利用Java實現(xiàn)網(wǎng)站聚合工具

    利用Java實現(xiàn)網(wǎng)站聚合工具

    互聯(lián)網(wǎng)上有數(shù)以萬億計的網(wǎng)站,每個網(wǎng)站大都具有一定的功能。搜索引擎雖然對互聯(lián)網(wǎng)上的部分網(wǎng)站建立了索引,但是其作為一個大而全的搜索系統(tǒng),無法很好的定位到一些特殊的需求。因此本文將介紹一個用java實現(xiàn)的網(wǎng)站數(shù)據(jù)聚合工具,需要的可以參考一下
    2022-01-01
  • 詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用

    詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用

    這篇文章主要介紹了詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • SpringBoot自動配置原理及案例源碼解析

    SpringBoot自動配置原理及案例源碼解析

    這篇文章主要為大家介紹了SpringBoot自動配置原理及自動配置案例源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • mybatis的dtd約束文件及配置文件xml自動提示操作

    mybatis的dtd約束文件及配置文件xml自動提示操作

    這篇文章主要介紹了mybatis的dtd約束文件及配置文件xml自動提示操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • 淺談Spring事務(wù)傳播行為實戰(zhàn)

    淺談Spring事務(wù)傳播行為實戰(zhàn)

    這篇文章主要介紹了淺談Spring事務(wù)傳播行為實戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java中的Native關(guān)鍵字講解

    Java中的Native關(guān)鍵字講解

    本文介紹了Java中的Native關(guān)鍵字,native關(guān)鍵字是架起本機語言和JAVA之間鴻溝的橋梁。如果我們的軟件與硬件的交互在使用預(yù)先存在的代碼時更有效,那么這可以作為一個關(guān)鍵環(huán)節(jié)。與從頭開始設(shè)計新的應(yīng)用程序代碼相比,只要可以避免,它就可以使實現(xiàn)工作更少,下面來了解集體內(nèi)容
    2021-12-12
  • springboot+dubbo啟動項目時報錯 zookeeper not connected的問題及解決方案

    springboot+dubbo啟動項目時報錯 zookeeper not connect

    這篇文章主要介紹了springboot+dubbo項目啟動項目時報錯 zookeeper not connected的問題,本文給大家定位問題及解決方案,結(jié)合實例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下
    2023-06-06

最新評論

民县| 翼城县| 宁城县| 金昌市| 万载县| 叶城县| 凤台县| 桂东县| 当涂县| 巴彦县| 上犹县| 嵊州市| 翁源县| 天全县| 马山县| 廉江市| 白银市| 离岛区| 滕州市| 达州市| 拜城县| 禹城市| 伊宁县| 蒙阴县| 林芝县| 廊坊市| 南安市| 司法| 盱眙县| 汕头市| 蒲城县| 利津县| 平和县| 缙云县| 安化县| 上饶县| 襄樊市| 云和县| 关岭| 伊通| 洱源县|