栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

Springboot应用中设置Cookie的SameSite属性

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot应用中设置Cookie的SameSite属性

cookie除了key和value以外有几个属性。

  • httpOnly 是否允许js读取cookie
  • secure 是否仅仅在https的链接下,才提交cookie
  • domain cookie提交的域
  • path cookie提交的path
  • maxAge cookie存活时间
  • sameSite 同站策略,枚举值:Strict Lax None

其他的都很熟悉了,最后一个是 Chrome 51 开始,浏览器的 cookie 新增加了一个 SameSite 属性,用来防止 CSRF 攻击和用户追踪。

关于SameSite的详细解释 可以看 cookie 的 SameSite 属性

在Javaweb应用中 ,设置 cookie一般都是用 javax.servlet.http.cookie,但是SameSite属性出来不久,Servlet库还没更新,所以没有设置SameSite的方法.

javax.servlet.http.cookie 中定义的的属性

可以看到,还没有SameSite的定义

//
// The value of the cookie itself.
//

private String name; // NAME= ... "$Name" style is reserved
private String value; // value of NAME

//
// Attributes encoded in the header's cookie fields.
//

private String comment; // ;Comment=VALUE ... describes cookie's use
// ;Discard ... implied by maxAge < 0
private String domain; // ;Domain=VALUE ... domain that sees cookie
private int maxAge = -1; // ;Max-Age=VALUE ... cookies auto-expire
private String path; // ;Path=VALUE ... URLs that see the cookie
private boolean secure; // ;Secure ... e.g. use SSL
private int version = 0; // ;Version=1 ... means RFC 2109++ style
private boolean isHttponly = false;
通过 Responsecookie 给客户端设置cookie

本质上,cookie也只是一个header。我们可以不使用cookie对象,而通过自定义Header的方式来给客户端设置cookie。

Responsecookie 是Spring定义的一个cookie构建工具类,极其简单

import java.time.Duration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.http.Responsecookie;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class TestController {
	
	@GetMapping("/test")
	public Object test (HttpServletRequest request,
					HttpServletResponse response) throws Exception {
		
		Responsecookie cookie = Responsecookie.from("mycookie", "mycookievalue") // key & value
				.httpOnly(true)		// 禁止js读取
				.secure(false)		// 在http下也传输
				.domain("localhost")// 域名
				.path("/")			// path
				.maxAge(Duration.ofHours(1))	// 1个小时候过期
				.sameSite("Lax")	// 大多数情况也是不发送第三方 cookie,但是导航到目标网址的 Get 请求除外
				.build()
				;
		
		// 设置cookie Header
		response.setHeader(HttpHeaders.SET_cookie, cookie.toString());
		
		return "ok";
	}
}

响应给客户端的cookie

所有属性都响应正确 √

HttpSession cookie 的SameSite属性

HttpSession依赖一个名称叫做JSESSIONID(默认名称)的cookie。

对于JSESSIonID cookie 的设置,可以修改如下配置。但是,目前spring也没实现SameSite的配置项。

配置类 : org.springframework.boot.web.servlet.server.cookie

server.servlet.session.cookie.comment
server.servlet.session.cookie.domain
server.servlet.session.cookie.http-only
server.servlet.session.cookie.max-age
server.servlet.session.cookie.name
server.servlet.session.cookie.path
server.servlet.session.cookie.secure
通过修改容器的配置,对Session cookie设置SameSite属性 Tomcat
import org.apache.tomcat.util.http.Rfc6265cookieProcessor;
import org.apache.tomcat.util.http.SameSitecookies;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TomcatConfiguration {

	@Bean
	public TomcatContextCustomizer sameSitecookiesConfig() {
		return context -> {
			final Rfc6265cookieProcessor cookieProcessor = new Rfc6265cookieProcessor();
			// 设置cookie的SameSite
			cookieProcessor.setSameSitecookies(SameSitecookies.LAX.getValue());
			context.setcookieProcessor(cookieProcessor);
		};
	}
}
Spring Session的SameSite属性

通过自定义 cookieSerializer 设置 SameSite属性

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.web.http.cookieSerializer;

import com.video.common.spring.session.DynamiccookieMaxAgecookieSerializer;

@Configuration
public class SpringSessionConfiguration {
	
	@Bean
	public cookieSerializer cookieSerializer() {
		DynamiccookieMaxAgecookieSerializer serializer = new DynamiccookieMaxAgecookieSerializer();
		serializer.setcookieName("JSESSIONID");
		serializer.setDomainName("localhost");
		serializer.setcookiePath("/");
		serializer.setcookieMaxAge(3600);
		serializer.setSameSite("Lax");  // 设置SameSite属性
		serializer.setUseHttpOnlycookie(true);
		serializer.setUseSecurecookie(false);
		return serializer;
	}
}

首发:https://springboot.io/t/topic/2602

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号