{"id":1095,"date":"2017-02-21T11:29:06","date_gmt":"2017-02-21T03:29:06","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1095"},"modified":"2017-10-09T17:40:59","modified_gmt":"2017-10-09T09:40:59","slug":"spring-%e6%95%b4%e5%90%88-shiro-%e5%ae%9e%e7%8e%b0%e7%99%bb%e5%bd%95%e8%ae%a4%e8%af%81%e5%92%8c%e6%9d%83%e9%99%90%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1095","title":{"rendered":"Spring \u6574\u5408 Shiro \u5b9e\u73b0\u767b\u5f55\u8ba4\u8bc1\u548c\u6743\u9650\u63a7\u5236"},"content":{"rendered":"<p>\n\t\u524d\u9762\u6709\u4e00\u7bc7\u6587\u7ae0\u5199\u5230\u4e86 <a href=\"http:\/\/shiro.apache.org\/\" target=\"_blank\">Shiro<\/a> \u8ba4\u8bc1\u7684\u76f8\u5173\u6570\u636e\u5e93\u8bbe\u8ba1\u300a<a href=\"http:\/\/www.xttblog.com\/?p=979\" target=\"_blank\">\u5f00\u6e90\u6743\u9650\u6846\u67b6 Shiro \u6574\u5408 web \u9879\u76ee\u7684\u6570\u636e\u5e93\u8bbe\u8ba1<\/a>\u300b\u3002\u4eca\u5929\u63a5\u7740\u5b8c\u6210\u76f8\u5173\u540e\u53f0\u4ee3\u7801\uff0c\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\n<\/p>\n<h2>\n\t\u529f\u80fd\u903b\u8f91<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t\u7528\u6237\u5fc5\u987b\u8981\u767b\u9646\u4e4b\u540e\u624d\u80fd\u8bbf\u95ee\u5b9a\u4e49\u94fe\u63a5\uff0c\u5426\u5219\u8df3\u8f6c\u5230\u767b\u5f55\u9875\u9762\u3002\n\t<\/li>\n<li>\n\t\t\u5bf9\u94fe\u63a5\u8fdb\u884c\u6743\u9650\u63a7\u5236\uff0c\u53ea\u6709\u5f53\u5f53\u524d\u767b\u5f55\u7528\u6237\u6709\u8fd9\u4e2a\u94fe\u63a5\u8bbf\u95ee\u6743\u9650\u624d\u53ef\u4ee5\u8bbf\u95ee\uff0c\u5426\u5219\u8df3\u8f6c\u5230\u6307\u5b9a\u9875\u9762\u3002\n\t<\/li>\n<li>\n\t\t\u8f93\u5165\u9519\u8bef\u5bc6\u7801\u7528\u6237\u540d\u6216\u5219\u7528\u6237\u88ab\u8bbe\u7f6e\u4e3a\u7981\u6b62\u767b\u5f55\uff0c\u8fd4\u56de\u76f8\u5e94json\u4e32\u4fe1\u606f\u3002\n\t<\/li>\n<\/ul>\n<h2>\n\t\u914d\u7f6e shiro \u4f9d\u8d56\u5305\u5230pom.xml<br \/>\n<\/h2>\n<pre class=\"brush:xml;\">\r\n&lt;!-- shiro\u6743\u9650\u63a7\u5236\u6846\u67b6 --&gt;\r\n&lt;dependency&gt;\r\n    &lt;groupId&gt;org.apache.shiro&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;shiro-spring&lt;\/artifactId&gt;\r\n    &lt;version&gt;1.3.2&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>\n\tDao\u5c42\u7684entity\uff0cservice\uff0cmapper\u7b49\u6211\u662f\u91c7\u7528mybatisplus\u7684\u4ee3\u7801\u81ea\u52a8\u751f\u6210\u5de5\u5177\u751f\u6210\u7684\uff0c\u5177\u5907\u4e86\u5355\u8868\u7684\u589e\u5220\u6539\u67e5\u529f\u80fd\u548c\u5206\u9875\u529f\u80fd\uff0c\u6bd4\u8f83\u65b9\u4fbf\uff0c\u8fd9\u91cc\u6211\u5c31\u4e0d\u8d34\u4ee3\u7801\u4e86\u3002\n<\/p>\n<h2>\n\t\u914d\u7f6e Shiro<br \/>\n<\/h2>\n<pre class=\"brush:java;\">\r\n@Configuration\r\npublic class ShiroConfig {\r\n\t\/**\r\n\t * ShiroFilterFactoryBean \u5904\u7406\u62e6\u622a\u8d44\u6e90\u6587\u4ef6\u95ee\u9898\u3002\r\n\t * \u6ce8\u610f\uff1a\u5355\u72ec\u4e00\u4e2aShiroFilterFactoryBean\u914d\u7f6e\u662f\u6216\u62a5\u9519\u7684\uff0c\u4ee5\u4e3a\u5728\r\n\t * \u521d\u59cb\u5316ShiroFilterFactoryBean\u7684\u65f6\u5019\u9700\u8981\u6ce8\u5165\uff1aSecurityManager\r\n\t *\r\n\t * Filter Chain\u5b9a\u4e49\u8bf4\u660e 1\u3001\u4e00\u4e2aURL\u53ef\u4ee5\u914d\u7f6e\u591a\u4e2aFilter\uff0c\u4f7f\u7528\u9017\u53f7\u5206\u9694 2\u3001\u5f53\u8bbe\u7f6e\u591a\u4e2a\u8fc7\u6ee4\u5668\u65f6\uff0c\u5168\u90e8\u9a8c\u8bc1\u901a\u8fc7\uff0c\u624d\u89c6\u4e3a\u901a\u8fc7\r\n\t * 3\u3001\u90e8\u5206\u8fc7\u6ee4\u5668\u53ef\u6307\u5b9a\u53c2\u6570\uff0c\u5982perms\uff0croles\r\n\t *\r\n\t *\/\r\n\t@Bean\r\n\tpublic ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {\r\n\t\tShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();\r\n\t\t\/\/ \u5fc5\u987b\u8bbe\u7f6e SecurityManager\r\n\t\tshiroFilterFactoryBean.setSecurityManager(securityManager);\r\n\t\t\/\/ \u5982\u679c\u4e0d\u8bbe\u7f6e\u9ed8\u8ba4\u4f1a\u81ea\u52a8\u5bfb\u627eWeb\u5de5\u7a0b\u6839\u76ee\u5f55\u4e0b\u7684&quot;\/login.jsp&quot;\u9875\u9762\r\n\t\tshiroFilterFactoryBean.setLoginUrl(&quot;\/login&quot;);\r\n\t\t\/\/ \u767b\u5f55\u6210\u529f\u540e\u8981\u8df3\u8f6c\u7684\u94fe\u63a5\r\n\t\tshiroFilterFactoryBean.setSuccessUrl(&quot;\/index&quot;);\r\n\t\t\/\/ \u672a\u6388\u6743\u754c\u9762;\r\n\t\tshiroFilterFactoryBean.setUnauthorizedUrl(&quot;\/403&quot;);\r\n\t\t\/\/ \u62e6\u622a\u5668.\r\n\t\tMap&lt;String, String&gt; filterChainDefinitionMap = new LinkedHashMap&lt;String, String&gt;();\r\n\t\t\/\/ \u914d\u7f6e\u4e0d\u4f1a\u88ab\u62e6\u622a\u7684\u94fe\u63a5 \u987a\u5e8f\u5224\u65ad\r\n\t\tfilterChainDefinitionMap.put(&quot;\/static\/**&quot;, &quot;anon&quot;);\r\n\t\tfilterChainDefinitionMap.put(&quot;\/ajaxLogin&quot;, &quot;anon&quot;);\r\n\t\t\/\/ \u914d\u7f6e\u9000\u51fa\u8fc7\u6ee4\u5668,\u5176\u4e2d\u7684\u5177\u4f53\u7684\u9000\u51fa\u4ee3\u7801Shiro\u5df2\u7ecf\u66ff\u6211\u4eec\u5b9e\u73b0\u4e86\r\n\t\tfilterChainDefinitionMap.put(&quot;\/logout&quot;, &quot;logout&quot;);\r\n\t\tfilterChainDefinitionMap.put(&quot;\/add&quot;, &quot;perms[\u6743\u9650\u6dfb\u52a0]&quot;);\r\n\t\t\/\/ &lt;!-- \u8fc7\u6ee4\u94fe\u5b9a\u4e49\uff0c\u4ece\u4e0a\u5411\u4e0b\u987a\u5e8f\u6267\u884c\uff0c\u4e00\u822c\u5c06 \/**\u653e\u5728\u6700\u4e3a\u4e0b\u8fb9 --&gt;:\u8fd9\u662f\u4e00\u4e2a\u5751\u5462\uff0c\u4e00\u4e0d\u5c0f\u5fc3\u4ee3\u7801\u5c31\u4e0d\u597d\u4f7f\u4e86;\r\n\t\t\/\/ &lt;!-- authc:\u6240\u6709url\u90fd\u5fc5\u987b\u8ba4\u8bc1\u901a\u8fc7\u624d\u53ef\u4ee5\u8bbf\u95ee; anon:\u6240\u6709url\u90fd\u90fd\u53ef\u4ee5\u533f\u540d\u8bbf\u95ee--&gt;\r\n\t\tfilterChainDefinitionMap.put(&quot;\/**&quot;, &quot;authc&quot;);\r\n\t\tshiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);\r\n\t\tSystem.out.println(&quot;Shiro\u62e6\u622a\u5668\u5de5\u5382\u7c7b\u6ce8\u5165\u6210\u529f&quot;);\r\n\t\treturn shiroFilterFactoryBean;\r\n\t}\r\n\t@Bean\r\n\tpublic SecurityManager securityManager() {\r\n\t\tDefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();\r\n\t\t\/\/ \u8bbe\u7f6erealm.\r\n\t\tsecurityManager.setRealm(myShiroRealm());\r\n\t\treturn securityManager;\r\n\t}\r\n\t\/**\r\n\t * \u8eab\u4efd\u8ba4\u8bc1realm; (\u8fd9\u4e2a\u9700\u8981\u81ea\u5df1\u5199\uff0c\u8d26\u53f7\u5bc6\u7801\u6821\u9a8c\uff1b\u6743\u9650\u7b49)\r\n\t * \r\n\t * @return\r\n\t *\/\r\n\t@Bean\r\n\tpublic MyShiroRealm myShiroRealm() {\r\n\t\tMyShiroRealm myShiroRealm = new MyShiroRealm();\r\n\t\treturn myShiroRealm;\r\n\t}\r\n}<\/pre>\n<h2>\n\t\u767b\u5f55\u8ba4\u8bc1\u5b9e\u73b0<br \/>\n<\/h2>\n<p>\n\t\u5728\u8ba4\u8bc1\u3001\u6388\u6743\u5185\u90e8\u5b9e\u73b0\u673a\u5236\u4e2d\u90fd\u6709\u63d0\u5230\uff0c\u6700\u7ec8\u5904\u7406\u90fd\u5c06\u4ea4\u7ed9Real\u8fdb\u884c\u5904\u7406\u3002\u56e0\u4e3a\u5728Shiro\u4e2d\uff0c\u6700\u7ec8\u662f\u901a\u8fc7Realm\u6765\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u7528\u6237\u3001\u89d2\u8272\u53ca\u6743\u9650\u4fe1\u606f\u7684\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5728Realm\u4e2d\u4f1a\u76f4\u63a5\u4ece\u6211\u4eec\u7684\u6570\u636e\u6e90\u4e2d\u83b7\u53d6Shiro\u9700\u8981\u7684\u9a8c\u8bc1\u4fe1\u606f\u3002\u53ef\u4ee5\u8bf4\uff0cRealm\u662f\u4e13\u7528\u4e8e\u5b89\u5168\u6846\u67b6\u7684DAO.<br \/>\n\tShiro\u7684\u8ba4\u8bc1\u8fc7\u7a0b\u6700\u7ec8\u4f1a\u4ea4\u7531Realm\u6267\u884c\uff0c\u8fd9\u65f6\u4f1a\u8c03\u7528Realm\u7684getAuthenticationInfo(token)\u65b9\u6cd5\u3002<br \/>\n\t\u8be5\u65b9\u6cd5\u4e3b\u8981\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c:\n<\/p>\n<ul>\n<li>\n\t\t\u68c0\u67e5\u63d0\u4ea4\u7684\u8fdb\u884c\u8ba4\u8bc1\u7684\u4ee4\u724c\u4fe1\u606f\n\t<\/li>\n<li>\n\t\t\u6839\u636e\u4ee4\u724c\u4fe1\u606f\u4ece\u6570\u636e\u6e90(\u901a\u5e38\u4e3a\u6570\u636e\u5e93)\u4e2d\u83b7\u53d6\u7528\u6237\u4fe1\u606f\n\t<\/li>\n<li>\n\t\t\u5bf9\u7528\u6237\u4fe1\u606f\u8fdb\u884c\u5339\u914d\u9a8c\u8bc1\u3002\n\t<\/li>\n<li>\n\t\t\u9a8c\u8bc1\u901a\u8fc7\u5c06\u8fd4\u56de\u4e00\u4e2a\u5c01\u88c5\u4e86\u7528\u6237\u4fe1\u606f\u7684AuthenticationInfo\u5b9e\u4f8b\u3002\n\t<\/li>\n<li>\n\t\t\u9a8c\u8bc1\u5931\u8d25\u5219\u629b\u51faAuthenticationException\u5f02\u5e38\u4fe1\u606f\u3002\u800c\u5728\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8981\u505a\u7684\u5c31\u662f\u81ea\u5b9a\u4e49\u4e00\u4e2aRealm\u7c7b\uff0c\u7ee7\u627fAuthorizingRealm\u62bd\u8c61\u7c7b\uff0c\u91cd\u8f7ddoGetAuthenticationInfo()\uff0c\u91cd\u5199\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u7684\u65b9\u6cd5\u3002\n\t<\/li>\n<\/ul>\n<h2>\n\tdoGetAuthenticationInfo\u7684\u91cd\u5199<br \/>\n<\/h2>\n<pre class=\"brush:java;\">\r\n\/\/\u8ba4\u8bc1\u4fe1\u606f.(\u8eab\u4efd\u9a8c\u8bc1) : Authentication \u662f\u7528\u6765\u9a8c\u8bc1\u7528\u6237\u8eab\u4efd\r\n@Override\r\nprotected AuthenticationInfo doGetAuthenticationInfo(\r\n\t\tAuthenticationToken authcToken) throws AuthenticationException {\r\n\tSystem.out.println(&quot;\u8eab\u4efd\u8ba4\u8bc1\u65b9\u6cd5\uff1aMyShiroRealm.doGetAuthenticationInfo()&quot;);\r\n\tShiroToken token = (ShiroToken) authcToken;\r\n\tMap&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();\r\n\tmap.put(&quot;nickname&quot;, token.getUsername());\r\n\tmap.put(&quot;pswd&quot;, token.getPswd());\r\n\tSysUser user = null;\r\n\t\/\/ \u4ece\u6570\u636e\u5e93\u83b7\u53d6\u5bf9\u5e94\u7528\u6237\u540d\u5bc6\u7801\u7684\u7528\u6237\r\n\tList&lt;SysUser&gt; userList = sysUserService.selectByMap(map);\r\n\tif(userList.size()!=0){\r\n\t\tuser = userList.get(0);\r\n\t}\r\n\tif (null == user) {\r\n\t\tthrow new AccountException(&quot;\u5e10\u53f7\u6216\u5bc6\u7801\u4e0d\u6b63\u786e\uff01&quot;);\r\n\t}else if(user.getStatus()==0){\r\n\t\t\/**\r\n\t\t * \u5982\u679c\u7528\u6237\u7684status\u4e3a\u7981\u7528\u3002\u90a3\u4e48\u5c31\u629b\u51fa&lt;code&gt;DisabledAccountException&lt;\/code&gt;\r\n\t\t *\/\r\n\t\tthrow new DisabledAccountException(&quot;\u5e10\u53f7\u5df2\u7ecf\u7981\u6b62\u767b\u5f55\uff01&quot;);\r\n\t}else{\r\n\t\t\/\/\u66f4\u65b0\u767b\u5f55\u65f6\u95f4 last login time\r\n\t\tuser.setLastLoginTime(new Date());\r\n\t\tsysUserService.updateById(user);\r\n\t}\r\n\treturn new SimpleAuthenticationInfo(user, user.getPswd(), getName());\r\n}<\/pre>\n<h2>\n\t\u94fe\u63a5\u6743\u9650\u7684\u5b9e\u73b0<br \/>\n<\/h2>\n<blockquote>\n<p>\n\t\tshiro\u7684\u6743\u9650\u6388\u6743\u662f\u901a\u8fc7\u7ee7\u627fAuthorizingRealm\u62bd\u8c61\u7c7b\uff0c\u91cd\u8f7ddoGetAuthorizationInfo();<br \/>\n\t\t\u5f53\u8bbf\u95ee\u5230\u9875\u9762\u7684\u65f6\u5019\uff0c\u94fe\u63a5\u914d\u7f6e\u4e86\u76f8\u5e94\u7684\u6743\u9650\u6216\u8005shiro\u6807\u7b7e\u624d\u4f1a\u6267\u884c\u6b64\u65b9\u6cd5\u5426\u5219\u4e0d\u4f1a\u6267\u884c\uff0c\u6240\u4ee5\u5982\u679c\u53ea\u662f\u7b80\u5355\u7684\u8eab\u4efd\u8ba4\u8bc1\u6ca1\u6709\u6743\u9650\u7684\u63a7\u5236\u7684\u8bdd\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u4e0d\u8fdb\u884c\u5b9e\u73b0\uff0c\u76f4\u63a5\u8fd4\u56denull\u5373\u53ef\u3002<br \/>\n\t\t\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u4e3b\u8981\u662f\u4f7f\u7528\u7c7b\uff1aSimpleAuthorizationInfo<br \/>\n\t\t\u8fdb\u884c\u89d2\u8272\u7684\u6dfb\u52a0\u548c\u6743\u9650\u7684\u6dfb\u52a0\u3002<br \/>\n\t\tauthorizationInfo.addRole(role.getRole());<br \/>\n\t\tauthorizationInfo.addStringPermission(p.getPermission());<br \/>\n\t\t\u5f53\u7136\u4e5f\u53ef\u4ee5\u6dfb\u52a0set\u96c6\u5408\uff1aroles\u662f\u4ece\u6570\u636e\u5e93\u67e5\u8be2\u7684\u5f53\u524d\u7528\u6237\u7684\u89d2\u8272\uff0cstringPermissions\u662f\u4ece\u6570\u636e\u5e93\u67e5\u8be2\u7684\u5f53\u524d\u7528\u6237\u5bf9\u5e94\u7684\u6743\u9650<br \/>\n\t\tauthorizationInfo.setRoles(roles);<br \/>\n\t\tauthorizationInfo.setStringPermissions(stringPermissions);<br \/>\n\t\t\u5c31\u662f\u8bf4\u5982\u679c\u5728shiro\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e86filterChainDefinitionMap.put(&ldquo;\/add&rdquo;, &ldquo;perms[\u6743\u9650\u6dfb\u52a0]&rdquo;);<br \/>\n\t\t\u5c31\u8bf4\u660e\u8bbf\u95ee\/add\u8fd9\u4e2a\u94fe\u63a5\u5fc5\u987b\u8981\u6709&ldquo;\u6743\u9650\u6dfb\u52a0&rdquo;\u8fd9\u4e2a\u6743\u9650\u624d\u53ef\u4ee5\u8bbf\u95ee\uff0c<br \/>\n\t\t\u5982\u679c\u5728shiro\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e86filterChainDefinitionMap.put(&ldquo;\/add&rdquo;, &ldquo;roles[100002]\uff0cperms[\u6743\u9650\u6dfb\u52a0]&rdquo;);<br \/>\n\t\t\u5c31\u8bf4\u660e\u8bbf\u95ee\/add\u8fd9\u4e2a\u94fe\u63a5\u5fc5\u987b\u8981\u6709&ldquo;\u6743\u9650\u6dfb\u52a0&rdquo;\u8fd9\u4e2a\u6743\u9650\u548c\u5177\u6709&ldquo;100002&rdquo;\u8fd9\u4e2a\u89d2\u8272\u624d\u53ef\u4ee5\u8bbf\u95ee\u3002\n\t<\/p>\n<\/blockquote>\n<pre class=\"brush:java;\">\r\n\/\/\u6388\u6743\r\n@Override\r\nprotected AuthorizationInfo doGetAuthorizationInfo(\r\n\t\tPrincipalCollection principals) {\r\n\tSystem.out.println(&quot;\u6743\u9650\u8ba4\u8bc1\u65b9\u6cd5\uff1aMyShiroRealm.doGetAuthenticationInfo()&quot;);\r\n\tSysUser token = (SysUser)SecurityUtils.getSubject().getPrincipal();\r\n\tString userId = token.getId();\r\n\tSimpleAuthorizationInfo info =  new SimpleAuthorizationInfo();\r\n\t\/\/\u6839\u636e\u7528\u6237ID\u67e5\u8be2\u89d2\u8272\uff08role\uff09\uff0c\u653e\u5165\u5230Authorization\u91cc\u3002\r\n\t\/*Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();\r\n\tmap.put(&quot;user_id&quot;, userId);\r\n\tList&lt;SysRole&gt; roleList = sysRoleService.selectByMap(map);\r\n\tSet&lt;String&gt; roleSet = new HashSet&lt;String&gt;();\r\n\tfor(SysRole role : roleList){\r\n\t\troleSet.add(role.getType());\r\n\t}*\/\r\n\t\/\/\u5b9e\u9645\u5f00\u53d1\uff0c\u5f53\u524d\u767b\u5f55\u7528\u6237\u7684\u89d2\u8272\u548c\u6743\u9650\u4fe1\u606f\u662f\u4ece\u6570\u636e\u5e93\u6765\u83b7\u53d6\u7684\uff0c\u6211\u8fd9\u91cc\u5199\u6b7b\u662f\u4e3a\u4e86\u65b9\u4fbf\u6d4b\u8bd5\r\n\tSet&lt;String&gt; roleSet = new HashSet&lt;String&gt;();\r\n\troleSet.add(&quot;100002&quot;);\r\n\tinfo.setRoles(roleSet);\r\n\t\/\/\u6839\u636e\u7528\u6237ID\u67e5\u8be2\u6743\u9650\uff08permission\uff09\uff0c\u653e\u5165\u5230Authorization\u91cc\u3002\r\n\t\/*List&lt;SysPermission&gt; permissionList = sysPermissionService.selectByMap(map);\r\n\tSet&lt;String&gt; permissionSet = new HashSet&lt;String&gt;();\r\n\tfor(SysPermission Permission : permissionList){\r\n\t\tpermissionSet.add(Permission.getName());\r\n\t}*\/\r\n\tSet&lt;String&gt; permissionSet = new HashSet&lt;String&gt;();\r\n\tpermissionSet.add(&quot;\u6743\u9650\u6dfb\u52a0&quot;);\r\n\tinfo.setStringPermissions(permissionSet);\r\n       return info;\r\n}<\/pre>\n<h2>\n\t\u767b\u5f55 Controller \u4ee3\u7801<br \/>\n<\/h2>\n<pre class=\"brush:java;\">\r\n\/\/\u8df3\u8f6c\u5230\u767b\u5f55\u8868\u5355\u9875\u9762\r\n@RequestMapping(value=&quot;login&quot;)\r\npublic String login() {\r\n\treturn &quot;login&quot;;\r\n}\r\n\/\/ajax\u767b\u5f55\u8bf7\u6c42\r\n@RequestMapping(value=&quot;ajaxLogin&quot;,method=RequestMethod.POST)\r\n@ResponseBody\r\npublic Map&lt;String,Object&gt; submitLogin(String username, String password,Model model) {\r\n\tMap&lt;String, Object&gt; resultMap = new LinkedHashMap&lt;String, Object&gt;();\r\n\ttry {\r\n\t\t\r\n\t\tShiroToken token = new ShiroToken(username, password);\r\n\t\tSecurityUtils.getSubject().login(token);\r\n\t\tresultMap.put(&quot;status&quot;, 200);\r\n\t\tresultMap.put(&quot;message&quot;, &quot;\u767b\u5f55\u6210\u529f&quot;);\r\n\t} catch (Exception e) {\r\n\t\tresultMap.put(&quot;status&quot;, 500);\r\n\t\tresultMap.put(&quot;message&quot;, e.getMessage());\r\n\t}\r\n\treturn resultMap;\r\n}<\/pre>\n<h2>\n\t\u767b\u5f55\u9875\u9762<br \/>\n<\/h2>\n<pre class=\"brush:xml;\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=utf-8&quot;&gt;\r\n&lt;script type=&quot;text\/javascript&quot;\r\n\tsrc=&quot;&lt;%=basePath%&gt;\/static\/js\/jquery-1.11.3.js&quot;&gt;&lt;\/script&gt;\r\n&lt;title&gt;\u767b\u5f55&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\t\u9519\u8bef\u4fe1\u606f\uff1a\r\n\t&lt;h4 id=&quot;erro&quot;&gt;&lt;\/h4&gt;\r\n\t&lt;form&gt;\r\n\t\t&lt;p&gt;\r\n\t\t\t\u8d26\u53f7\uff1a&lt;input type=&quot;text&quot; name=&quot;username&quot; id=&quot;username&quot; value=&quot;admin&quot; \/&gt;\r\n\t\t&lt;\/p&gt;\r\n\t\t&lt;p&gt;\r\n\t\t\t\u5bc6\u7801\uff1a&lt;input type=&quot;text&quot; name=&quot;password&quot; id=&quot;password&quot; value=&quot;123&quot; \/&gt;\r\n\t\t&lt;\/p&gt;\r\n\t\t&lt;p&gt;\r\n\t\t\t&lt;input type=&quot;button&quot; id=&quot;ajaxLogin&quot; value=&quot;\u767b\u5f55&quot; \/&gt;\r\n\t\t&lt;\/p&gt;\r\n\t&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n&lt;script&gt;\r\n\tvar username = $(&quot;#username&quot;).val();\r\n\tvar password = $(&quot;#password&quot;).val();\r\n\t$(&quot;#ajaxLogin&quot;).click(function() {\r\n\t\t$.post(&quot;\/ajaxLogin&quot;, {\r\n\t\t\t&quot;username&quot; : username,\r\n\t\t\t&quot;password&quot; : password\r\n\t\t}, function(result) {\r\n\t\t\tif (result.status == 200) {\r\n\t\t\t\tlocation.href = &quot;\/index&quot;;\r\n\t\t\t} else {\r\n\t\t\t\t$(&quot;#erro&quot;).html(result.message);\r\n\t\t\t}\r\n\t\t});\r\n\t});\r\n&lt;\/script&gt;\r\n&lt;\/html&gt;<\/pre>\n<h2>\n\tShiro \u603b\u7ed3<br \/>\n<\/h2>\n<p>\n\t\u8fd9\u91cc\u7531\u4e8e\u7bc7\u5e45\u539f\u56e0\uff0c\u8fd9\u91cc\u53ea\u662f\u622a\u53d6\u4e86\u90e8\u5206\u4ee3\u7801\uff0c\u8fd8\u6709\u51e0\u4e2a\u9875\u9762\u548c\u52a8\u6001\u6743\u9650\u63a7\u5236\u5206\u914d\u7684\u4ee3\u7801\u672a\u6574\u7406\u51fa\u6765\uff0c\u5927\u5bb6\u53ef\u4ee5\u5230\u8fd9\u91cc\u94fe\u63a5\uff1a<a href=\"http:\/\/pan.baidu.com\/s\/1dFKJKrf\" target=\"_blank\">http:\/\/pan.baidu.com\/s\/1dFKJKrf<\/a> \u5bc6\u7801\uff1a4cqj\u4e0b\u8f7d\u6e90\u4ee3\u7801\u3002\n<\/p>\n<p>\n\t\u5f53\u7136shiro\u5f88\u5f3a\u5927\uff0c\u8fd9\u4ec5\u4ec5\u662f\u5b8c\u6210\u4e86\u767b\u5f55\u8ba4\u8bc1\u548c\u6743\u9650\u7ba1\u7406\u8fd9\u4e24\u4e2a\u529f\u80fd\uff0c\u63a5\u4e0b\u6765\u6211\u4f1a\u7ee7\u7eed\u5b66\u4e60\u548c\u5206\u4eab\uff0c\u8bf4\u8bf4\u63a5\u4e0b\u6765\u7684\u5b66\u4e60\u8def\u7ebf\u5427\uff1a\n<\/p>\n<ul>\n<li>\n\t\tshiro+redis\u96c6\u6210\uff0c\u907f\u514d\u6bcf\u6b21\u8bbf\u95ee\u6709\u6743\u9650\u7684\u94fe\u63a5\u90fd\u4f1a\u53bb\u6267\u884cMyShiroRealm.doGetAuthenticationInfo()\u65b9\u6cd5\u6765\u67e5\u8be2\u5f53\u524d\u7528\u6237\u7684\u6743\u9650\uff0c\u56e0\u4e3a\u5b9e\u9645\u60c5\u51b5\u4e2d\u6743\u9650\u662f\u4e0d\u4f1a\u7ecf\u5e38\u53d8\u5f97\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4f7f\u7528redis\u8fdb\u884c\u6743\u9650\u7684\u7f13\u5b58\u3002\n\t<\/li>\n<li>\n\t\t\u5b9e\u73b0shiro\u94fe\u63a5\u6743\u9650\u7684\u52a8\u6001\u52a0\u8f7d\uff0c\u4e4b\u524d\u8981\u6dfb\u52a0\u4e00\u4e2a\u94fe\u63a5\u7684\u6743\u9650\uff0c\u8981\u5728shiro\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0filterChainDefinitionMap.put(&ldquo;\/add&rdquo;, &ldquo;roles[100002]\uff0cperms[\u6743\u9650\u6dfb\u52a0]&rdquo;)\uff0c\u8fd9\u6837\u5f88\u4e0d\u65b9\u4fbf\u7ba1\u7406\uff0c\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\n\t<\/li>\n<li>\n\t\t\u63a5\u7684\u6743\u9650\u4f7f\u7528\u6570\u636e\u5e93\u8fdb\u884c\u52a0\u8f7d\uff0c\u53e6\u4e00\u79cd\u662f\u901a\u8fc7init\u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u5f0f\u8bfb\u53d6\u3002\n\t<\/li>\n<li>\n\t\tShiro \u81ea\u5b9a\u4e49\u6743\u9650\u6821\u9a8cFilter\u5b9a\u4e49\uff0c\u53ca\u529f\u80fd\u5b9e\u73b0\u3002\n\t<\/li>\n<li>\n\t\tShiro Ajax\u8bf7\u6c42\u6743\u9650\u4e0d\u6ee1\u8db3\uff0c\u62e6\u622a\u540e\u89e3\u51b3\u65b9\u6848\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u524d\u63d0\uff0c\u6211\u4eec\u77e5\u9053Ajax\u4e0d\u80fd\u505a\u9875\u9762redirect\u548cforward\u8df3\u8f6c\uff0c\u6240\u4ee5Ajax\u8bf7\u6c42\u5047\u5982\u6ca1\u767b\u5f55\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8bf7\u6c42\u7ed9\u7528\u6237\u7684\u611f\u89c9\u5c31\u662f\u6ca1\u6709\u4efb\u4f55\u53cd\u5e94\uff0c\u800c\u7528\u6237\u53c8\u4e0d\u77e5\u9053\u7528\u6237\u5df2\u7ecf\u9000\u51fa\u4e86\u3002\n\t<\/li>\n<li>\n\t\tShiro JSP\u6807\u7b7e\u4f7f\u7528\u3002\n\t<\/li>\n<li>\n\t\tShiro \u767b\u5f55\u540e\u8df3\u8f6c\u5230\u6700\u540e\u4e00\u4e2a\u8bbf\u95ee\u7684\u9875\u9762\n\t<\/li>\n<li>\n\t\t\u5728\u7ebf\u663e\u793a\uff0c\u5728\u7ebf\u7528\u6237\u7ba1\u7406\uff08\u8e22\u51fa\u767b\u5f55\uff09\u3002\n\t<\/li>\n<li>\n\t\t\u767b\u5f55\u6ce8\u518c\u5bc6\u7801\u52a0\u5bc6\u4f20\u8f93\u3002\n\t<\/li>\n<li>\n\t\t\u96c6\u6210\u9a8c\u8bc1\u7801\u3002\n\t<\/li>\n<li>\n\t\t\u8bb0\u4f4f\u6211\u7684\u529f\u80fd\u3002\u5173\u95ed\u6d4f\u89c8\u5668\u540e\u8fd8\u662f\u767b\u5f55\u72b6\u6001\u3002\n\t<\/li>\n<li>\n\t\t\u8fd8\u6709\u6ca1\u6709\u60f3\u5230\u7684\u540e\u9762\u518d\u8bf4\uff0c\u6b22\u8fce\u5927\u5bb6\u63d0\u51fa\u4e00\u4e9b\u5efa\u8bae\u3002\n\t<\/li>\n<\/ul>\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=1095\">Spring \u6574\u5408 Shiro \u5b9e\u73b0\u767b\u5f55\u8ba4\u8bc1\u548c\u6743\u9650\u63a7\u5236<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u524d\u9762\u6709\u4e00\u7bc7\u6587\u7ae0\u5199\u5230\u4e86 Shiro \u8ba4\u8bc1\u7684\u76f8\u5173\u6570\u636e\u5e93\u8bbe\u8ba1\u300a\u5f00\u6e90\u6743\u9650\u6846\u67b6 Shiro \u6574\u5408 web \u9879\u76ee\u7684\u6570\u636e\u5e93\u8bbe [&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,551,829,830,828],"class_list":["post-1095","post","type-post","status-publish","format-standard","hentry","category-java","tag-shiro","tag-spring","tag-829","tag-830","tag-828"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1095","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=1095"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1095\/revisions"}],"predecessor-version":[{"id":1998,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1095\/revisions\/1998"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}