{"id":10006423,"date":"2022-09-08T08:13:13","date_gmt":"2022-09-08T00:13:13","guid":{"rendered":"https:\/\/ityet.com\/?p=10006423"},"modified":"2022-09-08T08:13:13","modified_gmt":"2022-09-08T00:13:13","slug":"%e9%9d%a2%e8%af%95%e7%aa%81%e5%87%bb81%ef%bc%9a%e4%bb%80%e4%b9%88%e6%98%af%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98%ef%bc%9f%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006423","title":{"rendered":"\u9762\u8bd5\u7a81\u51fb81\uff1a\u4ec0\u4e48\u662f\u8de8\u57df\u95ee\u9898\uff1f\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p>\u8de8\u57df\u95ee\u9898\u6307\u7684\u662f\u4e0d\u540c\u7ad9\u70b9\u4e4b\u95f4\uff0c\u4f7f\u7528 ajax \u65e0\u6cd5\u76f8\u4e92\u8c03\u7528\u7684\u95ee\u9898\u3002<strong>\u8de8\u57df\u95ee\u9898\u672c\u8d28\u662f\u6d4f\u89c8\u5668\u7684\u4e00\u79cd\u4fdd\u62a4\u673a\u5236\uff0c\u5b83\u7684\u521d\u8877\u662f\u4e3a\u4e86\u4fdd\u8bc1\u7528\u6237\u7684\u5b89\u5168\uff0c\u9632\u6b62\u6076\u610f\u7f51\u7ad9\u7a83\u53d6\u6570\u636e\u3002<\/strong> \u4f46\u8fd9\u4e2a\u4fdd\u62a4\u673a\u5236\u4e5f\u5e26\u6765\u4e86\u65b0\u7684\u95ee\u9898\uff0c\u5b83\u7684\u95ee\u9898\u662f\u7ed9\u4e0d\u540c\u7ad9\u70b9\u4e4b\u95f4\u7684\u6b63\u5e38\u8c03\u7528\uff0c\u4e5f\u5e26\u6765\u7684\u963b\u788d\uff0c\u90a3\u600e\u4e48\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u63a5\u4e0b\u6765\u6211\u4eec\u4e00\u8d77\u6765\u770b\u3002<\/p>\n<h2>1.\u8de8\u57df\u4e09\u79cd\u60c5\u51b5<\/h2>\n<p>\u5728\u8bf7\u6c42\u65f6\uff0c\u5982\u679c\u51fa\u73b0\u4e86\u4ee5\u4e0b\u60c5\u51b5\u4e2d\u7684\u4efb\u610f\u4e00\u79cd\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u8de8\u57df\u8bf7\u6c42\uff1a<\/p>\n<ol>\n<li><strong>\u534f\u8bae\u4e0d\u540c\uff0c\u5982 http \u548c https\uff1b<\/strong><\/li>\n<li><strong>\u57df\u540d\u4e0d\u540c\uff1b<\/strong><\/li>\n<li><strong>\u7aef\u53e3\u4e0d\u540c\u3002<\/strong><\/li>\n<\/ol>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c<strong>\u5373\u4f7f\u57df\u540d\u76f8\u540c\uff0c\u5982\u679c\u4e00\u4e2a\u4f7f\u7528\u7684\u662f http\uff0c\u53e6\u4e00\u4e2a\u4f7f\u7528\u7684\u662f https\uff0c\u90a3\u4e48\u5b83\u4eec\u4e5f\u5c5e\u4e8e\u8de8\u57df\u8bbf\u95ee<\/strong>\u3002\u5e38\u89c1\u7684\u8de8\u57df\u95ee\u9898\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042447583\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>2.\u8de8\u57df\u95ee\u9898\u6f14\u793a<\/h2>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u4e24\u4e2a Spring Boot \u9879\u76ee\u6765\u6f14\u793a\u8de8\u57df\u7684\u95ee\u9898\uff0c\u5176\u4e2d\u4e00\u4e2a\u662f\u7aef\u53e3\u53f7\u4e3a 8080 \u7684\u524d\u7aef\u9879\u76ee\uff0c\u53e6\u4e00\u4e2a\u7aef\u53e3\u53f7\u4e3a 9090 \u7684\u540e\u7aef\u63a5\u53e3\u9879\u76ee\u3002<\/p>\n<h3>2.1 \u524d\u7aef\u7f51\u7ad9<\/h3>\n<p>\u524d\u7aef\u9879\u76ee\u53ea\u9700\u8981\u5728 resources \u4e0b\u653e\u4e24\u4e2a\u6587\u4ef6\uff0c\u4e00\u4e2a\u7528\u4e8e\u53d1\u9001 ajax \u8bf7\u6c42\u7684 jquery.js\uff0c\u53e6\u4e00\u4e2a\u662f html \u524d\u7aef\u9875\u9762\uff0c\u5de5\u7a0b\u76ee\u5f55\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042447584\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u5176\u4e2d\u524d\u7aef\u9875\u9762 index.html \u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"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;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;\u8de8\u57df\u6d4b\u8bd5\u9875\u9762&lt;\/title&gt;\n    &lt;script src=&quot;js\/jquery.min.js&quot;&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;\u8de8\u57df\u6d4b\u8bd5&lt;\/h1&gt;\n&lt;div&gt;\n    &lt;input type=&quot;button&quot; onclick=&quot;mySubmit()&quot; value=&quot; \u53d1\u9001\u8de8\u57df\u8bf7\u6c42 &quot;&gt;\n&lt;\/div&gt;\n&lt;script&gt;\n    function mySubmit() {\n        \/\/ \u53d1\u9001\u8de8\u57df\u8bf7\u6c42\n        jQuery.ajax({\n            url: &quot;http:\/\/localhost:9090\/test&quot;,\n            type: &quot;POST&quot;,\n            data: {&quot;name&quot;: &quot;Java&quot;},\n            success: function (result) {\n                alert(&quot;\u8fd4\u56de\u6570\u636e\uff1a&quot; + result.data);\n            }\n        });\n    }\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h3>2.2 \u540e\u7aef\u63a5\u53e3<\/h3>\n<p>\u540e\u7aef\u63a5\u53e3\u9879\u76ee\u9996\u5148\u5148\u5728 application.properties \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u8bbe\u7f6e\u9879\u76ee\u7684\u7aef\u53e3\u53f7\u4e3a 9090\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"properties\">server.port=9090<\/code><\/pre>\n<p>\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u540e\u7aef\u63a7\u5236\u5668\uff0c\u8fd4\u56de\u4e00\u4e2a JSON \u683c\u5f0f\u7684\u6570\u636e\uff0c\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\nimport java.util.HashMap;\n\n@RestController\npublic class TestController {\n    @RequestMapping(&quot;\/test&quot;)\n    public HashMap&lt;String, Object&gt; test() {\n        return new HashMap&lt;String, Object&gt;() {{\n            put(&quot;state&quot;, 200);\n            put(&quot;data&quot;, &quot;success&quot;);\n            put(&quot;msg&quot;, &quot;&quot;);\n        }};\n    }\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4e24\u4e2a\u9879\u76ee\u521b\u5efa\u5e76\u542f\u52a8\u6210\u529f\u4e4b\u540e\uff0c\u4f7f\u7528\u524d\u7aef\u9879\u76ee\u8bbf\u95ee\u540e\u7aef\u63a5\u53e3\uff0c\u56e0\u4e3a\u7aef\u53e3\u4e0d\u4e00\u6837\uff0c\u6240\u4ee5\u4e5f\u5c5e\u4e8e\u8de8\u57df\u8bbf\u95ee\uff0c\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042447585\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>3.\u89e3\u51b3\u8de8\u57df\u95ee\u9898<\/h2>\n<p>\u5728 Spring  Boot \u4e2d\u8de8\u57df\u95ee\u9898\u6709\u5f88\u591a\u79cd\u89e3\u51b3\u65b9\u6848\uff0c\u6bd4\u5982\u4ee5\u4e0b 5 \u4e2a\uff1a<\/p>\n<ol>\n<li><strong>\u4f7f\u7528 @CrossOrigin \u6ce8\u89e3\u5b9e\u73b0\u8de8\u57df\uff1b<\/strong><\/li>\n<li><strong>\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u5b9e\u73b0\u8de8\u57df\uff1b<\/strong><\/li>\n<li><strong>\u901a\u8fc7 CorsFilter \u5bf9\u8c61\u5b9e\u73b0\u8de8\u57df\uff1b<\/strong><\/li>\n<li><strong>\u901a\u8fc7 Response \u5bf9\u8c61\u5b9e\u73b0\u8de8\u57df\uff1b<\/strong><\/li>\n<li><strong>\u901a\u8fc7\u5b9e\u73b0 ResponseBodyAdvice \u5b9e\u73b0\u8de8\u57df\u3002<\/strong><\/li>\n<\/ol>\n<p>\u5f53\u7136\u5982\u679c\u4f60\u613f\u610f\u7684\u8bdd\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8fc7\u6ee4\u5668\u6765\u5b9e\u73b0\u8de8\u57df\uff0c\u4f46\u5b83\u7684\u5b9e\u73b0\u548c\u7b2c 5 \u79cd\u5b9e\u73b0\u7c7b\u4f3c\uff0c\u6240\u4ee5\u672c\u6587\u5c31\u4e0d\u8d58\u8ff0\u4e86\u3002<\/p>\n<h3>3.1 \u901a\u8fc7\u6ce8\u89e3\u8de8\u57df<\/h3>\n<p><strong>\u4f7f\u7528 @CrossOrigin \u6ce8\u89e3\u53ef\u4ee5\u8f7b\u677e\u7684\u5b9e\u73b0\u8de8\u57df\uff0c\u6b64\u6ce8\u89e3\u65e2\u53ef\u4ee5\u4fee\u9970\u7c7b\uff0c\u4e5f\u53ef\u4ee5\u4fee\u9970\u65b9\u6cd5\u3002\u5f53\u4fee\u9970\u7c7b\u65f6\uff0c\u8868\u793a\u6b64\u7c7b\u4e2d\u7684\u6240\u6709\u63a5\u53e3\u90fd\u53ef\u4ee5\u8de8\u57df\uff1b\u5f53\u4fee\u9970\u65b9\u6cd5\u65f6\uff0c\u8868\u793a\u6b64\u65b9\u6cd5\u53ef\u4ee5\u8de8\u57df<\/strong>\uff0c\u5b83\u7684\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.web.bind.annotation.CrossOrigin;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\nimport java.util.HashMap;\n\n@RestController\n@CrossOrigin(origins = &quot;*&quot;)\npublic class TestController {\n    @RequestMapping(&quot;\/test&quot;)\n    public HashMap&lt;String, Object&gt; test() {\n        return new HashMap&lt;String, Object&gt;() {{\n            put(&quot;state&quot;, 200);\n            put(&quot;data&quot;, &quot;success&quot;);\n            put(&quot;msg&quot;, &quot;&quot;);\n        }};\n    }\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042447586\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u4ece\u4e0a\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u524d\u7aef\u9879\u76ee\u8bbf\u95ee\u53e6\u4e00\u4e2a\u540e\u7aef\u9879\u76ee\u6210\u529f\u4e86\uff0c\u4e5f\u5c31\u8bf4\u660e\u5b83\u89e3\u51b3\u4e86\u8de8\u57df\u95ee\u9898\u3002<br \/><strong>\u4f18\u7f3a\u70b9\u5206\u6790<\/strong><br \/>\u6b64\u65b9\u5f0f\u867d\u7136\u867d\u7136\u5b9e\u73b0\uff08\u8de8\u57df\uff09\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u7ec6\u5fc3\u7684\u670b\u53cb\u4e5f\u80fd\u53d1\u73b0\uff0c<strong>\u4f7f\u7528\u6b64\u65b9\u5f0f\u53ea\u80fd\u5b9e\u73b0\u5c40\u90e8\u8de8\u57df\uff0c\u5f53\u4e00\u4e2a\u9879\u76ee\u4e2d\u5b58\u5728\u591a\u4e2a\u7c7b\u7684\u8bdd\uff0c\u4f7f\u7528\u6b64\u65b9\u5f0f\u5c31\u4f1a\u6bd4\u8f83\u9ebb\u70e6\uff08\u9700\u8981\u7ed9\u6240\u6709\u7c7b\u4e0a\u90fd\u6dfb\u52a0\u6b64\u6ce8\u89e3\uff09\u3002<\/strong><\/p>\n<h3>3.2 \u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u8de8\u57df<\/h3>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec<strong>\u901a\u8fc7\u8bbe\u7f6e\u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u5f0f\u5c31\u53ef\u4ee5\u5b9e\u73b0\u5168\u5c40\u8de8\u57df<\/strong>\u4e86\uff0c\u5b83\u7684\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u914d\u7f6e\u6587\u4ef6\uff1b<\/li>\n<li>\u6dfb\u52a0 @Configuration \u6ce8\u89e3\uff0c\u5b9e\u73b0 WebMvcConfigurer \u63a5\u53e3\uff1b<\/li>\n<li>\u91cd\u5199 addCorsMappings \u65b9\u6cd5\uff0c\u8bbe\u7f6e\u5141\u8bb8\u8de8\u57df\u7684\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p>\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.context.annotation.Configuration;\nimport org.springframework.web.servlet.config.annotation.CorsRegistry;\nimport org.springframework.web.servlet.config.annotation.WebMvcConfigurer;\n\n@Configuration \/\/ \u4e00\u5b9a\u4e0d\u8981\u5ffd\u7565\u6b64\u6ce8\u89e3\npublic class CorsConfig implements WebMvcConfigurer {\n    @Override\n    public void addCorsMappings(CorsRegistry registry) {\n        registry.addMapping(&quot;\/**&quot;) \/\/ \u6240\u6709\u63a5\u53e3\n                .allowCredentials(true) \/\/ \u662f\u5426\u53d1\u9001 Cookie\n                .allowedOriginPatterns(&quot;*&quot;) \/\/ \u652f\u6301\u57df\n                .allowedMethods(new String[]{&quot;GET&quot;, &quot;POST&quot;, &quot;PUT&quot;, &quot;DELETE&quot;}) \/\/ \u652f\u6301\u65b9\u6cd5\n                .allowedHeaders(&quot;*&quot;)\n                .exposedHeaders(&quot;*&quot;);\n    }\n}<\/code><\/pre>\n<h3>3.3 <strong>\u901a\u8fc7 CorsFilter \u8de8\u57df<\/strong><\/h3>\n<p>\u6b64\u5b9e\u73b0\u65b9\u5f0f\u548c\u4e0a\u4e00\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7c7b\u4f3c\uff0c<strong>\u5b83\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5168\u5c40\u8de8\u57df<\/strong>\uff0c\u5b83\u7684\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.web.cors.CorsConfiguration;\nimport org.springframework.web.cors.UrlBasedCorsConfigurationSource;\nimport org.springframework.web.filter.CorsFilter;\n\n@Configuration \/\/ \u4e00\u5b9a\u4e0d\u80fd\u5ffd\u7565\u6b64\u6ce8\u89e3\npublic class MyCorsFilter {\n    @Bean\n    public CorsFilter corsFilter() {\n        \/\/ 1.\u521b\u5efa CORS \u914d\u7f6e\u5bf9\u8c61\n        CorsConfiguration config = new CorsConfiguration();\n        \/\/ \u652f\u6301\u57df\n        config.addAllowedOriginPattern(&quot;*&quot;);\n        \/\/ \u662f\u5426\u53d1\u9001 Cookie\n        config.setAllowCredentials(true);\n        \/\/ \u652f\u6301\u8bf7\u6c42\u65b9\u5f0f\n        config.addAllowedMethod(&quot;*&quot;);\n        \/\/ \u5141\u8bb8\u7684\u539f\u59cb\u8bf7\u6c42\u5934\u90e8\u4fe1\u606f\n        config.addAllowedHeader(&quot;*&quot;);\n        \/\/ \u66b4\u9732\u7684\u5934\u90e8\u4fe1\u606f\n        config.addExposedHeader(&quot;*&quot;);\n        \/\/ 2.\u6dfb\u52a0\u5730\u5740\u6620\u5c04\n        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();\n        corsConfigurationSource.registerCorsConfiguration(&quot;\/**&quot;, config);\n        \/\/ 3.\u8fd4\u56de CorsFilter \u5bf9\u8c61\n        return new CorsFilter(corsConfigurationSource);\n    }\n}<\/code><\/pre>\n<h3>3.4 \u901a\u8fc7 Response \u8de8\u57df<\/h3>\n<p><strong>\u6b64\u65b9\u5f0f\u662f\u89e3\u51b3\u8de8\u57df\u95ee\u9898\u6700\u539f\u59cb\u7684\u65b9\u5f0f\uff0c\u4f46\u5b83\u53ef\u4ee5\u652f\u6301\u4efb\u610f\u7684 Spring Boot \u7248\u672c\uff08\u65e9\u671f\u7684 Spring Boot \u7248\u672c\u4e5f\u662f\u652f\u6301\u7684\uff09\u3002\u4f46\u6b64\u65b9\u5f0f\u4e5f\u662f\u5c40\u90e8\u8de8\u57df\uff0c\u5b83\u5e94\u7528\u7684\u8303\u56f4\u6700\u5c0f\uff0c\u8bbe\u7f6e\u7684\u662f\u65b9\u6cd5\u7ea7\u522b\u7684\u8de8\u57df<\/strong>\uff0c\u5b83\u7684\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\nimport javax.servlet.http.HttpServletResponse;\nimport java.util.HashMap;\n\n@RestController\npublic class TestController {\n    @RequestMapping(&quot;\/test&quot;)\n    public HashMap&lt;String, Object&gt; test(HttpServletResponse response) {\n        \/\/ \u8bbe\u7f6e\u8de8\u57df\n        response.setHeader(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);\n        return new HashMap&lt;String, Object&gt;() {{\n            put(&quot;state&quot;, 200);\n            put(&quot;data&quot;, &quot;success&quot;);\n            put(&quot;msg&quot;, &quot;&quot;);\n        }};\n    }\n}<\/code><\/pre>\n<h3>3.5 \u901a\u8fc7 ResponseBodyAdvice \u8de8\u57df<\/h3>\n<p>\u901a\u8fc7\u91cd\u5199 ResponseBodyAdvice \u63a5\u53e3\u4e2d\u7684 beforeBodyWrite\uff08\u8fd4\u56de\u4e4b\u524d\u91cd\u5199\uff09\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6240\u6709\u7684\u63a5\u53e3\u8fdb\u884c\u8de8\u57df\u8bbe\u7f6e\uff0c\u5b83\u7684\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.core.MethodParameter;\nimport org.springframework.http.MediaType;\nimport org.springframework.http.server.ServerHttpRequest;\nimport org.springframework.http.server.ServerHttpResponse;\nimport org.springframework.web.bind.annotation.ControllerAdvice;\nimport org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;\n\n@ControllerAdvice\npublic class ResponseAdvice implements ResponseBodyAdvice {\n    \/**\n     * \u5185\u5bb9\u662f\u5426\u9700\u8981\u91cd\u5199\uff08\u901a\u8fc7\u6b64\u65b9\u6cd5\u53ef\u4ee5\u9009\u62e9\u6027\u90e8\u5206\u63a7\u5236\u5668\u548c\u65b9\u6cd5\u8fdb\u884c\u91cd\u5199\uff09\n     * \u8fd4\u56de true \u8868\u793a\u91cd\u5199\n     *\/\n    @Override\n    public boolean supports(MethodParameter returnType, Class converterType) {\n        return true;\n    }\n    \/**\n     * \u65b9\u6cd5\u8fd4\u56de\u4e4b\u524d\u8c03\u7528\u6b64\u65b9\u6cd5\n     *\/\n    @Override\n    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,\n                                  Class selectedConverterType, ServerHttpRequest request,\n                                  ServerHttpResponse response) {\n        \/\/ \u8bbe\u7f6e\u8de8\u57df\n        response.getHeaders().set(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);\n        return body;\n    }\n}<\/code><\/pre>\n<p><strong>\u6b64\u5b9e\u73b0\u65b9\u5f0f\u4e5f\u662f\u5168\u5c40\u8de8\u57df\uff0c\u5b83\u5bf9\u6574\u4e2a\u9879\u76ee\u4e2d\u7684\u6240\u6709\u63a5\u53e3\u6709\u6548\u3002<\/strong><\/p>\n<h2>4.\u539f\u7406\u5206\u6790<\/h2>\n<p>\u4e3a\u4ec0\u4e48\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u8bbe\u7f6e\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u6b63\u5e38\u4ea4\u4e92\u5462\uff1f<br \/>\u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u4e5f\u5f88\u7b80\u5355\uff0c\u6211\u4eec\u4e4b\u524d\u5728\u8bf4\u8de8\u57df\u65f6\u8bb2\u5230\uff1a\u201c<strong>\u8de8\u57df\u95ee\u9898\u672c\u8d28\u662f\u6d4f\u89c8\u5668\u7684\u884c\u4e3a\uff0c\u5b83\u7684\u521d\u8877\u662f\u4e3a\u4e86\u4fdd\u8bc1\u7528\u6237\u7684\u8bbf\u95ee\u5b89\u5168\uff0c\u9632\u6b62\u6076\u610f\u7f51\u7ad9\u7a83\u53d6\u6570\u636e<\/strong>\u201d\uff0c\u90a3\u60f3\u8981\u89e3\u51b3\u8de8\u57df\u95ee\u9898\u5c31\u53d8\u5f97\u5f88\u7b80\u5355\u4e86\uff0c<strong>\u53ea\u9700\u8981\u544a\u8bc9\u6d4f\u89c8\u5668\u8fd9\u662f\u4e00\u4e2a\u5b89\u5168\u7684\u8bf7\u6c42\uff0c\u201c\u6211\u662f\u81ea\u5df1\u4eba\u201d\u5c31\u884c\u4e86<\/strong>\uff0c\u90a3\u600e\u4e48\u544a\u8bc9\u6d4f\u89c8\u5668\u8fd9\u662f\u4e00\u4e2a\u6b63\u5e38\u7684\u8bf7\u6c42\u5462\uff1f<br \/>\u53ea\u9700\u8981<strong>\u5728\u8fd4\u56de\u5934\u4e2d\u8bbe\u7f6e\u201cAccess-Control-Allow-Origin\u201d\u53c2\u6570\u5373\u53ef\u89e3\u51b3\u8de8\u57df\u95ee\u9898\uff0c\u6b64\u53c2\u6570\u5c31\u662f\u7528\u6765\u8868\u793a\u5141\u8bb8\u8de8\u57df\u8bbf\u95ee\u7684\u539f\u59cb\u57df\u540d\u7684\uff0c\u5f53\u8bbe\u7f6e\u4e3a\u201c*\u201d\u65f6\uff0c\u8868\u793a\u5141\u8bb8\u6240\u6709\u7ad9\u70b9\u8de8\u57df\u8bbf\u95ee<\/strong>\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042447587\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u6240\u4ee5<strong>\u4ee5\u4e0a 5 \u79cd\u89e3\u51b3\u8de8\u57df\u95ee\u9898\u7684\u672c\u8d28\u90fd\u662f\u7ed9\u54cd\u5e94\u5934\u4e2d\u52a0\u4e86\u4e00\u4e2a Access-Control-Allow-Origin \u7684\u54cd\u5e94\u5934\u800c\u5df2\u3002<\/strong><\/p>\n<h4>\u6f14\u793a\u9879\u76ee\u6e90\u7801<\/h4>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=pJyS4nsskoNZ9c9fLVkBFw%3D%3D.g35lZOGj18A7NyprEr0oH%2BgzEHKYz1K1TgpSzbJ12pWIWR43GR%2FabFGMQ3YPV3fyLT2SQSS0mJ28uQ5YHbfqcVKkMBg7q64zElqpzPvAm3Q%3D\" rel=\"nofollow\">https:\/\/gitee.com\/mydb\/springboot-examples\/tree\/master\/spring-boot-cross<\/a><\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u8de8\u57df\u95ee\u9898\u7684\u672c\u8d28\u662f\u6d4f\u89c8\u5668\u4e3a\u4e86\u4fdd\u8bc1\u7528\u6237\u7684\u4e00\u79cd\u5b89\u5168\u62e6\u622a\u673a\u5236\uff0c\u60f3\u8981\u89e3\u51b3\u8de8\u57df\u95ee\u9898\uff0c\u53ea\u9700\u8981\u544a\u8bc9\u6d4f\u89c8\u5668\u201c\u6211\u662f\u81ea\u5df1\u4eba\uff0c\u4e0d\u8981\u62e6\u6211\u201d\u5c31\u884c\u3002\u5b83\u7684\u5e38\u89c1\u5b9e\u73b0\u65b9\u5f0f\u6709 5 \u79cd\uff1a\u901a\u8fc7\u6ce8\u89e3\u5b9e\u73b0\u5c40\u90e8\u8de8\u57df\u3001\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u5b9e\u73b0\u5168\u5c40\u8de8\u57df\u3001\u901a\u8fc7 CorsFilter \u5bf9\u8c61\u5b9e\u73b0\u5168\u5c40\u8de8\u57df\u3001\u901a\u8fc7 Response \u5bf9\u8c61\u5b9e\u73b0\u5c40\u90e8\u8de8\u57df\uff0c\u901a\u8fc7 ResponseBodyAdvice \u5b9e\u73b0\u5168\u5c40\u8de8\u57df\u3002<\/p>\n<h4>\u53c2\u8003 &#038; \u9e23\u8c22<\/h4>\n<p>blog.csdn.net\/pjmike233\/article\/details\/82461911<\/p>\n<blockquote>\n<p>\u662f\u975e\u5ba1\u4e4b\u4e8e\u5df1\uff0c\u6bc1\u8a89\u542c\u4e4b\u4e8e\u4eba\uff0c\u5f97\u5931\u5b89\u4e4b\u4e8e\u6570\u3002<\/p>\n<p>\u516c\u4f17\u53f7\uff1aJava\u9762\u8bd5\u771f\u9898\u89e3\u6790<\/p>\n<p>\u9762\u8bd5\u5408\u96c6\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=bI2NH1JFP5X3bB4bHakKXw%3D%3D.mXScw2j4ZU%2Brqbk1ANOGgzes0ARw337n%2BWhjt4vYAtROoIusO%2Fp0D3Ey9w68VIAg\" rel=\"nofollow\">https:\/\/gitee.com\/mydb\/interview<\/a><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042447581\">https:\/\/segmentfault.com\/a\/1190000042447581<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8de8\u57df\u95ee\u9898\u6307\u7684\u662f\u4e0d\u540c\u7ad9\u70b9\u4e4b\u95f4\uff0c\u4f7f\u7528 ajax \u65e0\u6cd5\u76f8\u4e92\u8c03\u7528\u7684\u95ee\u9898\u3002\u8de8\u57df\u95ee\u9898\u672c\u8d28\u662f\u6d4f\u89c8\u5668\u7684\u4e00\u79cd\u4fdd\u62a4\u673a\u5236\uff0c\u5b83\u7684\u521d\u8877\u662f [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006423","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006423"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006423\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}