第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Webflux 訪問 Spring API 處理程序方法中的 JWT 聲明?

如何使用 Webflux 訪問 Spring API 處理程序方法中的 JWT 聲明?

倚天杖 2022-12-15 15:59:59
我正在添加一個 WebFilter 以在 SecurityWebFilterChain 內(nèi)部執(zhí)行 JWT 身份驗(yàn)證。我們在 JWT 中編碼了許多 API 端點(diǎn)所需的非身份驗(yàn)證相關(guān)信息,因此我需要能夠從 JWT 中提取信息并在我的 API 處理程序方法(例如,LoginController.爪哇)。實(shí)現(xiàn)這一目標(biāo)的最佳模式是什么?這是我的 SecurityWebFilterChain 顯示 WebFilter 身份驗(yàn)證:    @Bean    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {        return http                .authorizeExchange()                .pathMatchers("/login", "/")                .authenticated()                .and()                .addFilterAt(basicAuthenticationFilter(), SecurityWebFiltersOrder.HTTP_BASIC)                .authorizeExchange()                .pathMatchers("/adm")                .authenticated()                .and()                .addFilterAt(basicAuthenticationFilter(), SecurityWebFiltersOrder.HTTP_BASIC)                .authorizeExchange()                .pathMatchers("/api/**")                .access(authorizationManager)                .and()                .addFilterAt(bearerAuthenticationFilter(), SecurityWebFiltersOrder.AUTHENTICATION)                .build();    }這是我想在 LoginController.java 中訪問聲明的地方:@RestController()@RequestMapping(value = "/login")public class LoginController {    private final UserMongoRepository repository;    @Autowired    public LoginController(UserMongoRepository repository) {        this.repository = repository;    }    @PostMapping("")    public Mono<User> login(@RequestBody User post,                            @RequestParam String user_id,                            @RequestParam String username) {        //Need to access information from JWT claims here        return this.repository.findById(user_id);    }}
查看完整描述

1 回答

?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個贊

我會創(chuàng)建一個自定義Authentication對象并在其中存儲所需的信息。

對于用戶相關(guān)的數(shù)據(jù),存儲在其內(nèi)部Principal。對于非用戶相關(guān)的數(shù)據(jù),聽起來Details是一個存儲的好地方。

許多內(nèi)置函數(shù)AuthenticationProvider會創(chuàng)建一個UserDetails并存儲到PrincipalUserDetails這意味著如果您正在使用那些內(nèi)置的,您可以考慮只創(chuàng)建一個 customsied AuthenticationProvider。

因此,根據(jù)您實(shí)現(xiàn)身份驗(yàn)證邏輯的方式,您需要自定義相關(guān)AuthenticationProviderFilter。目的是訪問HttpServletRequest,從 HTTP 標(biāo)頭獲取 JWT,解析 JWT,設(shè)置和配置此自定義Authentication對象并將其設(shè)置為SecurityContext

SecurityContextHolder.getContext().setAuthentication(authenication);

Authentication要在 Controller 中訪問此對象,您可以使用:

Authentication auth = SecurityContextHolder.getContext().getAuthentication();

CurrentUser user = (CurrentUser) auth.getPrincipal();

CurrentRequestDetail detail= (CurrentRequestDetail) auth.getDetails();

/** CurrentUser and CurrentRequestDetail is the customised Principal and Details**/

如果您只需要訪問 [ Principal],您可以使用@AuthenticationPrincipal:


    @PostMapping("")

    public Mono<User> login(@RequestBody User post,

                            @RequestParam String user_id,

                            @RequestParam String username,

                            @AuthenticationPrincipal CurrentUser currentUser) {


        //Need to access information from JWT claims here


        return this.repository.findById(user_id);

    }


查看完整回答
反對 回復(fù) 2022-12-15
  • 1 回答
  • 0 關(guān)注
  • 102 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號