亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

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

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

倚天杖 2022-12-15 15:59:59
我正在添加一個 WebFilter 以在 SecurityWebFilterChain 內部執行 JWT 身份驗證。我們在 JWT 中編碼了許多 API 端點所需的非身份驗證相關信息,因此我需要能夠從 JWT 中提取信息并在我的 API 處理程序方法(例如,LoginController.爪哇)。實現這一目標的最佳模式是什么?這是我的 SecurityWebFilterChain 顯示 WebFilter 身份驗證:    @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貢獻1831條經驗 獲得超4個贊

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

對于用戶相關的數據,存儲在其內部Principal。對于非用戶相關的數據,聽起來Details是一個存儲的好地方。

許多內置函數AuthenticationProvider會創建一個UserDetails并存儲到PrincipalUserDetails這意味著如果您正在使用那些內置的,您可以考慮只創建一個 customsied AuthenticationProvider

因此,根據您實現身份驗證邏輯的方式,您需要自定義相關AuthenticationProviderFilter。目的是訪問HttpServletRequest,從 HTTP 標頭獲取 JWT,解析 JWT,設置和配置此自定義Authentication對象并將其設置為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);

    }


查看完整回答
反對 回復 2022-12-15
  • 1 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號