Skip to content

Commit 8ea6472

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

File tree

2 files changed

+37
-0
lines changed
  • spring-boot-project

2 files changed

+37
-0
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/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)