詳解Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性
Cookie
除了key
和value
以外有幾個(gè)屬性。
httpOnly
是否允許js讀取cookiesecure
是否僅僅在https的鏈接下,才提交cookiedomain
cookie提交的域path
cookie提交的pathmaxAge
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)文章
深入理解Java中的HashMap的實(shí)現(xiàn)機(jī)制
這篇文章主要介紹了深入理解Java中的HashMap的實(shí)現(xiàn)機(jī)制,同時(shí)也有助于理解Java中對(duì)于哈希函數(shù)的相關(guān)處理方式,需要的朋友可以參考下2015-07-07Java將CSV的數(shù)據(jù)發(fā)送到kafka的示例
這篇文章主要介紹了Java將CSV的數(shù)據(jù)發(fā)送到kafka得示例,幫助大家更好得理解和使用Java,感興趣的朋友可以了解下2020-11-11java實(shí)現(xiàn)酷狗音樂臨時(shí)緩存文件轉(zhuǎn)換為MP3文件的方法
這篇文章主要介紹了java實(shí)現(xiàn)酷狗音樂臨時(shí)緩存文件轉(zhuǎn)換為MP3文件的方法,涉及java針對(duì)文件操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08使用SpringBoot的CommandLineRunner遇到的坑及解決
這篇文章主要介紹了使用SpringBoot的CommandLineRunner遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02AsyncConfigurerSupport自定義異步線程池處理異常
這篇文章主要為大家介紹了AsyncConfigurerSupport自定義異步線程池處理異常詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Java中靜態(tài)類型檢查是如何進(jìn)行的實(shí)例思路詳解
這篇文章主要介紹了Java中靜態(tài)類型檢查是如何進(jìn)行的實(shí)例思路詳解的相關(guān)資料,需要的朋友可以參考下2016-05-05