{"id":232,"date":"2022-03-31T09:31:24","date_gmt":"2022-03-31T01:31:24","guid":{"rendered":"https:\/\/blog.lijiajia.top\/?p=232"},"modified":"2026-06-19T21:29:52","modified_gmt":"2026-06-19T13:29:52","slug":"springsecurity%e6%a1%86%e6%9e%b6%e5%ad%a6%e4%b9%a0%ef%bc%9a%e7%94%a8%e6%88%b7%e8%ae%a4%e8%af%81-%e8%87%aa%e5%ae%9a%e4%b9%89%e7%99%bb%e5%bd%95%e9%a1%b5%e9%9d%a2%e4%b8%83","status":"publish","type":"post","link":"https:\/\/blog.ttwow.top\/?p=232","title":{"rendered":"SpringSecurity\u6846\u67b6\u5b66\u4e60\uff1a\u7528\u6237\u8ba4\u8bc1 -\u81ea\u5b9a\u4e49\u767b\u5f55\u9875\u9762(\u4e03)"},"content":{"rendered":"<h2>\u914d\u7f6e\u7c7b<\/h2>\n<pre><code class=\"language-java\">@Configuration\npublic class SecurityConfigDiy extends WebSecurityConfigurerAdapter {\n\n    @Autowired\n    private UserDetailsService userDetailsService;\n\n    @Override\n    protected void configure(AuthenticationManagerBuilder auth) throws Exception{\n        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());\n    }\n\n    @Bean\n    PasswordEncoder passwordEncoder(){\n        return new BCryptPasswordEncoder();\n    }\n\n    @Override\n    protected void configure(HttpSecurity httpSecurity) throws Exception{\n\n        httpSecurity.formLogin()    \/\/\u81ea\u5b9a\u4e49\u81ea\u5df1\u7f16\u5199\u7684\u767b\u5f55\u9875\u9762\n                .loginPage(&quot;\/login.html&quot;)    \/\/\u767b\u5f55\u9875\u9762\u8bbe\u7f6e\n                .loginProcessingUrl(&quot;\/user\/login&quot;)  \/\/\u767b\u5f55\u8bbf\u95ee\u8def\u5f84 \u6309\u767b\u5f55\u6309\u94ae\u540e\u8df3\u8f6c\u8def\u5f84\n                .defaultSuccessUrl(&quot;\/test\/index&quot;).permitAll()   \/\/\u767b\u5f55\u6210\u529f\u4e4b\u540e,\u8df3\u8f6c\u8def\u5f84\n                .and().authorizeRequests()\n                .antMatchers(&quot;\/&quot;,&quot;\/test\/hello&quot;,&quot;\/user\/login&quot;).permitAll()   \/\/\u8bbe\u7f6e\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7684\u8def\u5f84\n                .anyRequest().authenticated()\n                .and().csrf().disable();    \/\/\u5173\u95edcsrf\u9632\u62a4\n\n    }\n}<\/code><\/pre>\n<h2>\u9875\u9762<\/h2>\n<p>\u8def\u5f84\uff1a\/resources\/static\/login.html<br \/>\n\u5f3a\u5236\u8981\u6c42name\u540d\u79f0\uff1a<br \/>\nname=&quot;username&quot;<br \/>\nname=&quot;password&quot;<\/p>\n<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;\u767b\u5f55&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n        &lt;form action=&quot;\/user\/login&quot; method=&quot;post&quot; &gt;\n            \u7528\u6237\u540d\uff1a&lt;input type=&quot;text&quot; name=&quot;username&quot;&gt;\n            &lt;br\/&gt;\n            \u5bc6\u7801\uff1a&lt;input type=&quot;text&quot; name=&quot;password&quot;&gt;\n            &lt;br\/&gt;\n            &lt;input type=&quot;submit&quot; value=&quot;\u767b\u5f55&quot;&gt;\n        &lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/bucket-blog-img.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2022\/03\/image-1648693740569.png'><img class=\"lazyload lazyload-style-11\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/bucket-blog-img.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2022\/03\/image-1648693740569.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"file\" \/><\/div><\/p>\n<h2>\u63a7\u5236\u5c42<\/h2>\n<pre><code class=\"language-java\">@RestController\n@RequestMapping(&quot;\/test&quot;)\npublic class TestController {\n\n    @GetMapping(&quot;\/hello&quot;)\n    public String hello(){\n        return &quot;hello security&quot;;\n    }\n\n    @GetMapping(&quot;\/index&quot;)\n    public String index(){\n        return &quot;hello index&quot;;\n    }\n}<\/code><\/pre>\n<p>\u901a\u8fc7\u9875\u9762post\u4f20\u9012\u8fc7\u6765\u7684\u7528\u6237\u540d\u5bc6\u7801<br \/>\n\u548c\u6839\u636e\u7528\u6237\u540d\u67e5\u8be2\u51fa\u6765\u7684\u7528\u6237\u540d\u5bc6\u7801 \u6bd4\u8f83<\/p>\n<h2>\u6e90\u7801\u6bd4\u8f83\u5bc6\u7801\u7684\u5730\u65b9<\/h2>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/bucket-blog-img.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2022\/03\/image-1648708275246.png'><img class=\"lazyload lazyload-style-11\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/bucket-blog-img.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2022\/03\/image-1648708275246.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u914d\u7f6e\u7c7b @Configuration public class SecurityConfigDiy exten [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[125],"tags":[72],"class_list":["post-232","post","type-post","status-publish","format-standard","hentry","category-java-backend","tag-springsecurity"],"_links":{"self":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=232"}],"version-history":[{"count":1,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":2097,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions\/2097"}],"wp:attachment":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}