{"id":302317,"date":"2021-01-17T18:42:28","date_gmt":"2021-01-17T10:42:28","guid":{"rendered":"http:\/\/4563.org\/?p=302317"},"modified":"2021-01-17T18:42:28","modified_gmt":"2021-01-17T10:42:28","slug":"springsecurity-%e5%89%8d%e5%8f%b0%e5%92%8c%e5%90%8e%e5%8f%b0%e7%94%a8%e6%88%b7%e4%b8%8d%e5%90%8c%e8%a1%a8%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=302317","title":{"rendered":"SpringSecurity \u524d\u53f0\u548c\u540e\u53f0\u7528\u6237\u4e0d\u540c\u8868\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  SpringSecurity \u524d\u53f0\u548c\u540e\u53f0\u7528\u6237\u4e0d\u540c\u8868\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : totoro52 <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6700\u8fd1\u5728\u5199\u4e00\u4e2a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5c0f\u9879\u76ee\uff0c\u5b89\u5168\u6846\u67b6\u91c7\u7528\u4e86 security\uff0c\u4f46\u7cfb\u7edf\u9700\u8981\u533a\u5206\u524d\u53f0\u7528\u6237\u548c\u540e\u53f0\u7528\u6237<\/p>\n<p>\u8d77\u521d\u6211\u7684\u60f3\u6cd5\u662f\u5199\u4e24\u4e2a provider\uff0c\u4e24\u4e2a userDetailsService \u5206\u522b\u5904\u7406\u524d\u53f0\u7528\u6237\u548c\u540e\u53f0\u7528\u6237 \u5e76\u91c7\u7528 jwt \u65b9\u5f0f\u6821\u9a8c<\/p>\n<p>\u5b9e\u73b0\u7684\u57fa\u672c\u7684\u767b\u5f55\u6d41\u7a0b\u540e\uff0c\u6211\u53d1\u73b0\u540e\u9762\u8d8a\u6765\u8d8a\u96be\u5199\u4e0b\u53bb\uff0c\u56e0\u4e3a\u540e\u53f0\u7528\u6237\u548c\u524d\u53f0\u7528\u6237\u662f\u4e24\u4e2a\u8868\u7684\uff0c\u6743\u9650\u4e5f\u5b8c\u5168\u4e0d\u540c\uff0c\u5f88\u96be\u878d\u5408\u5230\u4e00\u8d77 \u6709\u6ca1\u6709\u5927\u725b\u597d\u70b9\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u6216\u8005\u62c6\u5f00\u6210\u4e24\u4e2a\u9879\u76ee\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5072618\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : echo1937 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5565\u53eb\u505a\u533a\u5206\u524d\u53f0\u548c\u540e\u53f0\u7528\u6237\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5072619\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : totoro52 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @echo1937 \u6211\u524d\u53f0\u7528\u6237\u4e5f\u9700\u8981\u505a\u6743\u9650\u63a7\u5236\uff0c\u540e\u53f0\u4e5f\u9700\u8981\uff0c\u4f46\u662f\u4e24\u8005\u90fd\u662f\u4e0d\u540c\u7684\u8868\uff0c\u6743\u9650\u4e5f\u4e0d\u4e00\u6837                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5072620\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : soulzz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u90a3\u5c31\u518d\u5efa\u4e00\u5f20\u8868\uff0c\u6807\u8bb0\u7528\u6237\u662f\u524d\u53f0\u8fd8\u662f\u540e\u53f0\u540e\u6237                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5072621\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : notejava <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u91cc\u5e94\u8be5\u662f\u4e24\u4e2a\u9879\u76ee\uff0c\u4e24\u5957\u6743\u9650\u7ba1\u7406\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5072622\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : echo1937 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @totoro52 #2 \u7528\u6237\u662f\u7528\u6237\uff08 User \uff09\uff0c\u6743\u9650\u662f\u6743\u9650\uff08 Role \uff09\uff0c<\/p>\n<p>\u524d\u7aef\u9700\u8981\u6743\u9650\u63a7\u5236\uff0c\u90a3\u4e48\u5c31\u4f1a\u591a\u4e00\u5f20\u6743\u9650\u8868\uff0c\u4e3a\u5565\u662f\u641e 2 \u5957\u7528\u6237\uff0c<\/p>\n<p>\u6211\u8fd8\u662f\u4e0d\u660e\u767d\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5072623\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ourslay <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8d34\u4e0b\u4e4b\u524d\u56de\u590d\u7684\u3002<br \/>\u81ea\u5b9a\u4e49 Filter \u7ee7\u627f UsernamePasswordAuthenticationFilter\uff0c\u5206\u522b\u5b9e\u73b0\u5bf9 user\/admin \u7684 AuthenticationManager<\/p>\n<p>configure \u914d\u7f6e<br \/>&#8220;`<br \/> .addFilter(new MultipleAuthenticationFilter(adminAuthenticationManager(), &#8220;\/admin\/login&#8221;))<br \/> .addFilter(new MultipleAuthenticationFilter(userAuthenticationManager(), &#8220;\/user\/login&#8221;));<\/p>\n<p>&#8220;`<br \/>AuthenticationManager \u914d\u7f6e<br \/>&#8220;`<br \/>List&lt;AuthenticationProvider&gt; providers = new ArrayList&lt;&gt;();<br \/> DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();<br \/> daoAuthenticationProvider.setUserDetailsService(adminDetailsService()); \/\/ or userDetailsService<br \/> providers.add(daoAuthenticationProvider);<br \/> return new ProviderManager(providers);<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5072624\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : coder1v5 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             jwt \u4e2d\u5c06\u7528\u6237\u5173\u952e\u4fe1\u606f userid, userType \u52a0\u5165\u8fdb\u53bb\uff0c\u5199\u4e00\u4e2a jwtProvider\uff0c\u5728 jwtProvider \u4e2d\u89e3\u6790\u51fa\u6765 token\uff0ctoken \u672c\u8eab\u62bd\u8c61\u51fa\u6765\u4e5f\u662f\u8ba4\u8bc1\u4fe1\u606f\uff08\u7528\u6237\u540d\u548c\u5bc6\u7801\uff09\uff0c\u8fd9\u4e2a Provider \u5f53\u505a\u767b\u5f55\u8ba4\u8bc1 Provider \u53bb\u5199\u3002Filter \u505a\u914d\u7f6e\u8f6c\u53d1\u7ed9 Provider \u3002\u7136\u540e\u6839\u636e token \u4e2d\u89e3\u6790\u51fa\u6765\u7684 userId,userType, \u8c03\u7528\u4e0d\u540c\u7684 service \u53bb\u83b7\u53d6\u5bf9\u5e94\u7684\u6743\u9650.\u7136\u540e\u653e\u5165 SecurityContext \u4e2d\u5c31\u53ef\u4ee5\u4e86<br \/>if(Strings.equals(userType,\u540e\u53f0\u7528\u6237){<br \/> \/\/\u8c03\u7528\u540e\u53f0\u7528\u6237\u7684\u670d\u52a1\u7c7b\uff0c\u6dfb\u52a0\u7528\u6237\u4fe1\u606f\uff0c\u6743\u9650\u4fe1\u606f\uff0c\u653e\u5165 security \u4e0a\u4e0b\u6587\u4e2d<br \/> SecurityContextHolder.getContext().setAuthentication(authToken);<\/p>\n<p>}else{<br \/> \/\/\u8c03\u7528\u524d\u53f0\u7528\u6237\u7684\u670d\u52a1\u7c7b\uff0c\u6dfb\u52a0\u7528\u6237\u4fe1\u606f\uff0c\u6743\u9650\u4fe1\u606f\uff0c\u653e\u5165 security \u4e0a\u4e0b\u6587\u4e2d<br \/> SecurityContextHolder.getContext().setAuthentication(authToken);<br \/>}<\/p>\n<p>spring security \u5bf9\u4e8e\u8fd9\u79cd\u573a\u666f\u63d0\u4f9b\u4e86\u8db3\u591f\u7684\u652f\u6301\uff0c\u522b\u8bf4\u4e24\u79cd\u7528\u6237\u7c7b\u578b\u4e86\uff0c\u591a\u5c11\u79cd\u767b\u5f55\u7528\u6237\uff0c\u53ca\u591a\u79cd\u8ba4\u8bc1\u65b9\u5f0f\u90fd\u53ef\u4ee5\u8f7b\u677e\u6269\u5c55\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5072625\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : vjnjc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f30\u8ba1\u662f\u7ba1\u7406\u540e\u53f0\u7528\u6237\u548c\u8f6f\u4ef6\u6700\u7ec8\u7528\u6237\u4e24\u5957\u5427<\/p>\n<p>\u5176\u5b9e security \u6388\u6743\u8ba4\u8bc1\u53ef\u4ee5\u7528 jwt token \u89e3\u6790\u51fa\u6765 uid+userType\uff0c\u8ba4\u8bc1\u5b8c\u4e86\u4ee5\u540e\u6254\u4e00\u4e2a\u7528\u6237 object \u5728 context \u91cc\u9762\uff0c\u6ca1\u6709\u9650\u5b9a\u4e00\u5b9a\u8981\u540c\u4e00\u4e2a\u7528\u6237\u7c7b\u578b                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5072626\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nvkou <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u5e94\u8be5\u7406\u89e3\u4e3a\u4e0d\u540c\u6743\u9650\u5417\uff1f\u5206\u8868\u6709\u4ec0\u4e48\u610f\u4e49\uff1f<br \/>\u540e\u53f0\u5165\u53e3\u4e0d\u9274\u6743\u7684\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5072627\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : qinxi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7cfb\u7edf\u5206\u79bb\u5c31\u5b8c\u4e86&#8230;\u5404\u662f\u5404\u7684\u7528\u6237\u7cfb\u7edf. \u66f4\u65b0\u529f\u80fd\u4e92\u4e0d\u5f71\u54cd                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>SpringSecurity \u524d\u53f0&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/302317"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=302317"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/302317\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=302317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=302317"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=302317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}