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

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

基于自定義標頭的 Spring Security antMatcher 規則

基于自定義標頭的 Spring Security antMatcher 規則

千萬里不及你 2023-03-09 14:08:35
在 Spring 框架中,我目前正在嘗試使用自定義標頭而不是 url 來區分某些端點。目前,我似乎看不出如何允許帶有自定義標頭的特定 URL,但在 Spring Security 中拒絕另一個 URL。我的安全配置當前有一個 antMatcher,如下所示:.antMatchers( HttpMethod.POST, "/api/website-user" ).permitAll()但是,我還有一些其他的“POST”方法也受到保護——對于這個特定的端點,我只希望通過發送的標頭識別和排除它。您如何告訴 Spring 安全該 URL 應該通過未經身份驗證的方式傳遞 @PostMapping( headers = "X-Operation-Name=forgot-password" )    public WebsiteUser forgotPassword( @Valid PasswordResetRequestModel passwordReset )但是這個例如沒有(并且依賴于經過身份驗證的用戶)?@PostMapping( headers = "X-Operation-Name=resend-verification" )    public WebsiteUser resendVerification( Principal principal )
查看完整描述

1 回答

?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

您始終可以實現一個RequestMatcher來定義您自定義的 HTTP 請求匹配邏輯。如果匹配器為 HTTP 請求返回真值,它將允許該請求訪問:


public MyRequestMatcher implements RequestMatcher {


    boolean matches(HttpServletRequest request){

         //Define the matching logic here....

         if(request.getHeader("xxx") != null &&

            request.getHeader("xxx").equals("yyyy"){

             return true;

         }

         //blablablab

    }

并配置使用這個匹配器:


 httpSecurity.authorizeRequests().requestMatchers(new MyRequestMatcher()).permitAll();

Spring Security 也提供了一些常用的RequestMatcher比如RequestHeaderRequestMatcher和AndRequestMatcher,應該適合你的需求:


//This matches if the request has X-Operation-Name header and its value is forgot-password

RequestHeaderRequestMatcher headerMatcher = new RequestHeaderRequestMatcher("X-Operation-Name","forgot-password" );


// This matches if the request is POST to the /api/website-user

AntPathRequestMatcher antRequestMatcher = new AntPathRequestMatcher("/api/website-user", HttpMethod.POST)


// This matches if both of the above matches matches 

AndRequestMatcher andMatcher = new AndRequestMatcher(headerMatcher,antRequestMatcher );


httpSecurity.authorizeRequests().requestMatchers(andMatcher).permitAll();


查看完整回答
反對 回復 2023-03-09
  • 1 回答
  • 0 關注
  • 253 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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