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