Skip to content

Commit a8a9f95

Browse files
Merge branch '3.0.x' into 3.1.x
Closes gh-2467
2 parents a4f3ac2 + 4602eaa commit a8a9f95

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@
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]
28+
** xref:configuration/common.adoc[Common Configurations]
2029
* xref:http-session.adoc[HttpSession Integration]
2130
* xref:web-socket.adoc[WebSocket Integration]
2231
* xref:web-session.adoc[WebSession Integration]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[[common-configurations]]
2+
= Common Configurations
3+
4+
This section contains common configurations that applies to all or most Spring Session modules.
5+
It contains configuration examples for the following use cases:
6+
7+
- I need to <<customizing-session-cookie,customize the session cookie properties>>
8+
9+
[[customizing-session-cookie]]
10+
== Customizing Session Cookie
11+
12+
Once you have set up Spring Session, you can customize how the session cookie is written by exposing a `CookieSerializer` as a Spring bean.
13+
Spring Session comes with `DefaultCookieSerializer`.
14+
Exposing the `DefaultCookieSerializer` as a Spring bean augments the existing configuration when you use configurations like `@EnableRedisHttpSession`.
15+
The following example shows how to customize Spring Session's cookie:
16+
17+
====
18+
[source,java]
19+
----
20+
include::{samples-dir}spring-session-sample-javaconfig-custom-cookie/src/main/java/sample/Config.java[tags=cookie-serializer]
21+
----
22+
23+
<1> We customize the name of the cookie to be `JSESSIONID`.
24+
<2> We customize the path of the cookie to be `/` (rather than the default of the context root).
25+
<3> We customize the domain name pattern (a regular expression) to be `^.+?\\.(\\w+\\.[a-z]+)$`.
26+
This allows sharing a session across domains and applications.
27+
If the regular expression does not match, no domain is set and the existing domain is used.
28+
If the regular expression matches, the first https://docs.oracle.com/javase/tutorial/essential/regex/groups.html[grouping] is used as the domain.
29+
This means that a request to https://child.example.com sets the domain to `example.com`.
30+
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.
31+
====
32+
33+
WARNING: You should only match on valid domain characters, since the domain name is reflected in the response.
34+
Doing so prevents a malicious user from performing such attacks as https://en.wikipedia.org/wiki/HTTP_response_splitting[HTTP Response Splitting].
35+
36+
[[custom-cookie-options]]
37+
=== Configuration Options
38+
39+
The following configuration options are available:
40+
41+
* `cookieName`: The name of the cookie to use.
42+
Default: `SESSION`.
43+
* `useSecureCookie`: Specifies whether a secure cookie should be used.
44+
Default: Use the value of `HttpServletRequest.isSecure()` at the time of creation.
45+
* `cookiePath`: The path of the cookie.
46+
Default: The context root.
47+
* `cookieMaxAge`: Specifies the max age of the cookie to be set at the time the session is created.
48+
Default: `-1`, which indicates the cookie should be removed when the browser is closed.
49+
* `jvmRoute`: Specifies a suffix to be appended to the session ID and included in the cookie.
50+
Used to identify which JVM to route to for session affinity.
51+
With some implementations (that is, Redis) this option provides no performance benefit.
52+
However, it can help with tracing logs of a particular user.
53+
* `domainName`: Allows specifying a specific domain name to be used for the cookie.
54+
This option is simple to understand but often requires a different configuration between development and production environments.
55+
See `domainNamePattern` as an alternative.
56+
* `domainNamePattern`: A case-insensitive pattern used to extract the domain name from the `HttpServletRequest#getServerName()`.
57+
The pattern should provide a single grouping that is used to extract the value of the cookie domain.
58+
If the regular expression does not match, no domain is set and the existing domain is used.
59+
If the regular expression matches, the first https://docs.oracle.com/javase/tutorial/essential/regex/groups.html[grouping] is used as the domain.
60+
* `sameSite`: The value for the `SameSite` cookie directive.
61+
To disable the serialization of the `SameSite` cookie directive, you may set this value to `null`.
62+
Default: `Lax`
63+
64+
[[custom-cookie-in-webflux]]
65+
=== Custom Cookie in WebFlux
66+
67+
You can customize how the session cookie is written in a WebFlux application by exposing a `WebSessionIdResolver` as a Spring bean.
68+
Spring Session uses a `CookieWebSessionIdResolver` by default.
69+
The following example shows how to customize Spring Session's cookie:
70+
71+
====
72+
[source,java]
73+
----
74+
include::{samples-dir}spring-session-sample-boot-webflux-custom-cookie/src/main/java/sample/CookieConfig.java[tags=webflux-cookie-serializer]
75+
----
76+
77+
<1> We customize the name of the cookie to be `JSESSIONID`.
78+
<2> We customize the path of the cookie to be `/` (rather than the default of the context root).
79+
<3> We customize the `SameSite` cookie directive to be `Strict`.
80+
====

0 commit comments

Comments
 (0)