{"id":1137,"date":"2022-04-26T21:51:32","date_gmt":"2022-04-26T13:51:32","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1137"},"modified":"2022-04-26T21:51:32","modified_gmt":"2022-04-26T13:51:32","slug":"redis-session","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2022\/04\/26\/redis-session\/","title":{"rendered":"Redis Session"},"content":{"rendered":"\n<p>\u5728Spring\u9ed8\u8ba4\u7684\u9879\u76ee\u4e2d\uff0cSession\u6570\u636e\u662f\u5b58\u653e\u5728\u672c\u5730\uff0c\u5f53\u9879\u76ee\u91cd\u542f\uff0cSession\u6570\u636e\u5c31\u4f1a\u88ab\u6e05\u9664\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5b66\u4f1a\u4f7f\u7528Redis\u6765\u5b8c\u6210Session\u6570\u636e\u7684\u7f13\u5b58\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u5f15\u5165\u4f9d\u8d56<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u4f7f\u7528Redis\u8fdb\u884cSession\u9700\u8981\u5f15\u5165\u4f9d\u8d56\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u9700\u8981\u5148\u5220\u9664\u65e7\u7684Spring\u4f9d\u8d56\u9879\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\">&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework.session&lt;\/groupId&gt;\n  &lt;artifactId&gt;spring-session-core&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<p>\u6dfb\u52a0\u76f8\u5173\u4f9d\u8d56\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\"><em>&lt;!-- spring session \u652f\u6301 --&gt;<\/em>\n&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework.session&lt;\/groupId&gt;\n  &lt;artifactId&gt;spring-session-data-redis&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;org.redisson&lt;\/groupId&gt;\n  &lt;artifactId&gt;redisson-spring-boot-starter&lt;\/artifactId&gt;\n  &lt;version&gt;3.13.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><code>spring-session-data-redis<\/code>&nbsp;\u662f\u6838\u5fc3\u4f9d\u8d56\u5e93\uff0c\u4f1a\u81ea\u52a8\u5b8c\u6210 Session \u540c\u6b65\u5230 Redis \u7b49\u64cd\u4f5c<\/li><li><code>redisson-spring-boot-starter<\/code>&nbsp;\u662f\u9700\u8981\u7528\u5230\u7684 Redis \u5ba2\u6237\u7aef\uff0c\u63d0\u4f9b\u5f88\u591a\u5206\u5e03\u5f0f\u76f8\u5173\u64cd\u4f5c\u670d\u52a1\uff0c\u64cd\u4f5c Session \u6570\u636e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e0e\u6838\u5fc3\u4f9d\u8d56\u5e93\u642d\u914d\u4f7f\u7528\u3002<\/li><\/ul>\n\n\n\n<p>\u5f53\u7136\uff0cRedis\u4f9d\u8d56\u4e5f\u9700\u8981\u5f15\u5165\u3002<\/p>\n\n\n\n<div style=\"height:16px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">Session\u914d\u7f6e\u7c7b<\/p>\n\n\n\n<p>\u5728\u6211\u4eec\u4f7f\u7528Spring\u539f\u751f\u8fdb\u884cSession\u914d\u7f6e\u65f6\uff0c\u9700\u8981\u521b\u5efa\u5e76\u914d\u7f6eSession\u914d\u7f6e\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>@Configuration<\/strong>\n<strong>@EnableSpringHttpSession<\/strong>\n<strong>public<\/strong> <strong>class<\/strong> <strong>SpringHttpSessionConfig<\/strong> {\n    <strong>@Bean<\/strong>\n    <strong>public<\/strong> CookieSerializer <strong>cookieSerializer<\/strong>() {\n       DefaultCookieSerializer serializer = new DefaultCookieSerializer();\r\n       serializer.setCookieName(\"JSESSIONID\");\r\n       \/\/ \u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u914d\u7f6e\u5339\u914d\u7684\u57df\u540d\uff0c\u53ef\u4ee5\u517c\u5bb9 localhost\u3001127.0.0.1 \u7b49\u5404\u79cd\u573a\u666f\r\n       serializer.setDomainNamePattern(\"^.+?\\\\.(\\\\w+\\\\.[a-z]+)$\");\r\n       serializer.setCookiePath(\"\/\");\r\n       serializer.setUseHttpOnlyCookie(false);\r\n       \/\/ \u6700\u5927\u751f\u547d\u5468\u671f\u7684\u5355\u4f4d\u662f\u79d2\r\n       serializer.setCookieMaxAge(24 * 60 * 60);\r\n       return serializer;\n    }\n\n    <strong>@Bean<\/strong>\n    <strong>public<\/strong> MapSessionRepository <strong>sessionRepository<\/strong>() {\n        <strong>return<\/strong> <strong>new<\/strong> MapSessionRepository(<strong>new<\/strong> ConcurrentHashMap&lt;>());\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u4e86<strong>@EnableSpringHttpSession<\/strong>\u5b8c\u6210\u914d\u7f6e<\/p>\n\n\n\n<p><strong>\u6570\u636e\u4ed3\u5e93<\/strong>\uff1a\u5176\u4e2d\u56e0\u4e3a\u7cfb\u7edf\u672a\u63d0\u4f9b\u9ed8\u8ba4\u7684\u5b58\u50a8Session\u6570\u636e\u7684\u4ed3\u5e93\uff0c\u6240\u4ee5\u9700\u8981\u5728<code>sessionRepository()<\/code>\u00a0\u65b9\u6cd5\u4e2d\u00a0<code>new<\/code>\u00a0\u4e00\u4e2a\u4ed3\u5e93\u5bf9\u8c61\uff08Session\u5b58\u50a8\u4e8e\u672c\u5730\u670d\u52a1\u5668\uff09\uff0c\u6570\u636e\u5b58\u50a8\u4e0e\u7ebf\u7a0b\u5b89\u5168\u7684\u3001\u652f\u6301\u9ad8\u6548\u5e76\u53d1\u7684\u00a0<code>ConcurrentHashMap<\/code>\u3002<\/p>\n\n\n\n<p><strong>cookieSerializer()<\/strong>\u65b9\u6cd5\u662f\u7528\u6765\u5b9a\u5236Cookie\u4e2d\u7684Session\u4fe1\u606f\u5185\u5bb9\u5982\u4f55\u5199\u3002<\/p>\n\n\n\n<div style=\"height:22px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"font-size:25px\">\u4f7f\u7528Redis\u50a8\u5b58Session\u65b9\u6848\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528redis\u50a8\u5b58Session\u6838\u5fc3\u662f\u4fee\u6539\u6ce8\u89e3<code>@EnableSpringHttpSession<\/code>\u00a0\uff0c\u6539\u4e3a\u4f7f\u7528\u00a0@EnableRedisHttpSession<\/p>\n\n\n\n<p>\u56e0\u4e3aRedis\u672c\u8eab\u5c31\u662f\u4ed3\u5e93\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u518d\u53e6\u5916\u5efa\u4ed3\u5e93\uff0csessionRepository()\u4e5f\u5c31\u4e0d\u9700\u8981\u4e86\u3002<\/p>\n\n\n\n<p><code>@EnableRedisHttpSession<\/code>\u00a0\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u00a0<code>maxInactiveIntervalInSeconds<\/code>\u6ce8\u89e3\u53c2\u6570\u6765\u8bbe\u7f6eSession\u6570\u636e\u8fc7\u671f\u5931\u6548\u7684\u65f6\u95f4\uff0c\u5355\u4f4d\u662f\u79d2\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"><strong>@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 300)<\/strong><\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4e0d\u8bbe\u7f6e\uff0c\u9ed8\u8ba4\u662f1800\u79d2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Spring\u9ed8\u8ba4\u7684\u9879\u76ee\u4e2d\uff0cSession\u6570\u636e\u662f\u5b58\u653e\u5728\u672c\u5730\uff0c\u5f53\u9879\u76ee\u91cd\u542f\uff0cSession\u6570\u636e\u5c31\u4f1a\u88ab\u6e05\u9664\u3002 \u56e0\u6b64\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-1137","post","type-post","status-publish","format-standard","hentry","category-6","category-wcd"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1137"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1137"}],"version-history":[{"count":1,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1137\/revisions"}],"predecessor-version":[{"id":1138,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1137\/revisions\/1138"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}