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

詳解Spring Security中獲取當前登錄用戶的詳細信息的幾種方法

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

在Bean中獲取用戶信息

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

Spring Security 框架提供了多種 AuthenticationToken 的派生類,根據自己的應用場景,可以對 SecurityContextHolder 里面的 AuthenticationToken 進行類型轉換,如下:

UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
// details里面存放了當前登錄用戶的詳細信息
User userDetails = (User) authenticationToken.getDetails();

PS. AuthenticationToken的類型轉換同樣適用于下面提到的Principal類。

在Controller中獲取用戶信息

  • 通過Principal參數獲?。?/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參數獲?。?/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 的標簽特性,首先要在 JSP 頁面引入 Security 的 tag:

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

通過以下方式可以獲取到當前登錄用戶:

<security:authorize access="isAuthenticated()">
    authenticated as <security:authentication property="principal.username" /> 
</security:authorize>

到此這篇關于詳解Spring Security中獲取當前登錄用戶的詳細信息的幾種方法的文章就介紹到這了,更多相關Spring Securit獲取登錄用戶信息內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java實現時間與字符串互相轉換詳解

    Java實現時間與字符串互相轉換詳解

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

    劍指Offer之Java算法習題精講數組與字符串

    跟著思路走,之后從簡單題入手,反復去看,做過之后可能會忘記,之后再做一次,記不住就反復做,反復尋求思路和規(guī)律,慢慢積累就會發(fā)現質的變化
    2022-03-03
  • 深入探究Java線程的創(chuàng)建與構造方法

    深入探究Java線程的創(chuàng)建與構造方法

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

    利用Java實現網站聚合工具

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

    詳解XML,Object,Json轉換與Xstream的使用

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

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

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

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

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

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

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

    Java中的Native關鍵字講解

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

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

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

最新評論

婺源县| 万年县| 来安县| 尼勒克县| 忻州市| 苗栗县| 广元市| 盘锦市| 彭州市| 北川| 孟村| 宜春市| 周口市| 灵璧县| 巴林右旗| 墨竹工卡县| 肥西县| 和平区| 界首市| 延安市| 龙口市| 驻马店市| 宁都县| 拉萨市| 南岸区| 株洲市| 许昌县| 普兰店市| 寿阳县| 连州市| 望江县| 鸡东县| 满城县| 华宁县| 平南县| 遂川县| 乌鲁木齐县| 江阴市| 贵阳市| 平乐县| 肇庆市|