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
本質上,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;
}
}
