673.b19ed80d.js 10.6 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1
(window.webpackJsonp=window.webpackJsonp||[]).push([[673],{1104:function(e,t,s){"use strict";s.r(t);var o=s(56),r=Object(o.a)({},(function(){var e=this,t=e.$createElement,s=e._self._c||t;return s("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[s("h1",{attrs:{id:"spring-session-spring-启动"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-session-spring-启动"}},[e._v("#")]),e._v(" Spring Session- Spring 启动")]),e._v(" "),s("p",[e._v("本指南描述了在使用 Spring 引导时如何使用 Spring Session 来透明地利用关系数据库来支持 Web 应用程序的"),s("code",[e._v("HttpSession")]),e._v("")]),e._v(" "),s("table",[s("thead",[s("tr",[s("th"),e._v(" "),s("th",[e._v("你可以在"),s("a",{attrs:{href:"#httpsession-jdbc-boot-sample"}},[e._v("httpsession-jdbc-boot 示例应用程序")]),e._v("中找到完整的指南。")])])]),e._v(" "),s("tbody")]),e._v(" "),s("p",[s("RouterLink",{attrs:{to:"/spring-session/bootSamples/index.html"}},[e._v("Index")])],1),e._v(" "),s("h2",{attrs:{id:"更新依赖项"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#更新依赖项"}},[e._v("#")]),e._v(" 更新依赖项")]),e._v(" "),s("p",[e._v("在使用 Spring Session 之前,你必须更新你的依赖关系。我们假设你正在使用一个有效的启动 Web 应用程序。如果使用 Maven,则必须添加以下依赖项:")]),e._v(" "),s("p",[e._v("POM.xml")]),e._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("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-jdbc</artifactId>\n\t</dependency>\n</dependencies>\n")])])]),s("p",[e._v("Spring 启动为 Spring Session 模块提供了依赖管理,因此不需要显式地声明依赖版本。")]),e._v(" "),s("h2",{attrs:{id:"spring-引导配置"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-引导配置"}},[e._v("#")]),e._v(" Spring 引导配置")]),e._v(" "),s("p",[e._v("在添加了所需的依赖项之后,我们就可以创建我们的 Spring 启动配置了。多亏了一流的自动配置支持,在关系数据库支持下设置 Spring Session 就像在"),s("code",[e._v("application.properties")]),e._v("中添加一个配置属性一样简单。下面的清单展示了如何做到这一点:")]),e._v(" "),s("p",[e._v("SRC/主/资源/应用程序.properties")]),e._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[e._v("spring.session.store-type=jdbc # Session store type.\n")])])]),s("p",[e._v("如果在 Classpath 上存在单个 Spring Session 模块,则 Spring 引导将自动使用该存储实现。如果有多个实现,则必须选择要用来存储会话的 StoreType,如上面所示。")]),e._v(" "),s("p",[e._v("在这种情况下, Spring boot 应用的配置相当于手动添加"),s("code",[e._v("@EnableJdbcHttpSession")]),e._v("注释。这将创建一个名为"),s("code",[e._v("springSessionRepositoryFilter")]),e._v("的 Spring  Bean。 Bean 实现"),s("code",[e._v("Filter")]),e._v("。过滤器负责替换要由 Spring Session 支持的"),s("code",[e._v("HttpSession")]),e._v("实现。")]),e._v(" "),s("p",[e._v("你可以使用"),s("code",[e._v("application.properties")]),e._v("来进一步自定义。下面的清单展示了如何做到这一点:")]),e._v(" "),s("p",[e._v("SRC/主/资源/应用程序.properties")]),e._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[e._v("server.servlet.session.timeout= # Session timeout. If a duration suffix is not specified, seconds are used.\nspring.session.jdbc.initialize-schema=embedded # Database schema initialization mode.\nspring.session.jdbc.schema=classpath:org/springframework/session/jdbc/[email protected]@[email protected]@.sql # Path to the SQL file to use to initialize the database schema.\nspring.session.jdbc.table-name=SPRING_SESSION # Name of the database table used to store sessions.\n")])])]),s("p",[e._v("有关更多信息,请参见 Spring 引导文档的"),s("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"),s("OutboundLink")],1),e._v("部分。")]),e._v(" "),s("h2",{attrs:{id:"配置datasource"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置datasource"}},[e._v("#")]),e._v(" 配置"),s("code",[e._v("DataSource")])]),e._v(" "),s("p",[e._v("Spring 引导会自动创建一个"),s("code",[e._v("DataSource")]),e._v(",它将 Spring Session 连接到 H2 数据库的嵌入式实例。在生产环境中,你需要更新配置以指向关系数据库。例如,你可以在应用程序中包含以下内容:")]),e._v(" "),s("p",[e._v("SRC/主/资源/应用程序.properties")]),e._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[e._v("spring.datasource.url= # JDBC URL of the database.\nspring.datasource.username= # Login username of the database.\nspring.datasource.password= # Login password of the database.\n")])])]),s("p",[e._v("有关更多信息,请参见 Spring 引导文档的"),s("a",{attrs:{href:"https://docs.spring.io/spring-boot/docs/2.5.6/reference/htmlsingle/#boot-features-configure-datasource",target:"_blank",rel:"noopener noreferrer"}},[e._v("配置数据源"),s("OutboundLink")],1),e._v("部分。")]),e._v(" "),s("h2",{attrs:{id:"servlet-容器初始化"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#servlet-容器初始化"}},[e._v("#")]),e._v(" Servlet 容器初始化")]),e._v(" "),s("p",[e._v("我们的"),s("a",{attrs:{href:"#httpsession-jdbc-boot-spring-configuration"}},[e._v("Spring Boot Configuration")]),e._v("创建了一个名为"),s("code",[e._v("springSessionRepositoryFilter")]),e._v("的 Spring  Bean,它实现了"),s("code",[e._v("Filter")]),e._v(""),s("code",[e._v("springSessionRepositoryFilter")]),e._v(" Bean 负责用 Spring Session 支持的自定义实现替换"),s("code",[e._v("HttpSession")]),e._v("")]),e._v(" "),s("p",[e._v("为了让我们的"),s("code",[e._v("Filter")]),e._v("发挥其魔力, Spring 需要加载我们的"),s("code",[e._v("Config")]),e._v("类。最后,我们需要确保我们的 Servlet 容器(即 Tomcat)为每个请求使用我们的"),s("code",[e._v("springSessionRepositoryFilter")]),e._v("。幸运的是,Boot 为我们解决了这两个步骤。")]),e._v(" "),s("h2",{attrs:{id:"httpsession-jdbc-boot示例应用程序"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#httpsession-jdbc-boot示例应用程序"}},[e._v("#")]),e._v(" "),s("code",[e._v("httpsession-jdbc-boot")]),e._v("示例应用程序")]),e._v(" "),s("p",[e._v("HttpSession-JDBC-Boot 示例应用程序演示了如何在使用 Spring 引导时使用 Spring Session 透明地利用 H2 数据库来支持 Web 应用程序的"),s("code",[e._v("HttpSession")]),e._v("")]),e._v(" "),s("h3",{attrs:{id:"运行httpsession-jdbc-boot示例应用程序"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#运行httpsession-jdbc-boot示例应用程序"}},[e._v("#")]),e._v(" 运行"),s("code",[e._v("httpsession-jdbc-boot")]),e._v("示例应用程序")]),e._v(" "),s("p",[e._v("你可以通过获取"),s("a",{attrs:{href:"https://github.com/spring-projects/spring-session/archive/main.zip",target:"_blank",rel:"noopener noreferrer"}},[e._v("源代码"),s("OutboundLink")],1),e._v("并调用以下命令来运行示例:")]),e._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[e._v("$ ./gradlew :spring-session-sample-boot-jdbc:bootRun\n")])])]),s("p",[e._v("现在你应该可以在"),s("a",{attrs:{href:"http://localhost:8080/",target:"_blank",rel:"noopener noreferrer"}},[e._v("http://localhost:8080/"),s("OutboundLink")],1),e._v("上访问应用程序了。")]),e._v(" "),s("h3",{attrs:{id:"探索安全示例应用程序"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#探索安全示例应用程序"}},[e._v("#")]),e._v(" 探索安全示例应用程序")]),e._v(" "),s("p",[e._v("你现在可以尝试使用该应用程序了。要这样做,请输入以下内容以进行登录:")]),e._v(" "),s("ul",[s("li",[s("p",[s("strong",[e._v("用户 Name")]),e._v(" "),s("em",[e._v("User")])])]),e._v(" "),s("li",[s("p",[s("strong",[e._v("密码")]),e._v(" "),s("em",[e._v("密码")])])])]),e._v(" "),s("p",[e._v("现在点击"),s("strong",[e._v("登录")]),e._v("按钮。你现在应该会看到一条消息,该消息指示你是用先前输入的用户登录的。用户的信息存储在 H2 数据库中,而不是 Tomcat 的"),s("code",[e._v("HttpSession")]),e._v("实现。")]),e._v(" "),s("h3",{attrs:{id:"它是如何工作的"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#它是如何工作的"}},[e._v("#")]),e._v(" 它是如何工作的?")]),e._v(" "),s("p",[e._v("我们不使用 Tomcat 的"),s("code",[e._v("HttpSession")]),e._v(",而是将这些值保存在 H2 数据库中。 Spring Session 使用由关系数据库支持的实现来替换"),s("code",[e._v("HttpSession")]),e._v("。当 Spring Security 的"),s("code",[e._v("SecurityContextPersistenceFilter")]),e._v(""),s("code",[e._v("SecurityContext")]),e._v("保存到"),s("code",[e._v("HttpSession")]),e._v("时,它将被持久化到 H2 数据库中。")]),e._v(" "),s("p",[e._v("当创建一个新的"),s("code",[e._v("HttpSession")]),e._v("时, Spring Session 将在浏览器中创建一个名为"),s("code",[e._v("SESSION")]),e._v("的 cookie。该 cookie 包含你的会话的 ID。你可以查看 cookies(使用"),s("a",{attrs:{href:"https://developers.google.com/web/tools/chrome-devtools/manage-data/cookies",target:"_blank",rel:"noopener noreferrer"}},[e._v("Chrome"),s("OutboundLink")],1),e._v(""),s("a",{attrs:{href:"https://developer.mozilla.org/en-US/docs/Tools/Storage_Inspector",target:"_blank",rel:"noopener noreferrer"}},[e._v("Firefox"),s("OutboundLink")],1),e._v(")。")]),e._v(" "),s("p",[e._v("你可以通过使用 H2Web 控制台来删除会话:"),s("a",{attrs:{href:"http://localhost:8080/h2-console/",target:"_blank",rel:"noopener noreferrer"}},[e._v("http://localhost:8080/h2-console/"),s("OutboundLink")],1),e._v("(对于 JDBC URL 使用"),s("code",[e._v("jdbc:h2:mem:testdb")]),e._v(")。")]),e._v(" "),s("p",[e._v("现在,你可以访问"),s("a",{attrs:{href:"http://localhost:8080/",target:"_blank",rel:"noopener noreferrer"}},[e._v("http://localhost:8080/"),s("OutboundLink")],1),e._v("上的应用程序,并看到我们不再经过身份验证。")])])}),[],!1,null,null,null);t.default=r.exports}}]);