欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性

 更新時(shí)間:2022年01月25日 08:49:07   作者:KevinBlandy  
Chrome 51 開始,瀏覽器的 Cookie 新增加了一個(gè)SameSite屬性,用來防止 CSRF 攻擊和用戶追蹤。今天通過本文給大家介紹Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性,感興趣的朋友一起看看吧

Cookie除了keyvalue以外有幾個(gè)屬性。

  • httpOnly 是否允許js讀取cookie
  • secure 是否僅僅在https的鏈接下,才提交cookie
  • domain cookie提交的域
  • path cookie提交的path
  • maxAge cookie存活時(shí)間
  • sameSite 同站策略,枚舉值:Strict Lax None

其他的都很熟悉了,最后一個(gè)是 Chrome 51 開始,瀏覽器的 Cookie 新增加了一個(gè) SameSite 屬性,用來防止 CSRF 攻擊和用戶追蹤。

關(guān)于SameSite的詳細(xì)解釋 可以看 Cookie 的 SameSite 屬性

在Javaweb應(yīng)用中 ,設(shè)置 Cookie一般都是用 javax.servlet.http.Cookie,但是SameSite屬性出來不久,Servlet庫還沒更新,所以沒有設(shè)置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 給客戶端設(shè)置Cookie

本質(zhì)上,Cookie也只是一個(gè)header。我們可以不使用Cookie對(duì)象,而通過自定義Header的方式來給客戶端設(shè)置Cookie。

ResponseCookie 是Spring定義的一個(gè)Cookie構(gòu)建工具類,極其簡(jiǎn)單

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個(gè)小時(shí)候過期
				.sameSite("Lax")	// 大多數(shù)情況也是不發(fā)送第三方 Cookie,但是導(dǎo)航到目標(biāo)網(wǎng)址的 Get 請(qǐng)求除外
				.build()
				;
		
		// 設(shè)置Cookie Header
		response.setHeader(HttpHeaders.SET_COOKIE, cookie.toString());
		
		return "ok";
	}
}

響應(yīng)給客戶端的Cookie

所有屬性都響應(yīng)正確 √

HttpSession Cookie 的SameSite屬性

HttpSession依賴一個(gè)名稱叫做JSESSIONID(默認(rèn)名稱)的Cookie。

對(duì)于JSESSIONID Cookie 的設(shè)置,可以修改如下配置。但是,目前spring也沒實(shí)現(xiàn)SameSite的配置項(xiàng)。

配置類 : 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

通過修改容器的配置,對(duì)Session Cookie設(shè)置SameSite屬性

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();
			// 設(shè)置Cookie的SameSite
			cookieProcessor.setSameSiteCookies(SameSiteCookies.LAX.getValue());
			context.setCookieProcessor(cookieProcessor);
		};
	}
}

Spring Session的SameSite屬性

通過自定義 CookieSerializer 設(shè)置 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");  // 設(shè)置SameSite屬性
		serializer.setUseHttpOnlyCookie(true);
		serializer.setUseSecureCookie(false);
		return serializer;
	}
}

首發(fā):https://springboot.io/t/topic/2602

到此這篇關(guān)于Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性的文章就介紹到這了,更多相關(guān)Springboot設(shè)置Cookie的SameSite屬性內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論