1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -42,11 +42,11 @@ public class CookieWebSessionIdResolver implements WebSessionIdResolver {
42
42
private Duration cookieMaxAge = Duration .ofSeconds (-1 );
43
43
44
44
@ Nullable
45
- private Consumer <ResponseCookie .ResponseCookieBuilder > cookieInitializer = null ;
45
+ private Consumer <ResponseCookie .ResponseCookieBuilder > initializer = null ;
46
46
47
47
48
48
/**
49
- * Set the name of the cookie to use for the session ID .
49
+ * Set the name for the session id cookie .
50
50
* <p>By default set to "SESSION".
51
51
* @param cookieName the cookie name
52
52
*/
@@ -63,32 +63,32 @@ public String getCookieName() {
63
63
}
64
64
65
65
/**
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.
70
69
* @param maxAge the maxAge duration value
70
+ * @see ResponseCookie#getMaxAge()
71
71
*/
72
72
public void setCookieMaxAge (Duration maxAge ) {
73
73
this .cookieMaxAge = maxAge ;
74
74
}
75
75
76
76
/**
77
- * Get the configured "Max-Age" attribute value for the session cookie.
77
+ * Get the configured "Max-Age" for the session id cookie.
78
78
*/
79
79
public Duration getCookieMaxAge () {
80
80
return this .cookieMaxAge ;
81
81
}
82
82
83
83
/**
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
87
87
* @since 5.1
88
88
*/
89
89
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 ;
92
92
}
93
93
94
94
@@ -115,21 +115,19 @@ public void expireSession(ServerWebExchange exchange) {
115
115
exchange .getResponse ().getCookies ().set (this .cookieName , cookie );
116
116
}
117
117
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 )
122
120
.path (exchange .getRequest ().getPath ().contextPath ().value () + "/" )
123
121
.maxAge (maxAge )
124
122
.httpOnly (true )
125
123
.secure ("https" .equalsIgnoreCase (exchange .getRequest ().getURI ().getScheme ()))
126
124
.sameSite ("Lax" );
127
125
128
- if (this .cookieInitializer != null ) {
129
- this .cookieInitializer .accept (cookieBuilder );
126
+ if (this .initializer != null ) {
127
+ this .initializer .accept (builder );
130
128
}
131
129
132
- return cookieBuilder .build ();
130
+ return builder .build ();
133
131
}
134
132
135
133
}
0 commit comments