﻿{"id":1083,"date":"2023-04-27T20:53:47","date_gmt":"2023-04-27T12:53:47","guid":{"rendered":"http:\/\/blog.sway.com.cn\/?p=1083"},"modified":"2023-04-27T20:53:47","modified_gmt":"2023-04-27T12:53:47","slug":"springboot%e4%bd%bf%e7%94%a8shiro%e6%97%b6%e7%94%a8redis%e5%ad%98%e5%82%a8%e7%94%a8%e6%88%b7session%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/blog.sway.com.cn\/?p=1083","title":{"rendered":"SpringBoot\u4f7f\u7528Shiro\u65f6\u7528Redis\u5b58\u50a8\u7528\u6237Session\u7684\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u7b2c\u4e00\u6b65\uff1a\u5f15\u5165Shiro-Redis\u4f9d\u8d56<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e00\u6b65\uff1a\u5f15\u5165Shiro-Redis\u4f9d\u8d56<\/h2>\n\n\n\n<p>\u6ce8\u610f\uff1a\u539f\u6765\u7684Shiro\u4f9d\u8d56\u53ef\u4ee5\u53bb\u6389\u4e86\uff0c\u9ed8\u8ba4\u7684\u4ee5\u4e0b\u4f9d\u8d56\u5df2\u7ecf\u96c6\u6210\u4e86Shiro<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">        &lt;!-- https:\/\/mvnrepository.com\/artifact\/org.crazycake\/shiro-redis-spring-boot-starter -->\n        &lt;dependency>\n            &lt;groupId>org.crazycake&lt;\/groupId>\n            &lt;artifactId>shiro-redis-spring-boot-starter&lt;\/artifactId>\n            &lt;version>3.3.1&lt;\/version>\n        &lt;\/dependency><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e8c\u6b65\uff1a\u4fee\u6539ShiroConfi\u914d\u7f6e\u6587\u4ef6<\/h2>\n\n\n\n<p>\u6ce8\u5165redisSessionDAO\u548credisCacheManager\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Autowired\nRedisSessionDAO redisSessionDAO;\n\n@Autowired\nRedisCacheManager redisCacheManager;<\/pre>\n\n\n\n<p>\u5c06\u5b83\u4eec\u6ce8\u5165\u60a8\u81ea\u5df1\u7684SessionManager\u548cSessionsSecurityManager\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \/\/\u6743\u9650\u7ba1\u7406\uff0c\u914d\u7f6e\u4e3b\u8981\u662fRealm\u7684\u7ba1\u7406\u8ba4\u8bc1(redis)\n    @Bean\n    public SessionManager sessionManager() {\n        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();\n        \/\/ inject redisSessionDAO\n        sessionManager.setSessionDAO(redisSessionDAO);\n        \/\/ other stuff...\n        return sessionManager;\n    }\n    @Bean\n    public DefaultWebSecurityManager securityManager(List&lt;Realm> realms, SessionManager sessionManager) {\n        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(realms);\n        \/\/inject sessionManager\n        securityManager.setSessionManager(sessionManager);\n        \/\/ inject redisCacheManager\n        securityManager.setCacheManager(redisCacheManager);\n        \/\/ other stuff...\n        return securityManager;\n    }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e09\u6b65\uff1a\u914d\u7f6e\u5c5e\u6027 Configuration Properties\uff08\u53ef\u9009\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u6807\u9898 <strong>Title<\/strong><\/th><th>\u9ed8\u8ba4 <strong>Default<\/strong><\/th><th>\u8bf4\u660e <strong>Description<\/strong><\/th><\/tr><\/thead><tbody><tr><td>shiro-redis.enabled<\/td><td><code>true<\/code><\/td><td>\u542f\u7528 shiro-redis \u7684 Spring \u6a21\u5757<\/td><\/tr><tr><td>shiro-redis.redis-manager.deploy-mode<\/td><td><code>standalone<\/code><\/td><td>Redis \u90e8\u7f72\u6a21\u5f0f\u3002\u9009\u9879: <code>standalone<\/code>, <code>sentinel<\/code>, \u2018\u96c6\u7fa4\u2019<\/td><\/tr><tr><td>shiro-redis.redis-manager.host<\/td><td><code>127.0.0.1:6379<\/code><\/td><td>Redis \u4e3b\u673a\u3002\u5982\u679c\u60a8\u4e0d\u6307\u5b9a\u4e3b\u673a\uff0c\u5219\u9ed8\u8ba4\u503c\u4e3a<code>127.0.0.1:6379<\/code>. \u5982\u679c\u4f60\u5728\u54e8\u5175\u6a21\u5f0f\u6216\u96c6\u7fa4\u6a21\u5f0f\u4e0b\u8fd0\u884c redis\uff0c\u7528\u9017\u53f7\u5206\u9694\u4e3b\u673a\u540d\uff0c\u5982<code>127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381<\/code><\/td><\/tr><tr><td>shiro-redis.redis-manager.master-name<\/td><td><code>mymaster<\/code><\/td><td><strong>\u4ec5\u7528\u4e8e\u54e8\u5175\u6a21\u5f0f<\/strong> Redis\u54e8\u5175\u6a21\u5f0f\u7684\u4e3b\u8282\u70b9<\/td><\/tr><tr><td>shiro-redis.redis-manager.timeout<\/td><td><code>2000<\/code><\/td><td>Redis \u8fde\u63a5\u8d85\u65f6\u3002jedis \u5c1d\u8bd5\u8fde\u63a5\u5230 redis \u670d\u52a1\u5668\u8d85\u65f6\uff08\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\uff09<\/td><\/tr><tr><td>shiro-redis.redis-manager.so-timeout<\/td><td><code>2000<\/code><\/td><td><strong>\u4ec5\u7528\u4e8e\u54e8\u5175\u6a21\u5f0f\u6216\u96c6\u7fa4\u6a21\u5f0f<\/strong> jedis\u5c1d\u8bd5\u4eceredis\u670d\u52a1\u5668\u8bfb\u53d6\u6570\u636e\u7684\u8d85\u65f6\u65f6\u95f4<\/td><\/tr><tr><td>shiro-redis.redis-manager.max-attempts<\/td><td><code>3<\/code><\/td><td><strong>\u4ec5\u7528\u4e8e\u96c6\u7fa4\u6a21\u5f0f<\/strong> \u6700\u5927\u5c1d\u8bd5\u8fde\u63a5\u5230\u670d\u52a1\u5668<\/td><\/tr><tr><td>shiro-redis.redis-manager.password<\/td><td><\/td><td>Redis\u5bc6\u7801<\/td><\/tr><tr><td>shiro-redis.redis-manager.database<\/td><td><code>0<\/code><\/td><td>Redis \u6570\u636e\u5e93\u3002\u9ed8\u8ba4\u503c\u4e3a 0<\/td><\/tr><tr><td>shiro-redis.redis-manager.count<\/td><td><code>100<\/code><\/td><td>\u626b\u63cf\u8ba1\u6570\u3002Shiro-redis \u4f7f\u7528 Scan \u6765\u83b7\u53d6\u952e\uff0c\u56e0\u6b64\u60a8\u53ef\u4ee5\u5b9a\u4e49\u6bcf\u6b21\u8fed\u4ee3\u8fd4\u56de\u7684\u5143\u7d20\u6570\u91cf\u3002<\/td><\/tr><tr><td>shiro-redis.session-dao.expire<\/td><td><code>-2<\/code><\/td><td>Redis \u7f13\u5b58\u952e\/\u503c\u8fc7\u671f\u65f6\u95f4\u3002\u8fc7\u671f\u65f6\u95f4\u4ee5\u79d2\u4e3a\u5355\u4f4d\u3002 \u7279\u6b8a\u503c\uff1a <code>-1<\/code>: no expire <code>-2<\/code>\uff1a\u4e0e\u4f1a\u8bdd\u8d85\u65f6\u76f8\u540c \u9ed8\u8ba4\u503c\uff1a<code>-2<\/code> <strong>\u6ce8\u610f<\/strong>\uff1a\u786e\u4fdd\u8fc7\u671f\u65f6\u95f4\u957f\u4e8e\u4f1a\u8bdd\u8d85\u65f6\u3002<\/td><\/tr><tr><td>shiro-redis.session-dao.key-prefix<\/td><td><code>shiro:session:<\/code><\/td><td>\u4e3a\u4f1a\u8bdd\u7ba1\u7406\u81ea\u5b9a\u4e49\u60a8\u7684 redis \u5bc6\u94a5\u524d\u7f00 <strong>\u6ce8\u610f<\/strong>\uff1a\u8bf7\u8bb0\u4f4f\u5728\u524d\u7f00\u672b\u5c3e\u6dfb\u52a0\u5192\u53f7\u3002<\/td><\/tr><tr><td>shiro-redis.session-dao.session-in-memory-timeout<\/td><td><code>1000<\/code><\/td><td>\u5f53\u6211\u4eec\u767b\u5f55\u65f6\uff0c<code>doReadSession(sessionId)<\/code>\u4f1a\u88ab shiro \u8c03\u7528\u5927\u7ea6 10 \u6b21\u3002\u6240\u4ee5shiro-redis\u5c06Session\u4fdd\u5b58\u5728ThreadLocal\u4e2d\u6765\u7f13\u89e3\u8fd9\u4e2a\u95ee\u9898\u3002sessionInMemoryTimeout \u662f ThreadLocal \u4e2d Session \u7684\u5230\u671f\u65f6\u95f4\u3002 \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u60a8\u4e0d\u9700\u8981\u66f4\u6539\u5b83\u3002<\/td><\/tr><tr><td>shiro-redis.session-dao.session-in-memory-enabled<\/td><td><code>true<\/code><\/td><td>\u662f\u5426\u5728 ThreadLocal \u4e2d\u542f\u7528\u4e34\u65f6\u4fdd\u5b58\u4f1a\u8bdd<\/td><\/tr><tr><td>shiro-redis.cache-manager.principal-id-field-name<\/td><td><code>id<\/code><\/td><td>\u4e3b\u4f53 ID \u5b57\u6bb5\u540d\u79f0\u3002\u60a8\u53ef\u4ee5\u83b7\u5f97\u552f\u4e00 ID \u6765\u6807\u8bc6\u6b64\u4e3b\u4f53\u7684\u5b57\u6bb5\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u60a8\u4f7f\u7528 UserInfo \u4f5c\u4e3a Principal \u7c7b\uff0c\u5219 id \u5b57\u6bb5\u53ef\u80fd<code>id<\/code>\u662f<code>userId<\/code>\u3001<code>email<\/code>\u3001 \u7b49\u3002 \u8bf7\u8bb0\u4f4f\u5c06 getter \u6dfb\u52a0\u5230\u6b64 id \u5b57\u6bb5\u3002\u4f8b\u5982\uff0c<code>getId()<\/code>, <code>getUserId(<\/code>),<code>getEmail()<\/code>\u7b49\u3002 \u9ed8\u8ba4\u503c\u662f<code>id<\/code>\uff0c\u8fd9\u610f\u5473\u7740\u60a8\u7684\u4e3b\u4f53\u5bf9\u8c61\u5fc5\u987b\u6709\u4e00\u4e2a\u65b9\u6cd5\u8c03\u7528<code>getId()<\/code><\/td><\/tr><tr><td>shiro-redis.cache-manager.expire<\/td><td><code>1800<\/code><\/td><td>Redis \u7f13\u5b58\u952e\/\u503c\u8fc7\u671f\u65f6\u95f4\u3002 \u8fc7\u671f\u65f6\u95f4\u4ee5\u79d2\u4e3a\u5355\u4f4d\u3002<\/td><\/tr><tr><td>shiro-redis.cache-manager.key-prefix<\/td><td><code>shiro:cache:<\/code><\/td><td>\u81ea\u5b9a\u4e49\u60a8\u7684 redis \u952e\u524d\u7f00\u4ee5\u8fdb\u884c\u7f13\u5b58\u7ba1\u7406 <strong>\u6ce8\u610f<\/strong>\uff1a\u8bf7\u8bb0\u4f4f\u5728\u524d\u7f00\u672b\u5c3e\u6dfb\u52a0\u5192\u53f7\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u56db\u6b65\uff1a\u542f\u52a8\u6d4b\u8bd5<\/h2>\n\n\n\n<p>\u767b\u5f55\u7a0b\u5de5\u540e\u5728redis\u4e2d\u4f1a\u627e\u5230\u5982\u4e0b\u4f1a\u8bdd\u6570\u636e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"http:\/\/blog.sway.com.cn\/wp-content\/uploads\/2023\/04\/image-16.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"371\" src=\"http:\/\/blog.sway.com.cn\/wp-content\/uploads\/2023\/04\/image-16-1024x371.png\" alt=\"\" class=\"wp-image-1084\" srcset=\"http:\/\/blog.sway.com.cn\/wp-content\/uploads\/2023\/04\/image-16-1024x371.png 1024w, http:\/\/blog.sway.com.cn\/wp-content\/uploads\/2023\/04\/image-16-300x109.png 300w, http:\/\/blog.sway.com.cn\/wp-content\/uploads\/2023\/04\/image-16-768x279.png 768w, http:\/\/blog.sway.com.cn\/wp-content\/uploads\/2023\/04\/image-16-1536x557.png 1536w, http:\/\/blog.sway.com.cn\/wp-content\/uploads\/2023\/04\/image-16-2048x743.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u7b2c\u4e00\u6b65\uff1a\u5f15\u5165Shiro-Redis\u4f9d\u8d56 \u7b2c\u4e00\u6b65\uff1a\u5f15\u5165Shiro-Redis\u4f9d\u8d56 \u6ce8\u610f\uff1a\u539f\u6765\u7684Shiro\u4f9d\u8d56\u53ef\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1083","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1083"}],"version-history":[{"count":1,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1083\/revisions"}],"predecessor-version":[{"id":1085,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1083\/revisions\/1085"}],"wp:attachment":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1083"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}