323.2d67bb75.js 10.5 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1
(window.webpackJsonp=window.webpackJsonp||[]).push([[323],{749:function(e,t,o){"use strict";o.r(t);var n=o(56),s=Object(n.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-mongodb-repositories"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#spring-session-mongodb-repositories"}},[e._v("#")]),e._v(" Spring Session - MongoDB Repositories")]),e._v(" "),o("p",[e._v("This guide describes how to use Spring Session backed by MongoDB.")]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("The completed guide can be found in the "),o("a",{attrs:{href:"#mongo-sample"}},[e._v("mongo sample application")]),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("p",[o("RouterLink",{attrs:{to:"/en/spring-session/bootSamples/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 MongoDB, you must ensure to update your dependencies.\nWe assume you are working with a working Spring Boot web application.\nIf you are using Maven, ensure to 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\t<dependency>\n\t\t<groupId>org.springframework.session</groupId>\n\t\t<artifactId>spring-session-data-mongodb</artifactId>\n\t</dependency>\n</dependencies>\n")])])]),o("p",[e._v("Since We are using a SNAPSHOT version, we need to ensure to add the Spring Snapshot Maven Repository.\nEnsure you have the following in your pom.xml:")]),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("<repositories>\n\t\x3c!-- ... --\x3e\n\t<repository>\n\t\t<id>spring-snapshot</id>\n\t\t<url>https://repo.spring.io/libs-snapshot</url>\n\t</repository>\n</repositories>\n")])])]),o("h2",{attrs:{id:"spring-configuration"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#spring-configuration"}},[e._v("#")]),e._v(" Spring Configuration")]),e._v(" "),o("p",[e._v("After adding the required dependencies, we can create our Spring configuration.\nThe Spring configuration is responsible for creating a Servlet Filter that replaces the "),o("code",[e._v("HttpSession")]),e._v(" implementation with an implementation backed by Spring Session.")]),e._v(" "),o("p",[e._v("All you have to do is to add the following Spring Configuration:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("@EnableMongoHttpSession (1)\npublic class HttpSessionConfig {\n\n\t@Bean\n\tpublic JdkMongoSessionConverter jdkMongoSessionConverter() {\n\t\treturn new JdkMongoSessionConverter(Duration.ofMinutes(30)); (2)\n\t}\n\n}\n")])])]),o("table",[o("thead",[o("tr",[o("th",[o("strong",[e._v("1")])]),e._v(" "),o("th",[e._v("The "),o("code",[e._v("@EnableMongoHttpSession")]),e._v(" annotation creates a Spring Bean with the name of "),o("code",[e._v("springSessionRepositoryFilter")]),e._v(" that implements Filter."),o("br"),e._v("This filter is what replaces the default "),o("code",[e._v("HttpSession")]),e._v(" with the MongoDB-backed bean.")])])]),e._v(" "),o("tbody",[o("tr",[o("td",[o("strong",[e._v("2")])]),e._v(" "),o("td",[e._v("Configures the session timeout to 30 minutes.")])])])]),e._v(" "),o("h2",{attrs:{id:"configuring-the-mongodb-connection"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#configuring-the-mongodb-connection"}},[e._v("#")]),e._v(" Configuring the MongoDB Connection")]),e._v(" "),o("p",[e._v("Spring Boot automatically creates a "),o("code",[e._v("MongoClient")]),e._v(" that connects Spring Session to a MongoDB Server on localhost on port 27017 (default port).\nIn a production environment you need to ensure to update your configuration to point to your MongoDB server.\nFor example, you can include the following in your "),o("strong",[e._v("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.data.mongodb.host=mongo-srv\nspring.data.mongodb.port=27018\nspring.data.mongodb.database=prod\n")])])]),o("p",[e._v("For more information, refer to "),o("a",{attrs:{href:"https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-connecting-to-mongodb",target:"_blank",rel:"noopener noreferrer"}},[e._v("Connecting to MongoDB"),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-mongo-configuration"}},[e._v("Spring 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 (i.e. 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:"mongodb-sample-application"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#mongodb-sample-application"}},[e._v("#")]),e._v(" MongoDB Sample Application")]),e._v(" "),o("p",[e._v("The MongoDB Sample Application demonstrates how to use Spring Session to transparently leverage MongoDB to back a web application’s "),o("code",[e._v("HttpSession")]),e._v(" when using Spring Boot.")]),e._v(" "),o("h3",{attrs:{id:"running-the-mongodb-sample-application"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#running-the-mongodb-sample-application"}},[e._v("#")]),e._v(" Running the MongoDB 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 :samples:mongo:bootRun\n")])])]),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 security Sample Application")]),e._v(" "),o("p",[e._v("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 MongoDB 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 are actually persisting the values in Mongo.\nSpring Session replaces the "),o("code",[e._v("HttpSession")]),e._v(" with an implementation that is backed by Mongo.\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 Mongo.")]),e._v(" "),o("p",[e._v("When a new "),o("code",[e._v("HttpSession")]),e._v(" is created, Spring Session creates a cookie named SESSION in your browser that contains the id of your session.\nGo ahead and view the cookies (click for help with "),o("a",{attrs:{href:"https://developer.chrome.com/devtools/docs/resources#cookies",target:"_blank",rel:"noopener noreferrer"}},[e._v("Chrome"),o("OutboundLink")],1),e._v(" or "),o("a",{attrs:{href:"https://getfirebug.com/wiki/index.php/Cookies_Panel#Cookies_List",target:"_blank",rel:"noopener noreferrer"}},[e._v("Firefox"),o("OutboundLink")],1),e._v(").")]),e._v(" "),o("p",[e._v("If you like, you can easily inspect the session using mongo client. For example, on a Linux based system you can type:")]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("The sample application uses an embedded MongoDB instance that listens on a randomly allocated port."),o("br"),e._v("The port used by embedded MongoDB together with exact command to connect to it is logged during application startup.")])])]),e._v(" "),o("tbody")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("$ mongo --port ...\n> use test\n> db.sessions.find().pretty()\n")])])]),o("p",[e._v("Alternatively, you can also delete the explicit key. Enter the following into your terminal ensuring to replace "),o("code",[e._v("60f17293-839b-477c-bb92-07a9c3658843")]),e._v(" with the value of your SESSION cookie:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('> db.sessions.remove({"_id":"60f17293-839b-477c-bb92-07a9c3658843"})\n')])])]),o("p",[e._v("Now 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=s.exports}}]);