Skip to content

Commit ce5f48e

Browse files
committed
Support configure "domainNamePattern" and "sameSite" for spring session cookie
1 parent 253f98c commit ce5f48e

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
* @author Eddú Meléndez
7272
* @author Stephane Nicoll
7373
* @author Vedran Pavic
74+
* @author Yanming Zhou
7475
* @since 1.4.0
7576
*/
7677
@Configuration(proxyBeanMethods = false)
@@ -97,10 +98,12 @@ DefaultCookieSerializer cookieSerializer(ServerProperties serverProperties,
9798
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
9899
map.from(cookie::getName).to(cookieSerializer::setCookieName);
99100
map.from(cookie::getDomain).to(cookieSerializer::setDomainName);
101+
map.from(cookie::getDomainPattern).to(cookieSerializer::setDomainNamePattern);
100102
map.from(cookie::getPath).to(cookieSerializer::setCookiePath);
101103
map.from(cookie::getHttpOnly).to(cookieSerializer::setUseHttpOnlyCookie);
102104
map.from(cookie::getSecure).to(cookieSerializer::setUseSecureCookie);
103105
map.from(cookie::getMaxAge).to((maxAge) -> cookieSerializer.setCookieMaxAge((int) maxAge.getSeconds()));
106+
map.from(cookie::getSameSite).to(cookieSerializer::setSameSite);
104107
cookieSerializerCustomizers.orderedStream().forEach((customizer) -> customizer.customize(cookieSerializer));
105108
return cookieSerializer;
106109
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* @author Eddú Meléndez
5555
* @author Stephane Nicoll
5656
* @author Vedran Pavic
57+
* @author Yanming Zhou
5758
*/
5859
class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTests {
5960

@@ -143,7 +144,7 @@ void sessionCookieConfigurationIsAppliedToAutoConfiguredCookieSerializer() {
143144
.withPropertyValues("server.servlet.session.cookie.name=sid",
144145
"server.servlet.session.cookie.domain=spring", "server.servlet.session.cookie.path=/test",
145146
"server.servlet.session.cookie.httpOnly=false", "server.servlet.session.cookie.secure=false",
146-
"server.servlet.session.cookie.maxAge=10s")
147+
"server.servlet.session.cookie.maxAge=10s", "server.servlet.session.cookie.sameSite=Strict")
147148
.run((context) -> {
148149
DefaultCookieSerializer cookieSerializer = context.getBean(DefaultCookieSerializer.class);
149150
assertThat(cookieSerializer).hasFieldOrPropertyWithValue("cookieName", "sid");
@@ -152,6 +153,17 @@ void sessionCookieConfigurationIsAppliedToAutoConfiguredCookieSerializer() {
152153
assertThat(cookieSerializer).hasFieldOrPropertyWithValue("useHttpOnlyCookie", false);
153154
assertThat(cookieSerializer).hasFieldOrPropertyWithValue("useSecureCookie", false);
154155
assertThat(cookieSerializer).hasFieldOrPropertyWithValue("cookieMaxAge", 10);
156+
assertThat(cookieSerializer).hasFieldOrPropertyWithValue("sameSite", "Strict");
157+
});
158+
}
159+
160+
@Test
161+
void sessionCookieDomainPatternConfigurationIsAppliedToAutoConfiguredCookieSerializer() {
162+
this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class)
163+
.withPropertyValues("server.servlet.session.cookie.domainPattern=^.+?\\\\.(\\\\w+\\\\.[a-z]+)$")
164+
.run((context) -> {
165+
DefaultCookieSerializer cookieSerializer = context.getBean(DefaultCookieSerializer.class);
166+
assertThat(cookieSerializer).extracting("domainNamePattern").isNotNull();
155167
});
156168
}
157169

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* Session properties.
2828
*
2929
* @author Andy Wilkinson
30+
* @author Yanming Zhou
3031
* @since 2.0.0
3132
*/
3233
public class Session {
@@ -109,6 +110,8 @@ public static class Cookie {
109110

110111
private String domain;
111112

113+
private String domainPattern;
114+
112115
private String path;
113116

114117
private String comment;
@@ -120,6 +123,8 @@ public static class Cookie {
120123
@DurationUnit(ChronoUnit.SECONDS)
121124
private Duration maxAge;
122125

126+
private String sameSite;
127+
123128
/**
124129
* Return the session cookie name.
125130
* @return the session cookie name
@@ -140,10 +145,28 @@ public String getDomain() {
140145
return this.domain;
141146
}
142147

148+
/**
149+
* Cannot set both domain and domainPattern
150+
*/
143151
public void setDomain(String domain) {
144152
this.domain = domain;
145153
}
146154

155+
/**
156+
* Return the case insensitive pattern to extract the domain name.
157+
* @return the pattern to extract the domain
158+
*/
159+
public String getDomainPattern() {
160+
return this.domainPattern;
161+
}
162+
163+
/**
164+
* Cannot set both domain and domainPattern
165+
*/
166+
public void setDomainPattern(String domainPattern) {
167+
this.domainPattern = domainPattern;
168+
}
169+
147170
/**
148171
* Return the path of the session cookie.
149172
* @return the session cookie path
@@ -205,6 +228,17 @@ public void setMaxAge(Duration maxAge) {
205228
this.maxAge = maxAge;
206229
}
207230

231+
/**
232+
* Return the value for the {@code SameSite} cookie directive.
233+
*/
234+
public String getSameSite() {
235+
return sameSite;
236+
}
237+
238+
public void setSameSite(String sameSite) {
239+
this.sameSite = sameSite;
240+
}
241+
208242
}
209243

210244
/**

0 commit comments

Comments
 (0)