Skip to content

Commit 49375a2

Browse files
committed
Add guide for customizing cookie in WebFlux
Resolves gh-1614
1 parent 5375f51 commit 49375a2

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
= Spring Session - WebFlux with Custom Cookie
2+
Eleftheria Stein-Kousathana
3+
:toc: left
4+
:stylesdir: ../
5+
:highlightjsdir: ../js/highlight
6+
:docinfodir: guides
7+
8+
This guide describes how to configure Spring Session to use custom cookies in a WebFlux based application.
9+
The guide assumes you have already set up Spring Session in your project using your chosen data store. For example, link:./boot-redis.html[HttpSession with Redis].
10+
11+
NOTE: You can find the completed guide in the <<webflux-custom-cookie-sample, WebFlux Custom Cookie sample application>>.
12+
13+
[#index-link]
14+
link:../index.html[Index]
15+
16+
[[webflux-custom-cookie-spring-configuration]]
17+
== Spring Boot Configuration
18+
19+
Once you have set up Spring Session, you can customize how the session cookie is written by exposing a `WebSessionIdResolver` as a Spring bean.
20+
Spring Session uses a `CookieWebSessionIdResolver` by default.
21+
Exposing the `WebSessionIdResolver` as a Spring bean augments the existing configuration when you use configurations like `@EnableRedisHttpSession`.
22+
The following example shows how to customize Spring Session's cookie:
23+
24+
====
25+
[source,java]
26+
----
27+
include::{samples-dir}spring-session-sample-boot-webflux-custom-cookie/src/main/java/sample/CookieConfig.java[tags=webflux-cookie-serializer]
28+
----
29+
30+
<1> We customize the name of the cookie to be `JSESSIONID`.
31+
<2> We customize the path of the cookie to be `/` (rather than the default of the context root).
32+
<3> We customize the `SameSite` cookie directive to be `Strict`.
33+
====
34+
35+
[[webflux-custom-cookie-sample]]
36+
== `webflux-custom-cookie` Sample Application
37+
38+
This section describes how to work with the `webflux-custom-cookie` sample application.
39+
40+
=== Running the `webflux-custom-cookie` Sample Application
41+
42+
You can run the sample by obtaining the {download-url}[source code] and invoking the following command:
43+
44+
====
45+
----
46+
$ ./gradlew :spring-session-sample-boot-webflux-custom-cookie:bootRun
47+
----
48+
====
49+
50+
NOTE: For the sample to work, you must https://redis.io/download[install Redis 2.8+] on localhost and run it with the default port (6379).
51+
Alternatively, you can update the `RedisConnectionFactory` to point to a Redis server.
52+
Another option is to use https://www.docker.com/[Docker] to run Redis on localhost. See https://hub.docker.com/_/redis/[Docker Redis repository] for detailed instructions.
53+
54+
You should now be able to access the application at http://localhost:8080/
55+
56+
=== Exploring the `webflux-custom-cookie` Sample Application
57+
58+
Now you can use the application. Fill out the form with the following information:
59+
60+
* *Attribute Name:* _username_
61+
* *Attribute Value:* _rob_
62+
63+
Now click the *Set Attribute* button.
64+
You should now see the values displayed in the table.
65+
66+
If you look at the cookies for the application, you can see the cookie is saved to the custom name of `JSESSIONID`.

spring-session-docs/src/docs/asciidoc/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ To get started with Spring Session, the best place to start is our Sample Applic
7272

7373
| {gh-samples-url}spring-session-sample-boot-webflux-custom-cookie[WebFlux with Custom Cookie]
7474
| Demonstrates how to use Spring Session to customize the Session cookie in a WebFlux based application.
75-
|
75+
| link:guides/boot-webflux-custom-cookie.html[WebFlux with Custom Cookie Guide]
7676

7777
| {gh-samples-url}spring-session-sample-boot-redis-json[HttpSession with Redis JSON serialization]
7878
| Demonstrates how to use Spring Session to replace the `HttpSession` with Redis using JSON serialization.

spring-session-samples/spring-session-sample-boot-webflux-custom-cookie/src/main/java/sample/CookieConfig.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
@Configuration
2828
public class CookieConfig {
2929

30+
// tag::webflux-cookie-serializer[]
3031
@Bean
3132
public WebSessionIdResolver webSessionIdResolver() {
3233
CookieWebSessionIdResolver resolver = new CookieWebSessionIdResolver();
33-
resolver.setCookieName("JSESSIONID");
34-
resolver.addCookieInitializer((builder) -> builder.path("/"));
35-
resolver.addCookieInitializer((builder) -> builder.sameSite("Strict"));
34+
resolver.setCookieName("JSESSIONID"); // <1>
35+
resolver.addCookieInitializer((builder) -> builder.path("/")); // <2>
36+
resolver.addCookieInitializer((builder) -> builder.sameSite("Strict")); // <3>
3637
return resolver;
3738
}
39+
// end::webflux-cookie-serializer[]
3840

3941
}

0 commit comments

Comments
 (0)