scpting 安全 requireCsrfProtectionMatcher 与 csrfTokenRepository

ali*_*ele 1 cas csrf spring-security

我正在尝试禁用特定 url 的 Csrf。这是我到目前为止所做的:

public HttpSessionCsrfTokenRepository csrfTokenRepository() {
    final HttpSessionCsrfTokenRepository tokenRepository = new HttpSessionCsrfTokenRepository();
    tokenRepository.setHeaderName("X-XSRF-TOKEN");
    return tokenRepository;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    RequestMatcher matcher = request -> !("//j_spring_cas_security_check".equals(request.getRequestURI()));
    http.csrf()
            .requireCsrfProtectionMatcher(matcher)
            .csrfTokenRepository(csrfTokenRepository());
Run Code Online (Sandbox Code Playgroud)

如果我requireCsrfProtectionMatcher在所有匹配器中注释掉或简单地返回 false 将不会有错误,但是使用此配置它会给我:

HTTP Status 403 - Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-XSRF-TOKEN'.
Run Code Online (Sandbox Code Playgroud)

我需要禁用 csrf,j_spring_cas_security_check以便单点注销工作并tokenRepository使用 angularjs。有什么我想念的吗?

Pav*_*uri 5

如果您不向 传递任何内容requireCsrfProtectionMatcher,则默认行为是绕过所有 GET 请求。明确提供新的那一刻,行为就会丢失,您还将检查 GET 请求。将代码更改为以下以允许 GET 请求。

public class CsrfRequestMatcher implements RequestMatcher {

    // Always allow the HTTP GET method
    private Pattern allowedMethods = Pattern.compile("^GET$");

    @Override
    public boolean matches(HttpServletRequest request) {

        if (allowedMethods.matcher(request.getMethod()).matches()) {
            return false;
        }

        // Your logic goes here


        return true;
    }

}
Run Code Online (Sandbox Code Playgroud)