簡體   English   中英

Spring Security 3.2:如何繞過CSRFFilter中的所有ajax請求

[英]Spring security 3.2 : How to bypass all the ajax requests in CSRFFilter

鑒於在Spring Security 3.2中,為了防止CSRF攻擊,我們必須包含CSRF令牌。 它也適用於ajax請求。 對於ajax,我們必須包含標頭令牌。 但這似乎有很多返工。 覆蓋CSRFFilter是一種方法。 有什么更好的方法可以繞過針對ajax請求的令牌檢查

此代碼將幫助您實現這一目標,並且由於您尚未粘貼配置,因此很難提供幫助。

 http.csrf().requireCsrfProtectionMatcher(new RequestMatcher() {
        private Pattern allowedMethods = Pattern.compile("^(GET|HEAD|TRACE|OPTIONS)$");
        private RegexRequestMatcher apiMatcher = new RegexRequestMatcher("/v[0-9]*/.*", null);

        @Override
        public boolean matches(HttpServletRequest request) {
            // No CSRF due to allowedMethod
            if(allowedMethods.matcher(request.getMethod()).matches())
                return false;

            // No CSRF due to api call
            if(apiMatcher.matches(request))
                return false;

            // CSRF for everything else that is not an API call or an allowedMethod
            return true;
        }
    });

讓我知道這是否是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM