Skip to content

Commit 2d05e53

Browse files
committed
Set path in CookieWebSessionIdResolver
Issue: SPR-16030
1 parent 4831d38 commit 2d05e53

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

spring-web/src/main/java/org/springframework/web/server/session/CookieWebSessionIdResolver.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ public void expireSession(ServerWebExchange exchange) {
9898
private void setSessionCookie(ServerWebExchange exchange, String id, Duration maxAge) {
9999
String name = getCookieName();
100100
boolean secure = "https".equalsIgnoreCase(exchange.getRequest().getURI().getScheme());
101-
MultiValueMap<String, ResponseCookie> cookieMap = exchange.getResponse().getCookies();
102-
cookieMap.set(name, ResponseCookie.from(name, id).maxAge(maxAge).httpOnly(true).secure(secure).build());
101+
String path = exchange.getRequest().getPath().contextPath().value() + "/";
102+
exchange.getResponse().getCookies().set(name,
103+
ResponseCookie.from(name, id).path(path)
104+
.maxAge(maxAge).httpOnly(true).secure(secure).build());
103105
}
104106

105107
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2002-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.server.session;
17+
18+
import org.junit.Test;
19+
20+
import org.springframework.http.ResponseCookie;
21+
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
22+
import org.springframework.mock.web.test.server.MockServerWebExchange;
23+
import org.springframework.util.MultiValueMap;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertNotNull;
27+
28+
/**
29+
* Unit tests for {@link CookieWebSessionIdResolver}.
30+
* @author Rossen Stoyanchev
31+
*/
32+
public class CookieWebSessionIdResolverTests {
33+
34+
private final CookieWebSessionIdResolver resolver = new CookieWebSessionIdResolver();
35+
36+
37+
@Test
38+
public void setSessionId() throws Exception {
39+
MockServerHttpRequest request = MockServerHttpRequest.get("https://example.org/path").build();
40+
MockServerWebExchange exchange = MockServerWebExchange.from(request);
41+
this.resolver.setSessionId(exchange, "123");
42+
43+
MultiValueMap<String, ResponseCookie> cookies = exchange.getResponse().getCookies();
44+
assertEquals(1, cookies.size());
45+
ResponseCookie cookie = cookies.getFirst(this.resolver.getCookieName());
46+
assertNotNull(cookie);
47+
assertEquals("SESSION=123; Path=/; Secure; HttpOnly", cookie.toString());
48+
}
49+
}

0 commit comments

Comments
 (0)