Skip to content

Commit 971a2d1

Browse files
committed
Set cookie path to context path without trailing slash
Closes gh-1863
1 parent 8b5b370 commit 971a2d1

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

spring-session-core/src/main/java/org/springframework/session/web/http/DefaultCookieSerializer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -432,7 +432,8 @@ private String getDomainName(HttpServletRequest request) {
432432

433433
private String getCookiePath(HttpServletRequest request) {
434434
if (this.cookiePath == null) {
435-
return request.getContextPath() + "/";
435+
String contextPath = request.getContextPath();
436+
return (contextPath != null && contextPath.length() > 0) ? contextPath : "/";
436437
}
437438
return this.cookiePath;
438439
}

spring-session-core/src/test/java/org/springframework/session/web/http/CookieHttpSessionIdResolverTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -109,7 +109,7 @@ void onNewSessionCookiePath() {
109109
this.strategy.setSessionId(this.request, this.response, this.session.getId());
110110

111111
Cookie sessionCookie = this.response.getCookie(this.cookieName);
112-
assertThat(sessionCookie.getPath()).isEqualTo(this.request.getContextPath() + "/");
112+
assertThat(sessionCookie.getPath()).isEqualTo(this.request.getContextPath());
113113
}
114114

115115
@Test
@@ -131,7 +131,7 @@ void onDeleteSessionCookiePath() {
131131
this.strategy.expireSession(this.request, this.response);
132132

133133
Cookie sessionCookie = this.response.getCookie(this.cookieName);
134-
assertThat(sessionCookie.getPath()).isEqualTo(this.request.getContextPath() + "/");
134+
assertThat(sessionCookie.getPath()).isEqualTo(this.request.getContextPath());
135135
}
136136

137137
@Test

spring-session-core/src/test/java/org/springframework/session/web/http/DefaultCookieSerializerTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -267,15 +267,15 @@ void writeCookieCookiePathDefaultEmptyContextPathUsed() {
267267
void writeCookieCookiePathDefaultContextPathUsed() {
268268
this.request.setContextPath("/context");
269269
this.serializer.writeCookieValue(cookieValue(this.sessionId));
270-
assertThat(getCookie().getPath()).isEqualTo("/context/");
270+
assertThat(getCookie().getPath()).isEqualTo("/context");
271271
}
272272

273273
@Test
274274
void writeCookieCookiePathExplicitNullCookiePathContextPathUsed() {
275275
this.request.setContextPath("/context");
276276
this.serializer.setCookiePath(null);
277277
this.serializer.writeCookieValue(cookieValue(this.sessionId));
278-
assertThat(getCookie().getPath()).isEqualTo("/context/");
278+
assertThat(getCookie().getPath()).isEqualTo("/context");
279279
}
280280

281281
@Test

0 commit comments

Comments
 (0)