【问题标题】:What does Cookie CsrfTokenRepository.withHttpOnlyFalse () do and when to use it?Cookie CsrfTokenRepository.withHttpOnlyFalse () 有什么作用以及何时使用它?
【发布时间】:2020-10-20 04:55:08
【问题描述】:

我现在正在尝试学习 Spring Security,并且我已经看到了许多使用它的不同示例。我知道 CSRF 是什么,Spring Security 默认启用它。 我很想知道的是这种定制。

  .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  .and()
  .authorizeRequests(request -> {
                request
                    .antMatchers("/login").permitAll()
                    .anyRequest()
                    ....more code

.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())这行做了什么样的定制,什么时候合适用。 如果有人能提供一个简单的解释,我将不胜感激。

【问题讨论】:

    标签: java spring spring-boot spring-security csrf


    【解决方案1】:

    CSRF 代表 跨站请求伪造

    它是一种与请求一起发送以防止攻击的令牌。为了使用 Spring Security CSRF 保护,我们首先需要确保对任何修改状态的东西使用正确的 HTTP 方法(PATCHPOSTPUTDELETE——而不是 @ 987654326@).

    使用 Spring CookieCsrfTokenRepository 的 CSRF 保护工作如下:

    • 客户端向服务器(Spring Boot 后端)发出 GET 请求,例如请求主页
    • Spring 发送 GET 请求的响应以及包含安全生成的 XSRF 令牌的 Set-cookie 标头
    • 浏览器使用 XSRF Token 设置 cookie
    • 在发送状态更改请求(例如 POST)时,客户端(可能是有角度的)将 cookie 值复制到 HTTP 请求标头
    • 发送请求时同时带有标头和 cookie(浏览器会自动附加 cookie)
    • Spring比较header和cookie值,如果相同则接受请求,否则返回403给客户端

    withHttpOnlyFalse 方法允许 Angular 读取 XSRF cookie。确保 Angular 发出 XHR 请求时将 withCreddentials 标志设置为 true。


    代码来自CookieCsrfTokenRepository

    @Override
    public CsrfToken generateToken(HttpServletRequest request) {
        return new DefaultCsrfToken(this.headerName, this.parameterName,
                createNewToken());
    }
    
    @Override
    public void saveToken(CsrfToken token, HttpServletRequest request,
            HttpServletResponse response) {
        String tokenValue = token == null ? "" : token.getToken();
        Cookie cookie = new Cookie(this.cookieName, tokenValue);
        cookie.setSecure(request.isSecure());
        if (this.cookiePath != null && !this.cookiePath.isEmpty()) {
                cookie.setPath(this.cookiePath);
        } else {
                cookie.setPath(this.getRequestContext(request));
        }
        if (token == null) {
            cookie.setMaxAge(0);
        }
        else {
            cookie.setMaxAge(-1);
        }
        cookie.setHttpOnly(cookieHttpOnly);
        if (this.cookieDomain != null && !this.cookieDomain.isEmpty()) {
            cookie.setDomain(this.cookieDomain);
        }
    
        response.addCookie(cookie);
    }
    
    @Override
    public CsrfToken loadToken(HttpServletRequest request) {
        Cookie cookie = WebUtils.getCookie(request, this.cookieName);
        if (cookie == null) {
            return null;
        }
        String token = cookie.getValue();
        if (!StringUtils.hasLength(token)) {
            return null;
        }
        return new DefaultCsrfToken(this.headerName, this.parameterName, token);
    }
    
    
    public static CookieCsrfTokenRepository withHttpOnlyFalse() {
        CookieCsrfTokenRepository result = new CookieCsrfTokenRepository();
        result.setCookieHttpOnly(false);
        return result;
    }
    

    您可以探索here的方法

    【讨论】:

    • 'withHttpOnlyFalse' 是只适用于angular还是也适用于react等js框架?
    • 它应该适用于所有客户端框架 angular、react 等
    • 答案对 CSRF 有很多话要说,但对回答 TO 最初的问题几乎没有。
    猜你喜欢
    • 2022-11-11
    • 2011-03-06
    • 1970-01-01
    • 2014-06-19
    • 2018-08-05
    • 2011-12-27
    • 1970-01-01
    • 2016-10-26
    相关资源
    最近更新 更多