|
| 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`. |
0 commit comments