詳解Springboot應用中設置Cookie的SameSite屬性
Cookie除了key和value以外有幾個屬性。
httpOnly是否允許js讀取cookiesecure是否僅僅在https的鏈接下,才提交cookiedomaincookie提交的域pathcookie提交的pathmaxAgecookie存活時間sameSite同站策略,枚舉值:StrictLaxNone
其他的都很熟悉了,最后一個是 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
本質(zhì)上,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") // 大多數(shù)情況也是不發(fā)送第三方 Cookie,但是導航到目標網(wǎng)址的 Get 請求除外
.build()
;
// 設置Cookie Header
response.setHeader(HttpHeaders.SET_COOKIE, cookie.toString());
return "ok";
}
}響應給客戶端的Cookie

所有屬性都響應正確 √
HttpSession Cookie 的SameSite屬性
HttpSession依賴一個名稱叫做JSESSIONID(默認名稱)的Cookie。
對于JSESSIONID Cookie 的設置,可以修改如下配置。但是,目前spring也沒實現(xiàn)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屬性
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;
}
}首發(fā):https://springboot.io/t/topic/2602
到此這篇關于Springboot應用中設置Cookie的SameSite屬性的文章就介紹到這了,更多相關Springboot設置Cookie的SameSite屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java將CSV的數(shù)據(jù)發(fā)送到kafka的示例
這篇文章主要介紹了Java將CSV的數(shù)據(jù)發(fā)送到kafka得示例,幫助大家更好得理解和使用Java,感興趣的朋友可以了解下2020-11-11
java實現(xiàn)酷狗音樂臨時緩存文件轉(zhuǎn)換為MP3文件的方法
這篇文章主要介紹了java實現(xiàn)酷狗音樂臨時緩存文件轉(zhuǎn)換為MP3文件的方法,涉及java針對文件操作的相關技巧,需要的朋友可以參考下2016-08-08
使用SpringBoot的CommandLineRunner遇到的坑及解決
這篇文章主要介紹了使用SpringBoot的CommandLineRunner遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
AsyncConfigurerSupport自定義異步線程池處理異常
這篇文章主要為大家介紹了AsyncConfigurerSupport自定義異步線程池處理異常詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06

