{"id":1354,"date":"2023-04-18T21:32:03","date_gmt":"2023-04-18T13:32:03","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1354"},"modified":"2023-04-18T21:32:03","modified_gmt":"2023-04-18T13:32:03","slug":"%e4%bd%bf%e7%94%a8redis%e5%ad%98%e5%82%a8session","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2023\/04\/18\/%e4%bd%bf%e7%94%a8redis%e5%ad%98%e5%82%a8session\/","title":{"rendered":"\u4f7f\u7528Redis\u5b58\u50a8Session"},"content":{"rendered":"\n<p>pom\u4f9d\u8d56\uff1a<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\">&lt;!-- spring session \u652f\u6301 -->\r\n&lt;dependency>\r\n  &lt;groupId>org.springframework.session&lt;\/groupId>\r\n  &lt;artifactId>spring-session-data-redis&lt;\/artifactId>\r\n&lt;\/dependency>\r\n&lt;dependency>\r\n  &lt;groupId>org.redisson&lt;\/groupId>\r\n  &lt;artifactId>redisson-spring-boot-starter&lt;\/artifactId>\r\n  &lt;version>3.13.0&lt;\/version>\r\n&lt;\/dependency><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<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>\n\n\n\n<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>\n<\/ul>\n\n\n\n<p>Session\u914d\u7f6e\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>@Configuration<\/strong>\n\/\/ \u539f\u672c\u4f7f\u7528@EnableSpringHttpSession\r\u6ce8\u89e3\n<strong>@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 300)<\/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();\n        serializer.setCookieName(\"JSESSIONID\");\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\n        serializer.setDomainNamePattern(\"^.+?\\\\.(\\\\w+\\\\.[a-z]+)$\");\n        serializer.setCookiePath(\"\/\");\n        serializer.setUseHttpOnlyCookie(false);\n        \/\/ \u6700\u5927\u751f\u547d\u5468\u671f\u7684\u5355\u4f4d\u662f\u79d2\n        serializer.setCookieMaxAge(24 * 60 * 60);\n        return serializer;\n    }\n    \/* \u5728\u9ed8\u8ba4Session\u4f7f\u7528\u4e2d\uff0c\u9700\u8981\u521b\u5efaSession\u4ed3\u5e93\u6765\u4fdd\u5b58Session\u4fe1\u606f\uff0c\u800c\u5728Redis\u4e2d\u88ab\u5176\u4ee3\u66ff\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u518d\u521b\u5efa\u4ed3\u5e93\n    @Bean\n    public MapSessionRepository sessionRepository() {\n        return new MapSessionRepository(new ConcurrentHashMap&lt;>());\n    }\n    *\/\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4f7f\u7528 <code>@EnableRedisHttpSession<\/code>\u00a0\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u00a0<code>maxInactiveIntervalInSeconds<\/code>\u00a0\u6ce8\u89e3\u53c2\u6570\u8bbe\u7f6e Session \u6570\u636e\u7684\u8fc7\u671f\u5931\u6548\u65f6\u95f4\uff0c\u5355\u4f4d\u662f\u79d2\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4e0d\u8bbe\u7f6e\uff0c\u7cfb\u7edf\u9ed8\u8ba4\u662f\u8fc7\u671f\u65f6\u95f4\u662f 1800 \u79d2\uff0830 \u5206\u949f\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>pom\u4f9d\u8d56\uff1a<\/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":[4],"tags":[],"class_list":["post-1354","post","type-post","status-publish","format-standard","hentry","category-4"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1354"}],"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=1354"}],"version-history":[{"count":1,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1354\/revisions"}],"predecessor-version":[{"id":1355,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1354\/revisions\/1355"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}