321.69316136.js 11.9 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1
(window.webpackJsonp=window.webpackJsonp||[]).push([[321],{746:function(e,t,o){"use strict";o.r(t);var s=o(56),n=Object(s.a)({},(function(){var e=this,t=e.$createElement,o=e._self._c||t;return o("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[o("h1",{attrs:{id:"spring-session-spring-boot"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#spring-session-spring-boot"}},[e._v("#")]),e._v(" Spring Session - Spring Boot")]),e._v(" "),o("p",[e._v("This guide describes how to use Spring Session to transparently leverage Redis to back a web application’s "),o("code",[e._v("HttpSession")]),e._v(" when you use Spring Boot.")]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("You can find the completed guide in the "),o("a",{attrs:{href:"#boot-sample"}},[e._v("boot sample application")]),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("p",[o("RouterLink",{attrs:{to:"/en/spring-session/bootSamples/HttpSession/index.html"}},[e._v("Index")])],1),e._v(" "),o("h2",{attrs:{id:"updating-dependencies"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#updating-dependencies"}},[e._v("#")]),e._v(" Updating Dependencies")]),e._v(" "),o("p",[e._v("Before you use Spring Session, you must ensure your dependencies.\nWe assume you are working with a working Spring Boot web application.\nIf you are using Maven, you must add the following dependencies:")]),e._v(" "),o("p",[e._v("pom.xml")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("<dependencies>\n\t\x3c!-- ... --\x3e\n\n\t<dependency>\n\t\t<groupId>org.springframework.session</groupId>\n\t\t<artifactId>spring-session-data-redis</artifactId>\n\t</dependency>\n</dependencies>\n")])])]),o("p",[e._v("Spring Boot provides dependency management for Spring Session modules, so you need not explicitly declare dependency version.")]),e._v(" "),o("h2",{attrs:{id:"spring-boot-configuration"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#spring-boot-configuration"}},[e._v("#")]),e._v(" Spring Boot Configuration")]),e._v(" "),o("p",[e._v("After adding the required dependencies, we can create our Spring Boot configuration.\nThanks to first-class auto configuration support, setting up Spring Session backed by Redis is as simple as adding a single configuration property to your "),o("code",[e._v("application.properties")]),e._v(", as the following listing shows:")]),e._v(" "),o("p",[e._v("src/main/resources/application.properties")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("spring.session.store-type=redis # Session store type.\n")])])]),o("p",[e._v("Under the hood, Spring Boot applies configuration that is equivalent to manually adding "),o("code",[e._v("@EnableRedisHttpSession")]),e._v(" annotation.\nThis creates a Spring bean with the name of "),o("code",[e._v("springSessionRepositoryFilter")]),e._v(" that implements "),o("code",[e._v("Filter")]),e._v(".\nThe filter is in charge of replacing the "),o("code",[e._v("HttpSession")]),e._v(" implementation to be backed by Spring Session.")]),e._v(" "),o("p",[e._v("Further customization is possible by using "),o("code",[e._v("application.properties")]),e._v(", as the following listing shows:")]),e._v(" "),o("p",[e._v("src/main/resources/application.properties")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("server.servlet.session.timeout= # Session timeout. If a duration suffix is not specified, seconds is used.\nspring.session.redis.flush-mode=on_save # Sessions flush mode.\nspring.session.redis.namespace=spring:session # Namespace for keys used to store sessions.\n")])])]),o("p",[e._v("For more information, see the "),o("a",{attrs:{href:"https://docs.spring.io/spring-boot/docs/2.5.6/reference/htmlsingle/#boot-features-session",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Session"),o("OutboundLink")],1),e._v(" portion of the Spring Boot documentation.")]),e._v(" "),o("h2",{attrs:{id:"configuring-the-redis-connection"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#configuring-the-redis-connection"}},[e._v("#")]),e._v(" Configuring the Redis Connection")]),e._v(" "),o("p",[e._v("Spring Boot automatically creates a "),o("code",[e._v("RedisConnectionFactory")]),e._v(" that connects Spring Session to a Redis Server on localhost on port 6379 (default port).\nIn a production environment, you need to update your configuration to point to your Redis server.\nFor example, you can include the following in your application.properties:")]),e._v(" "),o("p",[e._v("src/main/resources/application.properties")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("spring.redis.host=localhost # Redis server host.\nspring.redis.password= # Login password of the redis server.\nspring.redis.port=6379 # Redis server port.\n")])])]),o("p",[e._v("For more information, see the "),o("a",{attrs:{href:"https://docs.spring.io/spring-boot/docs/2.5.6/reference/htmlsingle/#boot-features-connecting-to-redis",target:"_blank",rel:"noopener noreferrer"}},[e._v("Connecting to Redis"),o("OutboundLink")],1),e._v(" portion of the Spring Boot documentation.")]),e._v(" "),o("h2",{attrs:{id:"servlet-container-initialization"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#servlet-container-initialization"}},[e._v("#")]),e._v(" Servlet Container Initialization")]),e._v(" "),o("p",[e._v("Our "),o("a",{attrs:{href:"#boot-spring-configuration"}},[e._v("Spring Boot Configuration")]),e._v(" created a Spring bean named "),o("code",[e._v("springSessionRepositoryFilter")]),e._v(" that implements "),o("code",[e._v("Filter")]),e._v(".\nThe "),o("code",[e._v("springSessionRepositoryFilter")]),e._v(" bean is responsible for replacing the "),o("code",[e._v("HttpSession")]),e._v(" with a custom implementation that is backed by Spring Session.")]),e._v(" "),o("p",[e._v("In order for our "),o("code",[e._v("Filter")]),e._v(" to do its magic, Spring needs to load our "),o("code",[e._v("Config")]),e._v(" class.\nLast, we need to ensure that our servlet container (that is, Tomcat) uses our "),o("code",[e._v("springSessionRepositoryFilter")]),e._v(" for every request.\nFortunately, Spring Boot takes care of both of these steps for us.")]),e._v(" "),o("h2",{attrs:{id:"boot-sample-application"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#boot-sample-application"}},[e._v("#")]),e._v(" Boot Sample Application")]),e._v(" "),o("p",[e._v("The Boot Sample Application demonstrates how to use Spring Session to transparently leverage Redis to back a web application’s "),o("code",[e._v("HttpSession")]),e._v(" when you use Spring Boot.")]),e._v(" "),o("h3",{attrs:{id:"running-the-boot-sample-application"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#running-the-boot-sample-application"}},[e._v("#")]),e._v(" Running the Boot Sample Application")]),e._v(" "),o("p",[e._v("You can run the sample by obtaining the "),o("a",{attrs:{href:"https://github.com/spring-projects/spring-session/archive/main.zip",target:"_blank",rel:"noopener noreferrer"}},[e._v("source code"),o("OutboundLink")],1),e._v(" and invoking the following command:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("$ ./gradlew :spring-session-sample-boot-redis:bootRun\n")])])]),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("For the sample to work, you must "),o("a",{attrs:{href:"https://redis.io/download",target:"_blank",rel:"noopener noreferrer"}},[e._v("install Redis 2.8+"),o("OutboundLink")],1),e._v(" on localhost and run it with the default port (6379)."),o("br"),e._v("Alternatively, you can update the "),o("code",[e._v("RedisConnectionFactory")]),e._v(" to point to a Redis server."),o("br"),e._v("Another option is to use "),o("a",{attrs:{href:"https://www.docker.com/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Docker"),o("OutboundLink")],1),e._v(" to run Redis on localhost. See "),o("a",{attrs:{href:"https://hub.docker.com/_/redis/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Docker Redis repository"),o("OutboundLink")],1),e._v(" for detailed instructions.")])])]),e._v(" "),o("tbody")]),e._v(" "),o("p",[e._v("You should now be able to access the application at "),o("a",{attrs:{href:"http://localhost:8080/",target:"_blank",rel:"noopener noreferrer"}},[e._v("http://localhost:8080/"),o("OutboundLink")],1)]),e._v(" "),o("h3",{attrs:{id:"exploring-the-security-sample-application"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#exploring-the-security-sample-application"}},[e._v("#")]),e._v(" Exploring the "),o("code",[e._v("security")]),e._v(" Sample Application")]),e._v(" "),o("p",[e._v("Now you can try using the application. Enter the following to log in:")]),e._v(" "),o("ul",[o("li",[o("p",[o("strong",[e._v("Username")]),e._v(" "),o("em",[e._v("user")])])]),e._v(" "),o("li",[o("p",[o("strong",[e._v("Password")]),e._v(" "),o("em",[e._v("password")])])])]),e._v(" "),o("p",[e._v("Now click the "),o("strong",[e._v("Login")]),e._v(" button.\nYou should now see a message indicating your are logged in with the user entered previously.\nThe user’s information is stored in Redis rather than Tomcat’s "),o("code",[e._v("HttpSession")]),e._v(" implementation.")]),e._v(" "),o("h3",{attrs:{id:"how-does-it-work"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#how-does-it-work"}},[e._v("#")]),e._v(" How Does It Work?")]),e._v(" "),o("p",[e._v("Instead of using Tomcat’s "),o("code",[e._v("HttpSession")]),e._v(", we persist the values in Redis.\nSpring Session replaces the "),o("code",[e._v("HttpSession")]),e._v(" with an implementation that is backed by Redis.\nWhen Spring Security’s "),o("code",[e._v("SecurityContextPersistenceFilter")]),e._v(" saves the "),o("code",[e._v("SecurityContext")]),e._v(" to the "),o("code",[e._v("HttpSession")]),e._v(", it is then persisted into Redis.")]),e._v(" "),o("p",[e._v("When a new "),o("code",[e._v("HttpSession")]),e._v(" is created, Spring Session creates a cookie named "),o("code",[e._v("SESSION")]),e._v(" in your browser.\nThat cookie contains the ID of your session.\nYou can view the cookies (with "),o("a",{attrs:{href:"https://developers.google.com/web/tools/chrome-devtools/manage-data/cookies",target:"_blank",rel:"noopener noreferrer"}},[e._v("Chrome"),o("OutboundLink")],1),e._v(" or "),o("a",{attrs:{href:"https://developer.mozilla.org/en-US/docs/Tools/Storage_Inspector",target:"_blank",rel:"noopener noreferrer"}},[e._v("Firefox"),o("OutboundLink")],1),e._v(").")]),e._v(" "),o("p",[e._v("You can remove the session by using redis-cli.\nFor example, on a Linux based system you can type the following:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("\t$ redis-cli keys '*' | xargs redis-cli del\n")])])]),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("The Redis documentation has instructions for "),o("a",{attrs:{href:"https://redis.io/topics/quickstart",target:"_blank",rel:"noopener noreferrer"}},[e._v("installing redis-cli"),o("OutboundLink")],1),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("p",[e._v("Alternatively, you can also delete the explicit key.\nTo do so, enter the following into your terminal, being sure to replace "),o("code",[e._v("7e8383a4-082c-4ffe-a4bc-c40fd3363c5e")]),e._v(" with the value of your "),o("code",[e._v("SESSION")]),e._v(" cookie:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("\t$ redis-cli del spring:session:sessions:7e8383a4-082c-4ffe-a4bc-c40fd3363c5e\n")])])]),o("p",[e._v("Now you can visit the application at "),o("a",{attrs:{href:"http://localhost:8080/",target:"_blank",rel:"noopener noreferrer"}},[e._v("http://localhost:8080/"),o("OutboundLink")],1),e._v(" and observe that we are no longer authenticated.")])])}),[],!1,null,null,null);t.default=n.exports}}]);