{"id":8208,"date":"2023-11-28T20:18:51","date_gmt":"2023-11-28T12:18:51","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8208"},"modified":"2023-12-06T10:08:39","modified_gmt":"2023-12-06T02:08:39","slug":"springboot-zhong-shen-me-shicsrf-gong-ji","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhong-shen-me-shicsrf-gong-ji\/","title":{"rendered":"SpringBoot\u4e2d\u4ec0\u4e48\u662fCSRF\u653b\u51fb\uff1f"},"content":{"rendered":"<p>CSRF\uff08Cross-Site Request Forgery\uff0c\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff09\u662f\u4e00\u79cd\u7f51\u7edc\u653b\u51fb\uff0c\u653b\u51fb\u8005\u901a\u8fc7\u6b3a\u9a97\u7528\u6237\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6267\u884c\u672a\u7ecf\u6388\u6743\u7684\u64cd\u4f5c\u3002\u653b\u51fb\u8005\u5229\u7528\u7528\u6237\u5728\u53d7\u4fe1\u4efb\u7f51\u7ad9\u4e2d\u5df2\u7ecf\u767b\u5f55\u7684\u51ed\u8bc1\uff0c\u901a\u8fc7\u4f2a\u9020\u8bf7\u6c42\uff0c\u4ee5\u7528\u6237\u7684\u8eab\u4efd\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\uff0c\u4f8b\u5982\u66f4\u6539\u5bc6\u7801\u3001\u53d1\u8d77\u8f6c\u8d26\u7b49\u3002<\/p>\n<p>CSRF \u653b\u51fb\u901a\u5e38\u5229\u7528\u4e86\u6d4f\u89c8\u5668\u5bf9\u4e8e\u540c\u4e00\u57df\u4e0b\u7684\u8bf7\u6c42\u9ed8\u8ba4\u643a\u5e26 Cookie \u7684\u7279\u6027\u3002\u653b\u51fb\u8005\u901a\u8fc7\u5728\u7b2c\u4e09\u65b9\u7f51\u7ad9\u4e0a\u690d\u5165\u6076\u610f\u4ee3\u7801\uff0c\u4f7f\u7528\u6237\u5728\u53d7\u4fe1\u4efb\u7ad9\u70b9\u4e0a\u6267\u884c\u64cd\u4f5c\u65f6\uff0c\u540c\u65f6\u53d1\u9001\u4f2a\u9020\u8bf7\u6c42\u5230\u76ee\u6807\u7ad9\u70b9\u3002<\/p>\n<p>Spring Security \u662f Spring \u6846\u67b6\u63d0\u4f9b\u7684\u5b89\u5168\u6846\u67b6\uff0c\u7528\u4e8e\u4fdd\u62a4 Spring \u5e94\u7528\u7a0b\u5e8f\u3002Spring Security \u63d0\u4f9b\u4e86\u9632\u5fa1 CSRF \u653b\u51fb\u7684\u673a\u5236\uff0c\u901a\u5e38\u901a\u8fc7\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u914d\u7f6e CSRF \u4fdd\u62a4\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u5728 Spring Security \u4e2d\u9632\u5fa1 CSRF \u653b\u51fb\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<strong>\u542f\u7528 CSRF \u4fdd\u62a4\uff1a<\/strong> \u5728 Spring Security \u914d\u7f6e\u4e2d\u542f\u7528 CSRF \u4fdd\u62a4\u3002<\/p>\n<pre><code class=\"language-java\">@EnableWebSecurity\npublic class WebSecurityConfig extends WebSecurityConfigurerAdapter {\n\n    @Override\n    protected void configure(HttpSecurity http) throws Exception {\n        http\n            .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())\n            .and()\n            \/\/ other security configurations...\n            .authorizeRequests()\n            .antMatchers(&quot;\/public\/**&quot;).permitAll()\n            .anyRequest().authenticated();\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u914d\u7f6e\u4f7f\u7528 <code>CookieCsrfTokenRepository<\/code> \u6765\u914d\u7f6e CSRF \u4fdd\u62a4\uff0c\u5e76\u8bbe\u7f6e <code>httpOnly<\/code> \u4e3a <code>false<\/code>\uff0c\u4ee5\u4fbf\u5728\u524d\u7aef\u5e94\u7528\u4e2d\u80fd\u591f\u83b7\u53d6 CSRF \u4ee4\u724c\u3002\n<\/li>\n<li>\n<strong>\u5728\u8868\u5355\u4e2d\u5305\u542b CSRF \u4ee4\u724c\uff1a<\/strong> \u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u8868\u5355\u4e2d\u5305\u542b CSRF \u4ee4\u724c\u3002\u5728 Thymeleaf \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;form th:action=&quot;@{\/some-action}&quot; method=&quot;post&quot;&gt;\n    &lt;input type=&quot;hidden&quot; th:name=&quot;<span class=\"katex math inline\">{_csrf.parameterName}&quot; th:value=&quot;<\/span>{_csrf.token}&quot;\/&gt;\n    &lt;!-- other form fields... --&gt;\n    &lt;button type=&quot;submit&quot;&gt;Submit&lt;\/button&gt;\n&lt;\/form&gt;\n<\/code><\/pre>\n<p>\u5728 JSP \u4e2d\uff1a<\/p>\n<pre><code class=\"language-jsp\">&lt;form action=&quot;\/some-action&quot; method=&quot;post&quot;&gt;\n    &lt;input type=&quot;hidden&quot; name=&quot;<span class=\"katex math inline\">{_csrf.parameterName}&quot; value=&quot;<\/span>{_csrf.token}&quot;\/&gt;\n    &lt;!-- other form fields... --&gt;\n    &lt;button type=&quot;submit&quot;&gt;Submit&lt;\/button&gt;\n&lt;\/form&gt;\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u6bcf\u6b21\u63d0\u4ea4\u8868\u5355\u65f6\uff0c\u90fd\u4f1a\u643a\u5e26\u4e00\u4e2a\u6709\u6548\u7684 CSRF \u4ee4\u724c\uff0c\u9632\u6b62 CSRF \u653b\u51fb\u3002\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u5f0f\u914d\u7f6e CSRF \u4fdd\u62a4\uff0cSpring Security \u5c06\u5e2e\u52a9\u4f60\u9632\u8303 CSRF \u653b\u51fb\uff0c\u786e\u4fdd\u53ea\u6709\u6765\u81ea\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u8bf7\u6c42\u624d\u80fd\u6210\u529f\u6267\u884c\u654f\u611f\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CSRF\uff08Cross-Site Request Forgery\uff0c\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff09\u662f\u4e00\u79cd\u7f51\u7edc\u653b\u51fb\uff0c\u653b\u51fb\u8005\u901a\u8fc7\u6b3a\u9a97\u7528 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-8208","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=8208"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8208\/revisions"}],"predecessor-version":[{"id":16586,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8208\/revisions\/16586"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8208"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}