詳解Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性
Cookie除了key和value以外有幾個(gè)屬性。
httpOnly是否允許js讀取cookiesecure是否僅僅在https的鏈接下,才提交cookiedomaincookie提交的域pathcookie提交的pathmaxAgecookie存活時(shí)間sameSite同站策略,枚舉值:StrictLaxNone
其他的都很熟悉了,最后一個(gè)是 Chrome 51 開始,瀏覽器的 Cookie 新增加了一個(gè) SameSite 屬性,用來(lái)防止 CSRF 攻擊和用戶追蹤。
關(guān)于SameSite的詳細(xì)解釋 可以看 Cookie 的 SameSite 屬性
在Javaweb應(yīng)用中 ,設(shè)置 Cookie一般都是用 javax.servlet.http.Cookie,但是SameSite屬性出來(lái)不久,Servlet庫(kù)還沒(méi)更新,所以沒(méi)有設(shè)置SameSite的方法.
javax.servlet.http.Cookie 中定義的的屬性
可以看到,還沒(méi)有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;
通過(guò) ResponseCookie 給客戶端設(shè)置Cookie
本質(zhì)上,Cookie也只是一個(gè)header。我們可以不使用Cookie對(duì)象,而通過(guò)自定義Header的方式來(lái)給客戶端設(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í)候過(guò)期
.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也沒(méi)實(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
通過(guò)修改容器的配置,對(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屬性
通過(guò)自定義 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)文章
SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Zuul基礎(chǔ)
這篇文章主要介紹了SpringCloud?Zuul微服務(wù)網(wǎng)關(guān),負(fù)載均衡,熔斷和限流,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
java多線程編程之使用runnable接口創(chuàng)建線程
實(shí)現(xiàn)Runnable接口的類必須使用Thread類的實(shí)例才能創(chuàng)建線程,通過(guò)Runnable接口創(chuàng)建線程分為以下兩步2014-01-01
DolphinScheduler容錯(cuò)Master源碼分析
這篇文章主要為大家介紹了DolphinScheduler容錯(cuò)Master源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
在idea環(huán)境下構(gòu)建springCloud項(xiàng)目
本篇文章主要介紹了在idea環(huán)境下構(gòu)建springCloud項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11

