Skip to content

Commit 5df6e88

Browse files
committed
Polishing in CookieWebSessionIdResolver
See gh-31214
1 parent 654e822 commit 5df6e88

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

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

+18-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -42,11 +42,11 @@ public class CookieWebSessionIdResolver implements WebSessionIdResolver {
4242
private Duration cookieMaxAge = Duration.ofSeconds(-1);
4343

4444
@Nullable
45-
private Consumer<ResponseCookie.ResponseCookieBuilder> cookieInitializer = null;
45+
private Consumer<ResponseCookie.ResponseCookieBuilder> initializer = null;
4646

4747

4848
/**
49-
* Set the name of the cookie to use for the session ID.
49+
* Set the name for the session id cookie.
5050
* <p>By default set to "SESSION".
5151
* @param cookieName the cookie name
5252
*/
@@ -63,32 +63,32 @@ public String getCookieName() {
6363
}
6464

6565
/**
66-
* Set the value for the "Max-Age" attribute of the cookie that holds the
67-
* session ID.
68-
* <p>For the range of values see {@link ResponseCookie#getMaxAge()}.
69-
* <p>By default set to -1.
66+
* Set the "Max-Age" attribute for the session id cookie.
67+
* <p>By default set to -1 in which case the cookie is removed when the
68+
* browser is closed.
7069
* @param maxAge the maxAge duration value
70+
* @see ResponseCookie#getMaxAge()
7171
*/
7272
public void setCookieMaxAge(Duration maxAge) {
7373
this.cookieMaxAge = maxAge;
7474
}
7575

7676
/**
77-
* Get the configured "Max-Age" attribute value for the session cookie.
77+
* Get the configured "Max-Age" for the session id cookie.
7878
*/
7979
public Duration getCookieMaxAge() {
8080
return this.cookieMaxAge;
8181
}
8282

8383
/**
84-
* Add a {@link Consumer} for a {@code ResponseCookieBuilder} that will be invoked
85-
* for each cookie being built, just before the call to {@code build()}.
86-
* @param initializer consumer for a cookie builder
84+
* Add a {@link Consumer} to further initialize the session id cookie
85+
* after {@link #getCookieName()} and {@link #getCookieMaxAge()} are applied.
86+
* @param initializer consumer to initialize the cookie with
8787
* @since 5.1
8888
*/
8989
public void addCookieInitializer(Consumer<ResponseCookie.ResponseCookieBuilder> initializer) {
90-
this.cookieInitializer = this.cookieInitializer != null ?
91-
this.cookieInitializer.andThen(initializer) : initializer;
90+
this.initializer = this.initializer != null ?
91+
this.initializer.andThen(initializer) : initializer;
9292
}
9393

9494

@@ -115,21 +115,19 @@ public void expireSession(ServerWebExchange exchange) {
115115
exchange.getResponse().getCookies().set(this.cookieName, cookie);
116116
}
117117

118-
private ResponseCookie initSessionCookie(
119-
ServerWebExchange exchange, String id, Duration maxAge) {
120-
121-
ResponseCookie.ResponseCookieBuilder cookieBuilder = ResponseCookie.from(this.cookieName, id)
118+
private ResponseCookie initSessionCookie(ServerWebExchange exchange, String id, Duration maxAge) {
119+
ResponseCookie.ResponseCookieBuilder builder = ResponseCookie.from(this.cookieName, id)
122120
.path(exchange.getRequest().getPath().contextPath().value() + "/")
123121
.maxAge(maxAge)
124122
.httpOnly(true)
125123
.secure("https".equalsIgnoreCase(exchange.getRequest().getURI().getScheme()))
126124
.sameSite("Lax");
127125

128-
if (this.cookieInitializer != null) {
129-
this.cookieInitializer.accept(cookieBuilder);
126+
if (this.initializer != null) {
127+
this.initializer.accept(builder);
130128
}
131129

132-
return cookieBuilder.build();
130+
return builder.build();
133131
}
134132

135133
}

spring-web/src/test/java/org/springframework/web/server/session/CookieWebSessionIdResolverTests.java

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.http.ResponseCookie;
2222
import org.springframework.util.MultiValueMap;
23+
import org.springframework.web.server.ServerWebExchange;
2324
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
2425
import org.springframework.web.testfixture.server.MockServerWebExchange;
2526

@@ -33,35 +34,32 @@ public class CookieWebSessionIdResolverTests {
3334

3435
private final CookieWebSessionIdResolver resolver = new CookieWebSessionIdResolver();
3536

37+
private final ServerWebExchange exchange =
38+
MockServerWebExchange.from(MockServerHttpRequest.get("https://example.org/path"));
39+
3640

3741
@Test
3842
public void setSessionId() {
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-
assertThat(cookies).hasSize(1);
45-
ResponseCookie cookie = cookies.getFirst(this.resolver.getCookieName());
46-
assertThat(cookie).isNotNull();
47-
assertThat(cookie.toString()).isEqualTo("SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax");
43+
this.resolver.setSessionId(this.exchange, "123");
44+
assertCookieValue("SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax");
4845
}
4946

5047
@Test
5148
public void cookieInitializer() {
5249
this.resolver.addCookieInitializer(builder -> builder.domain("example.org"));
5350
this.resolver.addCookieInitializer(builder -> builder.sameSite("Strict"));
5451
this.resolver.addCookieInitializer(builder -> builder.secure(false));
52+
this.resolver.setSessionId(this.exchange, "123");
5553

56-
MockServerHttpRequest request = MockServerHttpRequest.get("https://example.org/path").build();
57-
MockServerWebExchange exchange = MockServerWebExchange.from(request);
58-
this.resolver.setSessionId(exchange, "123");
54+
assertCookieValue("SESSION=123; Path=/; Domain=example.org; HttpOnly; SameSite=Strict");
55+
}
5956

60-
MultiValueMap<String, ResponseCookie> cookies = exchange.getResponse().getCookies();
57+
private void assertCookieValue(String expected) {
58+
MultiValueMap<String, ResponseCookie> cookies = this.exchange.getResponse().getCookies();
6159
assertThat(cookies).hasSize(1);
6260
ResponseCookie cookie = cookies.getFirst(this.resolver.getCookieName());
6361
assertThat(cookie).isNotNull();
64-
assertThat(cookie.toString()).isEqualTo("SESSION=123; Path=/; Domain=example.org; HttpOnly; SameSite=Strict");
62+
assertThat(cookie.toString()).isEqualTo(expected);
6563
}
6664

6765
}

0 commit comments

Comments
 (0)