Skip to content

Commit 95275bd

Browse files
committed
Add Cookie's SameSite directive property
See spring-projects#15047
1 parent 1ea8c7b commit 95275bd

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public DefaultCookieSerializer cookieSerializer(ServerProperties serverPropertie
103103
map.from(cookie::getHttpOnly).to(cookieSerializer::setUseHttpOnlyCookie);
104104
map.from(cookie::getSecure).to(cookieSerializer::setUseSecureCookie);
105105
map.from(cookie::getMaxAge).to((maxAge) -> cookieSerializer.setCookieMaxAge((int) maxAge.getSeconds()));
106+
map.from(cookie::getSameSite).to((sameSite) -> cookieSerializer.setSameSite(sameSite.toString()));
106107
if (ClassUtils.isPresent(REMEMBER_ME_SERVICES_CLASS, getClass().getClassLoader())) {
107108
new RememberMeServicesCookieSerializerCustomizer().apply(cookieSerializer);
108109
}

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@
112112
"name": "server.servlet.session.cookie.secure",
113113
"description": "Whether to always mark the session cookie as secure."
114114
},
115+
{
116+
"name": "server.servlet.session.cookie.same-site",
117+
"description": "The \"SameSite\" directive for the cookie."
118+
},
115119
{
116120
"name": "server.servlet.session.persistent",
117121
"description": "Whether to persist session data between restarts.",

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizerTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public void customizeSessionProperties() throws Exception {
101101
map.put("server.servlet.session.cookie.http-only", "true");
102102
map.put("server.servlet.session.cookie.secure", "true");
103103
map.put("server.servlet.session.cookie.max-age", "60");
104+
map.put("server.servlet.session.cookie.same-site", "strict");
104105
bindProperties(map);
105106
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);
106107
this.customizer.customize(factory);
@@ -114,6 +115,7 @@ public void customizeSessionProperties() throws Exception {
114115
assertThat(cookie.getComment()).isEqualTo("testcomment");
115116
assertThat(cookie.getHttpOnly()).isTrue();
116117
assertThat(cookie.getMaxAge()).isEqualTo(Duration.ofSeconds(60));
118+
assertThat(cookie.getSameSite()).isEqualTo(Cookie.SameSite.STRICT);
117119

118120
}
119121

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/Session.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public static class Cookie {
120120
@DurationUnit(ChronoUnit.SECONDS)
121121
private Duration maxAge;
122122

123+
private SameSite sameSite;
124+
123125
/**
124126
* Return the session cookie name.
125127
* @return the session cookie name
@@ -205,6 +207,56 @@ public void setMaxAge(Duration maxAge) {
205207
this.maxAge = maxAge;
206208
}
207209

210+
/**
211+
* Return the SameSite directive for the cookie.
212+
* @return the SameSite directive for the cookie
213+
*/
214+
public SameSite getSameSite() {
215+
return this.sameSite;
216+
}
217+
218+
public void setSameSite(final SameSite sameSite) {
219+
this.sameSite = sameSite;
220+
}
221+
222+
/**
223+
* Available SameSite directives for the cookie.
224+
*/
225+
public enum SameSite {
226+
227+
/**
228+
* The cookie will only be sent if the site for the cookie matches the current
229+
* site URL. The cookie will not be sent along with requests initiated by
230+
* third party websites.
231+
*/
232+
STRICT("Strict"),
233+
234+
/**
235+
* The cookie will only be sent if the site for the cookie matches the current
236+
* site URL. The cookie will be sent along with the GET request initiated by
237+
* third party website.
238+
*/
239+
LAX("Lax"),
240+
241+
/**
242+
* The cookie will be sent cross-origin. This directive requires the Secure
243+
* attribute.
244+
*/
245+
NONE("None");
246+
247+
private String value;
248+
249+
SameSite(final String value) {
250+
this.value = value;
251+
}
252+
253+
@Override
254+
public String toString() {
255+
return this.value;
256+
}
257+
258+
}
259+
208260
}
209261

210262
/**

0 commit comments

Comments
 (0)