【问题标题】:Spring security 3.2 : How to bypass all the ajax requests in CSRFFilterSpring security 3.2:如何绕过 CSRFFilter 中的所有 ajax 请求
【发布时间】:2015-08-07 15:30:36
【问题描述】:

鉴于在 spring security 3.2 中,为了防止 CSRF 攻击,我们必须包含 CSRF 令牌。它也适用于 ajax 请求。对于 ajax,我们必须包含标头标记。但这似乎有很多返工。覆盖 CSRFFilter 是一种方法。有没有更好的方法可以绕过 ajax 请求的令牌检查

【问题讨论】:

    标签: java spring security csrf


    【解决方案1】:

    此代码将帮助您实现这一目标,并且由于您尚未粘贴配置,因此很难提供帮助。

     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;
            }
        });
    

    如果这是您想要的,请告诉我。

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 1970-01-01
      • 2014-09-30
      • 2021-04-14
      • 2015-05-13
      • 1970-01-01
      • 2017-09-05
      • 2018-03-14
      相关资源
      最近更新 更多