Skip to content

Commit d3786ce

Browse files
Merge branch '3.1.x'
Closes gh-2468
2 parents 19d97d9 + a8a9f95 commit d3786ce

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

spring-session-docs/modules/ROOT/nav.adoc

+8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@
1414
*** {gh-samples-url}spring-session-sample-boot-webflux[Redis]
1515
*** xref:guides/boot-webflux-custom-cookie.adoc[Custom Cookie]
1616
** Java Configuration
17+
*** xref:guides/java-custom-cookie.adoc[Custom Cookie]
18+
*** xref:guides/java-redis.adoc[Redis]
19+
*** xref:guides/java-jdbc.adoc[JDBC]
20+
*** xref:guides/java-hazelcast.adoc[Hazelcast]
21+
*** xref:guides/java-rest.adoc[REST]
22+
*** xref:guides/java-security.adoc[Spring Security]
1723
** XML Configuration
24+
*** xref:guides/xml-redis.adoc[Redis]
25+
*** xref:guides/xml-jdbc.adoc[JDBC]
1826
* xref:configurations.adoc[Configurations]
1927
** xref:configuration/redis.adoc[Redis]
2028
** xref:configuration/common.adoc[Common Configurations]

spring-session-docs/modules/ROOT/pages/configuration/common.adoc

+74
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This section contains common configurations that applies to all or most Spring S
55
It contains configuration examples for the following use cases:
66

77
- 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>>
89

910
[[changing-how-session-ids-are-generated]]
1011
== Changing How Session IDs Are Generated
@@ -55,3 +56,76 @@ public RedisSessionRepository redisSessionRepository(RedisOperations redisOperat
5556
}
5657
----
5758
======
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:
67+
68+
====
69+
[source,java]
70+
----
71+
include::{samples-dir}spring-session-sample-javaconfig-custom-cookie/src/main/java/sample/Config.java[tags=cookie-serializer]
72+
----
73+
74+
<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:
121+
122+
====
123+
[source,java]
124+
----
125+
include::{samples-dir}spring-session-sample-boot-webflux-custom-cookie/src/main/java/sample/CookieConfig.java[tags=webflux-cookie-serializer]
126+
----
127+
128+
<1> We customize the name of the cookie to be `JSESSIONID`.
129+
<2> We customize the path of the cookie to be `/` (rather than the default of the context root).
130+
<3> We customize the `SameSite` cookie directive to be `Strict`.
131+
====

0 commit comments

Comments
 (0)