{"id":1264,"date":"2017-04-01T17:01:09","date_gmt":"2017-04-01T09:01:09","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1264"},"modified":"2017-04-01T17:01:31","modified_gmt":"2017-04-01T09:01:31","slug":"shiro-%e9%99%90%e5%88%b6%e5%b9%b6%e5%8f%91%e7%99%bb%e5%bd%95%e4%ba%ba%e6%95%b0%e6%a1%88%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1264","title":{"rendered":"Shiro \u9650\u5236\u5e76\u53d1\u767b\u5f55\u4eba\u6570\u6848\u4f8b"},"content":{"rendered":"<p>\n\tspring security \u4e2d\u5b58\u5728\u8fd9\u6837\u7684\u529f\u80fd\uff1a\u6bcf\u4e2a\u8d26\u6237\u540c\u65f6\u53ea\u80fd\u6709\u4e00\u4e2a\u4eba\u767b\u5f55\u6216\u51e0\u4e2a\u4eba\u540c\u65f6\u767b\u5f55\uff0c\u5982\u679c\u540c\u65f6\u6709\u591a\u4eba\u767b\u5f55\uff1a\u8981\u4e48\u4e0d\u8ba9\u540e\u8005\u767b\u5f55\uff1b\u8981\u4e48\u8e22\u51fa\u524d\u8005\u767b\u5f55\uff08\u5f3a\u5236\u9000\u51fa\uff09\u3002Shiro\u7684\u8bdd\u6ca1\u6709\u63d0\u4f9b\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u4e0d\u8fc7\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5728Shiro\u4e2d\u52a0\u5165\u8fd9\u4e2a\u529f\u80fd\u3002\n<\/p>\n<h2>\n\tkickoutSessionControlFilter \u63a7\u5236\u5e76\u53d1\u767b\u5f55\u4eba\u6570<br \/>\n<\/h2>\n<p>\n\tKickoutSessionControlFilter\u6838\u5fc3\u4ee3\u7801\uff1a&nbsp;\n<\/p>\n<pre class=\"brush:java;\">\r\nprotected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {  \r\n    Subject subject = getSubject(request, response);  \r\n    if(!subject.isAuthenticated() &amp;&amp; !subject.isRemembered()) {  \r\n        \/\/\u5982\u679c\u6ca1\u6709\u767b\u5f55\uff0c\u76f4\u63a5\u8fdb\u884c\u4e4b\u540e\u7684\u6d41\u7a0b  \r\n        return true;  \r\n    }  \r\n  \r\n    Session session = subject.getSession();  \r\n    String username = (String) subject.getPrincipal();  \r\n    Serializable sessionId = session.getId();  \r\n  \r\n    \/\/TODO \u540c\u6b65\u63a7\u5236  \r\n    Deque&lt;Serializable&gt; deque = cache.get(username);  \r\n    if(deque == null) {  \r\n        deque = new LinkedList&lt;Serializable&gt;();  \r\n        cache.put(username, deque);  \r\n    }  \r\n  \r\n    \/\/\u5982\u679c\u961f\u5217\u91cc\u6ca1\u6709\u6b64sessionId\uff0c\u4e14\u7528\u6237\u6ca1\u6709\u88ab\u8e22\u51fa\uff1b\u653e\u5165\u961f\u5217  \r\n    if(!deque.contains(sessionId) &amp;&amp; session.getAttribute(&quot;kickout&quot;) == null) {  \r\n        deque.push(sessionId);  \r\n    }  \r\n  \r\n    \/\/\u5982\u679c\u961f\u5217\u91cc\u7684sessionId\u6570\u8d85\u51fa\u6700\u5927\u4f1a\u8bdd\u6570\uff0c\u5f00\u59cb\u8e22\u4eba  \r\n    while(deque.size() &gt; maxSession) {  \r\n        Serializable kickoutSessionId = null;  \r\n        if(kickoutAfter) { \/\/\u5982\u679c\u8e22\u51fa\u540e\u8005  \r\n            kickoutSessionId = deque.removeFirst();  \r\n        } else { \/\/\u5426\u5219\u8e22\u51fa\u524d\u8005  \r\n            kickoutSessionId = deque.removeLast();  \r\n        }  \r\n        try {  \r\n            Session kickoutSession =  \r\n                sessionManager.getSession(new DefaultSessionKey(kickoutSessionId));  \r\n            if(kickoutSession != null) {  \r\n                \/\/\u8bbe\u7f6e\u4f1a\u8bdd\u7684kickout\u5c5e\u6027\u8868\u793a\u8e22\u51fa\u4e86  \r\n                kickoutSession.setAttribute(&quot;kickout&quot;, true);  \r\n            }  \r\n        } catch (Exception e) {\/\/ignore exception  \r\n        }  \r\n    }  \r\n  \r\n    \/\/\u5982\u679c\u88ab\u8e22\u51fa\u4e86\uff0c\u76f4\u63a5\u9000\u51fa\uff0c\u91cd\u5b9a\u5411\u5230\u8e22\u51fa\u540e\u7684\u5730\u5740  \r\n    if (session.getAttribute(&quot;kickout&quot;) != null) {  \r\n        \/\/\u4f1a\u8bdd\u88ab\u8e22\u51fa\u4e86  \r\n        try {  \r\n            subject.logout();  \r\n        } catch (Exception e) { \/\/ignore  \r\n        }  \r\n        saveRequest(request);  \r\n        WebUtils.issueRedirect(request, response, kickoutUrl);  \r\n        return false;  \r\n    }  \r\n    return true;  \r\n}   <\/pre>\n<p>\n\t\u6b64\u5904\u4f7f\u7528\u4e86Cache\u7f13\u5b58\u7528\u6237\u540d&mdash;\u4f1a\u8bddid\u4e4b\u95f4\u7684\u5173\u7cfb\uff1b\u5982\u679c\u91cf\u6bd4\u8f83\u5927\u53ef\u4ee5\u8003\u8651\u5982\u6301\u4e45\u5316\u5230\u6570\u636e\u5e93\/\u5176\u4ed6\u5e26\u6301\u4e45\u5316\u7684Cache\u4e2d\uff1b\u53e6\u5916\u6b64\u5904\u6ca1\u6709\u5e76\u53d1\u63a7\u5236\u7684\u540c\u6b65\u5b9e\u73b0\uff0c\u53ef\u4ee5\u8003\u8651\u6839\u636e\u7528\u6237\u540d\u83b7\u53d6\u9501\u6765\u63a7\u5236\uff0c\u51cf\u5c11\u9501\u7684\u7c92\u5ea6\u3002\n<\/p>\n<h2>\n\tspring-config-shiro.xml \u914d\u7f6e<br \/>\n<\/h2>\n<p>\n\t\u914d\u7f6e\u5185\u5bb9\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;bean id=&quot;kickoutSessionControlFilter&quot;   \r\nclass=&quot;com.xttblog.shiro17.web.shiro.filter.KickoutSessionControlFilter&quot;&gt;  \r\n    &lt;property name=&quot;cacheManager&quot; ref=&quot;cacheManager&quot;\/&gt;  \r\n    &lt;property name=&quot;sessionManager&quot; ref=&quot;sessionManager&quot;\/&gt;  \r\n  \r\n    &lt;property name=&quot;kickoutAfter&quot; value=&quot;false&quot;\/&gt;  \r\n    &lt;property name=&quot;maxSession&quot; value=&quot;2&quot;\/&gt;  \r\n    &lt;property name=&quot;kickoutUrl&quot; value=&quot;\/login?kickout=1&quot;\/&gt;  \r\n&lt;\/bean&gt; <\/pre>\n<ul>\n<li>\n\t\tcacheManager\uff1a\u4f7f\u7528cacheManager\u83b7\u53d6\u76f8\u5e94\u7684cache\u6765\u7f13\u5b58\u7528\u6237\u767b\u5f55\u7684\u4f1a\u8bdd\uff1b\u7528\u4e8e\u4fdd\u5b58\u7528\u6237&mdash;\u4f1a\u8bdd\u4e4b\u95f4\u7684\u5173\u7cfb\u7684\uff1b\n\t<\/li>\n<li>\n\t\tsessionManager\uff1a\u7528\u4e8e\u6839\u636e\u4f1a\u8bddID\uff0c\u83b7\u53d6\u4f1a\u8bdd\u8fdb\u884c\u8e22\u51fa\u64cd\u4f5c\u7684\uff1b\n\t<\/li>\n<li>\n\t\tkickoutAfter\uff1a\u662f\u5426\u8e22\u51fa\u540e\u6765\u767b\u5f55\u7684\uff0c\u9ed8\u8ba4\u662ffalse\uff1b\u5373\u540e\u8005\u767b\u5f55\u7684\u7528\u6237\u8e22\u51fa\u524d\u8005\u767b\u5f55\u7684\u7528\u6237\uff1b\n\t<\/li>\n<li>\n\t\tmaxSession\uff1a\u540c\u4e00\u4e2a\u7528\u6237\u6700\u5927\u7684\u4f1a\u8bdd\u6570\uff0c\u9ed8\u8ba41\uff1b\u6bd4\u59822\u7684\u610f\u601d\u662f\u540c\u4e00\u4e2a\u7528\u6237\u5141\u8bb8\u6700\u591a\u540c\u65f6\u4e24\u4e2a\u4eba\u767b\u5f55\uff1b\n\t<\/li>\n<li>\n\t\tkickoutUrl\uff1a\u88ab\u8e22\u51fa\u540e\u91cd\u5b9a\u5411\u5230\u7684\u5730\u5740\uff1b\n\t<\/li>\n<\/ul>\n<h2>\n\tShiroFilterFactoryBean \u914d\u7f6e<br \/>\n<\/h2>\n<pre class=\"brush:xml;\">\r\n&lt;bean id=&quot;shiroFilter&quot; class=&quot;org.apache.shiro.spring.web.ShiroFilterFactoryBean&quot;&gt;  \r\n &lt;property name=&quot;securityManager&quot; ref=&quot;securityManager&quot;\/&gt;  \r\n &lt;property name=&quot;loginUrl&quot; value=&quot;\/login&quot;\/&gt;  \r\n &lt;property name=&quot;filters&quot;&gt;  \r\n\t &lt;util:map&gt;  \r\n\t\t &lt;entry key=&quot;authc&quot; value-ref=&quot;formAuthenticationFilter&quot;\/&gt;  \r\n\t\t &lt;entry key=&quot;sysUser&quot; value-ref=&quot;sysUserFilter&quot;\/&gt;  \r\n\t\t &lt;entry key=&quot;kickout&quot; value-ref=&quot;kickoutSessionControlFilter&quot;\/&gt;  \r\n\t &lt;\/util:map&gt;  \r\n &lt;\/property&gt;  \r\n &lt;property name=&quot;filterChainDefinitions&quot;&gt;  \r\n\t &lt;value&gt;  \r\n\t\t \/login = authc  \r\n\t\t \/logout = logout  \r\n\t\t \/authenticated = authc  \r\n\t\t \/** = kickout,user,sysUser  \r\n\t &lt;\/value&gt;  \r\n &lt;\/property&gt;  \r\n&lt;\/bean&gt; <\/pre>\n<p>\n\t\u6b64\u5904\u914d\u7f6e\u9664\u4e86\u767b\u5f55\u7b49\u4e4b\u5916\u7684\u5730\u5740\u90fd\u8d70kickout\u62e6\u622a\u5668\u8fdb\u884c\u5e76\u53d1\u767b\u5f55\u63a7\u5236\u3002\n<\/p>\n<h2>\n\t\u8fd0\u884c\u6548\u679c\u56fe<br \/>\n<\/h2>\n<p>\n\t\u6b64\u5904\u56e0\u4e3amaxSession=2\uff0c\u6240\u4ee5\u9700\u8981\u6253\u5f003\u4e2a\u6d4f\u89c8\u5668\uff08\u9700\u8981\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\uff0c\u5982IE\u3001Chrome\u3001Firefox\uff09\uff0c\u5206\u522b\u8bbf\u95eehttp:\/\/localhost:8080\/shiro17\/\u8fdb\u884c\u767b\u5f55\uff1b\u7136\u540e\u5237\u65b0\u7b2c\u4e00\u6b21\u6253\u5f00\u7684\u6d4f\u89c8\u5668\uff0c\u5c06\u4f1a\u88ab\u5f3a\u5236\u9000\u51fa\uff0c\u5982\u663e\u793a\u4e0b\u56fe\uff1a&nbsp;\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Shiro \u9650\u5236\u540c\u65f6\u767b\u5f55\u4eba\u6570\u6848\u4f8b\" src=\"http:\/\/dl2.iteye.com\/upload\/attachment\/0095\/4881\/c9c4d4a3-5719-3eef-8b69-e35125522fde.png\" style=\"width: 287px;height: 133px\" \/>\n<\/p>\n<p>\n\t\u672c\u6587\u6e90\u7801\u4e0b\u8f7d\u94fe\u63a5\uff1a<a href=\"http:\/\/pan.baidu.com\/s\/1cEMyUq\" name=\"Shiro \u5e76\u53d1\u767b\u5f55\u63a7\u5236\u6848\u4f8b\u6e90\u7801\u4e0b\u8f7d\" target=\"_blank\" title=\"Shiro \u5e76\u53d1\u767b\u5f55\u63a7\u5236\u6848\u4f8b\u6e90\u7801\u4e0b\u8f7d\">http:\/\/pan.baidu.com\/s\/1cEMyUq<\/a> \u5bc6\u7801\uff1aoda2<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1264\">Shiro \u9650\u5236\u5e76\u53d1\u767b\u5f55\u4eba\u6570\u6848\u4f8b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>spring security \u4e2d\u5b58\u5728\u8fd9\u6837\u7684\u529f\u80fd\uff1a\u6bcf\u4e2a\u8d26\u6237\u540c\u65f6\u53ea\u80fd\u6709\u4e00\u4e2a\u4eba\u767b\u5f55\u6216\u51e0\u4e2a\u4eba\u540c\u65f6\u767b\u5f55\uff0c\u5982\u679c\u540c\u65f6\u6709\u591a\u4eba [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[416,985,979,962,963],"class_list":["post-1264","post","type-post","status-publish","format-standard","hentry","category-java","tag-shiro","tag-shiro-demo","tag-shiro-"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1264"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1264\/revisions"}],"predecessor-version":[{"id":1265,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1264\/revisions\/1265"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}