672.aa2a7e31.js 11.4 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1
(window.webpackJsonp=window.webpackJsonp||[]).push([[672],{1102:function(e,s,t){"use strict";t.r(s);var r=t(56),o=Object(r.a)({},(function(){var e=this,s=e.$createElement,t=e._self._c||s;return t("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[t("h1",{attrs:{id:"spring-session-spring-boot"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#spring-session-spring-boot"}},[e._v("#")]),e._v(" Spring Session- Spring Boot")]),e._v(" "),t("p",[e._v("本指南描述了在使用 Spring 引导时如何使用 Spring Session 透明地利用 Redis 来支持 Web 应用程序的"),t("code",[e._v("HttpSession")]),e._v("")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("你可以在"),t("a",{attrs:{href:"#boot-sample"}},[e._v("引导示例应用程序")]),e._v("中找到完整的指南。")])])]),e._v(" "),t("tbody")]),e._v(" "),t("p",[t("RouterLink",{attrs:{to:"/spring-session/bootSamples/HttpSession/index.html"}},[e._v("Index")])],1),e._v(" "),t("h2",{attrs:{id:"更新依赖项"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#更新依赖项"}},[e._v("#")]),e._v(" 更新依赖项")]),e._v(" "),t("p",[e._v("在使用 Spring Session 之前,你必须确保你的依赖关系。我们假设你正在使用一个有效的启动 Web 应用程序。如果正在使用 Maven,则必须添加以下依赖项:")]),e._v(" "),t("p",[e._v("POM.xml")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("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")])])]),t("p",[e._v("Spring 启动为 Spring Session 模块提供了依赖管理,因此不需要显式声明依赖版本。")]),e._v(" "),t("h2",{attrs:{id:"spring-引导配置"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#spring-引导配置"}},[e._v("#")]),e._v(" Spring 引导配置")]),e._v(" "),t("p",[e._v("在添加了所需的依赖项之后,我们就可以创建我们的 Spring 启动配置了。多亏了一流的自动配置支持,由 Redis 支持的设置 Spring Session 非常简单,只需向你的"),t("code",[e._v("application.properties")]),e._v("添加一个配置属性,如以下清单所示:")]),e._v(" "),t("p",[e._v("SRC/主/资源/应用程序.properties")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("spring.session.store-type=redis # Session store type.\n")])])]),t("p",[e._v("在这种情况下, Spring boot 应用的配置相当于手动添加"),t("code",[e._v("@EnableRedisHttpSession")]),e._v("注释。这将创建一个名为"),t("code",[e._v("springSessionRepositoryFilter")]),e._v("的 Spring  Bean,实现"),t("code",[e._v("Filter")]),e._v("。过滤器负责替换要由 Spring Session 支持的"),t("code",[e._v("HttpSession")]),e._v("实现。")]),e._v(" "),t("p",[e._v("使用"),t("code",[e._v("application.properties")]),e._v("还可以进行进一步的定制,如以下清单所示:")]),e._v(" "),t("p",[e._v("SRC/主/资源/应用程序.properties")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("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")])])]),t("p",[e._v("有关更多信息,请参见 Spring 引导文档的"),t("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"),t("OutboundLink")],1),e._v("部分。")]),e._v(" "),t("h2",{attrs:{id:"配置-redis-连接"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#配置-redis-连接"}},[e._v("#")]),e._v(" 配置 Redis 连接")]),e._v(" "),t("p",[e._v("Spring 启动会自动创建一个"),t("code",[e._v("RedisConnectionFactory")]),e._v(",它将 Spring Session 连接到端口 6379(默认端口)上本地主机上的 Redis 服务器。在生产环境中,你需要更新配置以指向 Redis 服务器。例如,你可以在应用程序中包含以下内容:")]),e._v(" "),t("p",[e._v("SRC/主/资源/应用程序.properties")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("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")])])]),t("p",[e._v("有关更多信息,请参见 Spring 引导文档的"),t("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("连接到 Redis"),t("OutboundLink")],1),e._v("部分。")]),e._v(" "),t("h2",{attrs:{id:"servlet-容器初始化"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#servlet-容器初始化"}},[e._v("#")]),e._v(" Servlet 容器初始化")]),e._v(" "),t("p",[e._v("我们的"),t("a",{attrs:{href:"#boot-spring-configuration"}},[e._v("Spring Boot Configuration")]),e._v("创建了一个名为"),t("code",[e._v("springSessionRepositoryFilter")]),e._v("的 Spring  Bean,它实现了"),t("code",[e._v("Filter")]),e._v(""),t("code",[e._v("springSessionRepositoryFilter")]),e._v(" Bean 负责用 Spring Session 支持的自定义实现替换"),t("code",[e._v("HttpSession")]),e._v("")]),e._v(" "),t("p",[e._v("为了使我们的"),t("code",[e._v("Filter")]),e._v("发挥其魔力, Spring 需要加载我们的"),t("code",[e._v("Config")]),e._v("类。最后,我们需要确保我们的 Servlet 容器(即 Tomcat)为每个请求使用我们的"),t("code",[e._v("springSessionRepositoryFilter")]),e._v("。幸运的是,Boot 为我们解决了这两个步骤。")]),e._v(" "),t("h2",{attrs:{id:"引导示例应用程序"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#引导示例应用程序"}},[e._v("#")]),e._v(" 引导示例应用程序")]),e._v(" "),t("p",[e._v("引导示例应用程序演示了如何在使用 Spring 引导时使用 Spring Session 透明地利用 Redis 来支持 Web 应用程序的"),t("code",[e._v("HttpSession")]),e._v("")]),e._v(" "),t("h3",{attrs:{id:"运行引导示例应用程序"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#运行引导示例应用程序"}},[e._v("#")]),e._v(" 运行引导示例应用程序")]),e._v(" "),t("p",[e._v("你可以通过获取"),t("a",{attrs:{href:"https://github.com/spring-projects/spring-session/archive/main.zip",target:"_blank",rel:"noopener noreferrer"}},[e._v("源代码"),t("OutboundLink")],1),e._v("并调用以下命令来运行示例:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("$ ./gradlew :spring-session-sample-boot-redis:bootRun\n")])])]),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("要使示例工作,你必须在 localhost 上"),t("a",{attrs:{href:"https://redis.io/download",target:"_blank",rel:"noopener noreferrer"}},[e._v("安装 Redis2.8+"),t("OutboundLink")],1),e._v("并使用默认端口(6379)运行它。"),t("br"),e._v("或者,你可以更新"),t("code",[e._v("RedisConnectionFactory")]),e._v("以指向 Redis 服务器。"),t("br"),e._v("另一个选项是使用"),t("a",{attrs:{href:"https://www.docker.com/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Docker"),t("OutboundLink")],1),e._v("在 localhost 上运行 Redis。详细说明见"),t("a",{attrs:{href:"https://hub.docker.com/_/redis/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Docker Redis 存储库"),t("OutboundLink")],1),e._v("")])])]),e._v(" "),t("tbody")]),e._v(" "),t("p",[e._v("现在你应该可以在"),t("a",{attrs:{href:"http://localhost:8080/",target:"_blank",rel:"noopener noreferrer"}},[e._v("http://localhost:8080/"),t("OutboundLink")],1),e._v("上访问应用程序了。")]),e._v(" "),t("h3",{attrs:{id:"探索security示例应用程序"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#探索security示例应用程序"}},[e._v("#")]),e._v(" 探索"),t("code",[e._v("security")]),e._v("示例应用程序")]),e._v(" "),t("p",[e._v("现在你可以尝试使用该应用程序了。输入以下内容即可登录:")]),e._v(" "),t("ul",[t("li",[t("p",[t("strong",[e._v("用户 Name")]),e._v(" "),t("em",[e._v("User")])])]),e._v(" "),t("li",[t("p",[t("strong",[e._v("密码")]),e._v(" "),t("em",[e._v("密码")])])])]),e._v(" "),t("p",[e._v("现在点击"),t("strong",[e._v("登录")]),e._v("按钮。你现在应该会看到一条消息,表明你是用先前输入的用户登录的。用户的信息存储在 Redis 中,而不是 Tomcat 的"),t("code",[e._v("HttpSession")]),e._v("实现中。")]),e._v(" "),t("h3",{attrs:{id:"它是如何工作的"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#它是如何工作的"}},[e._v("#")]),e._v(" 它是如何工作的?")]),e._v(" "),t("p",[e._v("我们不使用 Tomcat 的"),t("code",[e._v("HttpSession")]),e._v(",而是在 Redis 中保存这些值。 Spring Session 用一个由 Redis 支持的实现替换"),t("code",[e._v("HttpSession")]),e._v("。当 Spring Security 的"),t("code",[e._v("SecurityContextPersistenceFilter")]),e._v(""),t("code",[e._v("SecurityContext")]),e._v("保存到"),t("code",[e._v("HttpSession")]),e._v("时,它将被持久化到 Redis 中。")]),e._v(" "),t("p",[e._v("当创建一个新的"),t("code",[e._v("HttpSession")]),e._v("时, Spring Session 将在浏览器中创建一个名为"),t("code",[e._v("SESSION")]),e._v("的 cookie。该 cookie 包含你的会话的 ID。你可以查看 cookies(使用"),t("a",{attrs:{href:"https://developers.google.com/web/tools/chrome-devtools/manage-data/cookies",target:"_blank",rel:"noopener noreferrer"}},[e._v("Chrome"),t("OutboundLink")],1),e._v(""),t("a",{attrs:{href:"https://developer.mozilla.org/en-US/docs/Tools/Storage_Inspector",target:"_blank",rel:"noopener noreferrer"}},[e._v("Firefox"),t("OutboundLink")],1),e._v(")。")]),e._v(" "),t("p",[e._v("你可以使用 redis-cli 删除会话。例如,在基于 Linux 的系统上,你可以键入以下内容:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\t$ redis-cli keys '*' | xargs redis-cli del\n")])])]),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("Redis 文档中有"),t("a",{attrs:{href:"https://redis.io/topics/quickstart",target:"_blank",rel:"noopener noreferrer"}},[e._v("安装 Redis-CLI"),t("OutboundLink")],1),e._v("的说明。")])])]),e._v(" "),t("tbody")]),e._v(" "),t("p",[e._v("或者,你也可以删除显式密钥。要这样做,请在终端中输入以下内容,并确保将"),t("code",[e._v("7e8383a4-082c-4ffe-a4bc-c40fd3363c5e")]),e._v("替换为"),t("code",[e._v("SESSION")]),e._v("cookie 的值:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\t$ redis-cli del spring:session:sessions:7e8383a4-082c-4ffe-a4bc-c40fd3363c5e\n")])])]),t("p",[e._v("现在,你可以访问"),t("a",{attrs:{href:"http://localhost:8080/",target:"_blank",rel:"noopener noreferrer"}},[e._v("http://localhost:8080/"),t("OutboundLink")],1),e._v("上的应用程序,并观察到我们不再经过身份验证。")])])}),[],!1,null,null,null);s.default=o.exports}}]);