Springboot2.6以下版本对cookie的samesite设置的通用方法

时间:2025-04-20 07:36:53

      通过安全扫描工具对spring技术栈开发的应用进行漏洞检查时,通常会扫描出关于cookie相关的漏洞,其中一个是: Cookie without SameSite attribute,对于其描述通常如下: 

When cookies lack the SameSite attribute, Web browsers may apply different and

sometimes unexpected defaults. It is therefore recommended to add a SameSite
attribute with an appropriate value of either "Strict", "Lax", or "None".
       由于 Java Servlet /4.0 规范不支持 SameSite cookie 属性,所以要解决此类问题没有一个统一的方式。 如何解决此问题呢? 大家在网上找到的解决方案有三类:
1.  Nginx或APACHE SERVER这些代理服务器上设置:
    以nginx为例:proxy_cookie_path / "/; httponly; secure; SameSite=Lax";
2.  在添加完cookie时,加下如下代码:
   
 Cookie cookie = new Cookie("test_001", ("123457890", "UTF-8"));
      ("/");
      (-1);
      (true);
Collection<String> headers = (HttpHeaders.SET_COOKIE);
    boolean firstHeader = true;
    for (String header : headers) { // there can be multiple Set-Cookie attributes
      if (firstHeader) {
        (HttpHeaders.SET_COOKIE, ("%s; SameSite=%s", header, ));
        firstHeader = false;
        continue;
      }
      (HttpHeaders.SET_COOKIE, ("%s; SameSite=%s", header, ));
    }

3. 对于springboot2.6及以上版本,设置:-site=LAX

但对于springboot2.6以下版本来说,利用spring security本身的机制解决此问题没有统一的方式。考虑到spring在写http header时利用了,所以可以通过自定义一个SameSiteCookieHeaderWriter来实现。具体实现如下:

  a) 实现一个自定义HeaderWriter: SameSiteCookieHeaderWriter

    

import ;

import ;
import ;

import ;
import ;

/**
 * 安全问题: Cookie without SameSite attribute
 * 
 */
public class SameSiteCookieHeaderWriter implements HeaderWriter {

  private String cookieSameSite;

  public SameSiteCookieHeaderWriter(String cookieSameSite) {
     = cookieSameSite;
    if (("NONE") || ("LAX")
      || ("STRICT")) {
       = "LAX";
    } else {
       = ();
    }

  }

  /*  
   *(non-Javadoc)  
   * @see #writeHeaders(, )  
   */
  @Override
  public void writeHeaders(HttpServletRequest request, HttpServletResponse response) {

    Collection<String> headers = (HttpHeaders.SET_COOKIE);
    boolean firstHeader = true;
    for (String header : headers) { // there can be multiple Set-Cookie attributes
      if (firstHeader) {
        (HttpHeaders.SET_COOKIE, ("%s; SameSite=%s", header, ));
        firstHeader = false;
        continue;
      }
      (HttpHeaders.SET_COOKIE, ("%s; SameSite=%s", header, ));
    }

  }

}

b) 在WebSecurityConfigurerAdapter的configure(HttpSecurity http) 中加上此SameSiteHeaderWriter

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http. //
      cors().and(). //
      csrf().disable().//
      sessionManagement().sessionCreationPolicy();

    // 设置 X-Frame-Options
    // disable 是没有 X-Frame-Options
    // 没有 disable 就是 DENY
    // sameOrigin 就是 SAMEORIGIN
    ().frameOptions().sameOrigin(); // disable submit dc99635
     // ().frameOptions().disable(); // 按配置来
     

    ().xssProtection().xssProtectionEnabled(true).block(true);
    
    ().addHeaderWriter(new SameSiteCookieHeaderWriter("LAX"));
 
  }
  
}

这种方法相对来说比较优雅,其原理是利用springsecurity的HeaderWriter机制将原来的Set-Cookie重写了。