From 49375a28fae80edad162f7185bdb73dff771453f Mon Sep 17 00:00:00 2001 From: Eleftheria Stein Date: Tue, 28 Apr 2020 16:25:40 -0400 Subject: [PATCH] Add guide for customizing cookie in WebFlux Resolves gh-1614 --- .../guides/boot-webflux-custom-cookie.adoc | 66 +++++++++++++++++++ .../src/docs/asciidoc/index.adoc | 2 +- .../src/main/java/sample/CookieConfig.java | 8 ++- 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 spring-session-docs/src/docs/asciidoc/guides/boot-webflux-custom-cookie.adoc diff --git a/spring-session-docs/src/docs/asciidoc/guides/boot-webflux-custom-cookie.adoc b/spring-session-docs/src/docs/asciidoc/guides/boot-webflux-custom-cookie.adoc new file mode 100644 index 00000000..e48b3fbe --- /dev/null +++ b/spring-session-docs/src/docs/asciidoc/guides/boot-webflux-custom-cookie.adoc @@ -0,0 +1,66 @@ += Spring Session - WebFlux with Custom Cookie +Eleftheria Stein-Kousathana +:toc: left +:stylesdir: ../ +:highlightjsdir: ../js/highlight +:docinfodir: guides + +This guide describes how to configure Spring Session to use custom cookies in a WebFlux based application. +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]. + +NOTE: You can find the completed guide in the <>. + +[#index-link] +link:../index.html[Index] + +[[webflux-custom-cookie-spring-configuration]] +== Spring Boot Configuration + +Once you have set up Spring Session, you can customize how the session cookie is written by exposing a `WebSessionIdResolver` as a Spring bean. +Spring Session uses a `CookieWebSessionIdResolver` by default. +Exposing the `WebSessionIdResolver` as a Spring bean augments the existing configuration when you use configurations like `@EnableRedisHttpSession`. +The following example shows how to customize Spring Session's cookie: + +==== +[source,java] +---- +include::{samples-dir}spring-session-sample-boot-webflux-custom-cookie/src/main/java/sample/CookieConfig.java[tags=webflux-cookie-serializer] +---- + +<1> We customize the name of the cookie to be `JSESSIONID`. +<2> We customize the path of the cookie to be `/` (rather than the default of the context root). +<3> We customize the `SameSite` cookie directive to be `Strict`. +==== + +[[webflux-custom-cookie-sample]] +== `webflux-custom-cookie` Sample Application + +This section describes how to work with the `webflux-custom-cookie` sample application. + +=== Running the `webflux-custom-cookie` Sample Application + +You can run the sample by obtaining the {download-url}[source code] and invoking the following command: + +==== +---- +$ ./gradlew :spring-session-sample-boot-webflux-custom-cookie:bootRun +---- +==== + +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). +Alternatively, you can update the `RedisConnectionFactory` to point to a Redis server. +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. + +You should now be able to access the application at http://localhost:8080/ + +=== Exploring the `webflux-custom-cookie` Sample Application + +Now you can use the application. Fill out the form with the following information: + +* *Attribute Name:* _username_ +* *Attribute Value:* _rob_ + +Now click the *Set Attribute* button. +You should now see the values displayed in the table. + +If you look at the cookies for the application, you can see the cookie is saved to the custom name of `JSESSIONID`. diff --git a/spring-session-docs/src/docs/asciidoc/index.adoc b/spring-session-docs/src/docs/asciidoc/index.adoc index 2992374f..f6519589 100644 --- a/spring-session-docs/src/docs/asciidoc/index.adoc +++ b/spring-session-docs/src/docs/asciidoc/index.adoc @@ -72,7 +72,7 @@ To get started with Spring Session, the best place to start is our Sample Applic | {gh-samples-url}spring-session-sample-boot-webflux-custom-cookie[WebFlux with Custom Cookie] | Demonstrates how to use Spring Session to customize the Session cookie in a WebFlux based application. -| +| link:guides/boot-webflux-custom-cookie.html[WebFlux with Custom Cookie Guide] | {gh-samples-url}spring-session-sample-boot-redis-json[HttpSession with Redis JSON serialization] | Demonstrates how to use Spring Session to replace the `HttpSession` with Redis using JSON serialization. diff --git a/spring-session-samples/spring-session-sample-boot-webflux-custom-cookie/src/main/java/sample/CookieConfig.java b/spring-session-samples/spring-session-sample-boot-webflux-custom-cookie/src/main/java/sample/CookieConfig.java index 95a4f1a6..cc5f3c88 100644 --- a/spring-session-samples/spring-session-sample-boot-webflux-custom-cookie/src/main/java/sample/CookieConfig.java +++ b/spring-session-samples/spring-session-sample-boot-webflux-custom-cookie/src/main/java/sample/CookieConfig.java @@ -27,13 +27,15 @@ import org.springframework.web.server.session.WebSessionIdResolver; @Configuration public class CookieConfig { + // tag::webflux-cookie-serializer[] @Bean public WebSessionIdResolver webSessionIdResolver() { CookieWebSessionIdResolver resolver = new CookieWebSessionIdResolver(); - resolver.setCookieName("JSESSIONID"); - resolver.addCookieInitializer((builder) -> builder.path("/")); - resolver.addCookieInitializer((builder) -> builder.sameSite("Strict")); + resolver.setCookieName("JSESSIONID"); // <1> + resolver.addCookieInitializer((builder) -> builder.path("/")); // <2> + resolver.addCookieInitializer((builder) -> builder.sameSite("Strict")); // <3> return resolver; } + // end::webflux-cookie-serializer[] }