You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spring-session-docs/modules/ROOT/pages/configuration/common.adoc
+74
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,7 @@ This section contains common configurations that applies to all or most Spring S
5
5
It contains configuration examples for the following use cases:
6
6
7
7
- I need to <<changing-how-session-ids-are-generated,change the way that Session IDs are generated>>
8
+
- I need to <<customizing-session-cookie,customize the session cookie properties>>
8
9
9
10
[[changing-how-session-ids-are-generated]]
10
11
== Changing How Session IDs Are Generated
@@ -55,3 +56,76 @@ public RedisSessionRepository redisSessionRepository(RedisOperations redisOperat
55
56
}
56
57
----
57
58
======
59
+
60
+
[[customizing-session-cookie]]
61
+
== Customizing Session Cookie
62
+
63
+
Once you have set up Spring Session, you can customize how the session cookie is written by exposing a `CookieSerializer` as a Spring bean.
64
+
Spring Session comes with `DefaultCookieSerializer`.
65
+
Exposing the `DefaultCookieSerializer` as a Spring bean augments the existing configuration when you use configurations like `@EnableRedisHttpSession`.
66
+
The following example shows how to customize Spring Session's cookie:
<1> We customize the name of the cookie to be `JSESSIONID`.
75
+
<2> We customize the path of the cookie to be `/` (rather than the default of the context root).
76
+
<3> We customize the domain name pattern (a regular expression) to be `^.+?\\.(\\w+\\.[a-z]+)$`.
77
+
This allows sharing a session across domains and applications.
78
+
If the regular expression does not match, no domain is set and the existing domain is used.
79
+
If the regular expression matches, the first https://docs.oracle.com/javase/tutorial/essential/regex/groups.html[grouping] is used as the domain.
80
+
This means that a request to https://child.example.com sets the domain to `example.com`.
81
+
However, a request to http://localhost:8080/ or https://192.168.1.100:8080/ leaves the cookie unset and, thus, still works in development without any changes being necessary for production.
82
+
====
83
+
84
+
WARNING: You should only match on valid domain characters, since the domain name is reflected in the response.
85
+
Doing so prevents a malicious user from performing such attacks as https://en.wikipedia.org/wiki/HTTP_response_splitting[HTTP Response Splitting].
86
+
87
+
[[custom-cookie-options]]
88
+
=== Configuration Options
89
+
90
+
The following configuration options are available:
91
+
92
+
* `cookieName`: The name of the cookie to use.
93
+
Default: `SESSION`.
94
+
* `useSecureCookie`: Specifies whether a secure cookie should be used.
95
+
Default: Use the value of `HttpServletRequest.isSecure()` at the time of creation.
96
+
* `cookiePath`: The path of the cookie.
97
+
Default: The context root.
98
+
* `cookieMaxAge`: Specifies the max age of the cookie to be set at the time the session is created.
99
+
Default: `-1`, which indicates the cookie should be removed when the browser is closed.
100
+
* `jvmRoute`: Specifies a suffix to be appended to the session ID and included in the cookie.
101
+
Used to identify which JVM to route to for session affinity.
102
+
With some implementations (that is, Redis) this option provides no performance benefit.
103
+
However, it can help with tracing logs of a particular user.
104
+
* `domainName`: Allows specifying a specific domain name to be used for the cookie.
105
+
This option is simple to understand but often requires a different configuration between development and production environments.
106
+
See `domainNamePattern` as an alternative.
107
+
* `domainNamePattern`: A case-insensitive pattern used to extract the domain name from the `HttpServletRequest#getServerName()`.
108
+
The pattern should provide a single grouping that is used to extract the value of the cookie domain.
109
+
If the regular expression does not match, no domain is set and the existing domain is used.
110
+
If the regular expression matches, the first https://docs.oracle.com/javase/tutorial/essential/regex/groups.html[grouping] is used as the domain.
111
+
* `sameSite`: The value for the `SameSite` cookie directive.
112
+
To disable the serialization of the `SameSite` cookie directive, you may set this value to `null`.
113
+
Default: `Lax`
114
+
115
+
[[custom-cookie-in-webflux]]
116
+
=== Custom Cookie in WebFlux
117
+
118
+
You can customize how the session cookie is written in a WebFlux application by exposing a `WebSessionIdResolver` as a Spring bean.
119
+
Spring Session uses a `CookieWebSessionIdResolver` by default.
120
+
The following example shows how to customize Spring Session's cookie:
0 commit comments