{"id":176851,"date":"2020-10-16T22:44:07","date_gmt":"2020-10-16T14:44:07","guid":{"rendered":"http:\/\/4563.org\/?p=176851"},"modified":"2020-10-16T22:44:07","modified_gmt":"2020-10-16T14:44:07","slug":"springmvc-%e5%85%a8%e5%b1%80-cors-%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=176851","title":{"rendered":"SpringMvc \u5168\u5c40 CORS \u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  SpringMvc \u5168\u5c40 CORS \u89e3\u51b3\u65b9\u6848               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : haishiwuyuehao <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>@Component public class CORSConfig implements WebMvcConfigurer{   @Override  public void addCorsMappings(CorsRegistry registry) {   registry.addMapping(\"\/**\")     .allowedOrigins(\"*\")     .allowedHeaders(\"*\")     .allowedMethods(\"*\")     .allowCredentials(true);   WebMvcConfigurer.super.addCorsMappings(registry);  }   } <\/code><\/pre>\n<pre><code>Connection keep-alive Content-Type application\/json Date Mon, 26 Oct 2020 01:04:07 GMT Keep-Alive timeout=60 Transfer-Encoding chunked Vary Origin, Access-Control-Request-Method, Access-Control-Request-Headers <\/code><\/pre>\n<p>\u4e0d\u518d\u5efa\u8bae\u5927\u5bb6\u4f7f\u7528 <code>WebMvcConfigurerAdapter<\/code> \u8be5\u7c7b\u4ee5\u8fc7\u65f6<\/p>\n<p>CORS: Cross Origin Resouce Shading \u8de8\u8d44\u6e90\u5171\u4eab,\u5b9a\u4e49\u4e86\u6d4f\u89c8\u5668\u4e0e\u670d\u52a1\u5668\u5982\u4f55\u5b9e\u73b0\u8de8\u6e90\u901a\u4fe1.CORS \u80cc\u540e\u7684\u57fa\u672c\u601d\u8def\u5c31\u662f\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 HTTP \u5934\u90e8\u5141\u8bb8\u6d4f\u89c8\u5668\u548c\u670d\u52a1\u5668\u76f8\u4e92\u4e86\u89e3\uff0c\u4ee5\u786e\u5b9e\u8bf7\u6c42\u706b\u54cd\u5e94\u5e94\u8be5\u662f\u6210\u529f\u8fd8\u662f\u5931\u8d25\u3002<\/p>\n<p>\u5bf9\u4e8e\u7b80\u5355\u7684\u8bf7\u6c42\uff0c\u6bd4\u5982 GET \u6216 POST \u8bf7\u6c42\uff0c\u6ca1\u6709\u81ea\u5b9a\u4e49\u5934\u90e8\uff0c\u800c\u4e14\u8bf7\u6c42\u4f53\u662f text\/plain \u7c7b\u578b\uff0c\u5728\u53d1\u9001\u8bf7\u6c42\u524d\uff0c\u6d4f\u89c8\u5668\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\u4f1a\u6709\u4e00\u4e2a<em>\u989d\u5916<\/em>\u7684\u5934\u90e8<em>Origin<\/em> Origin \u5934\u90e8\u5305\u542b\u53d1\u9001\u8bf7\u6c42\u7684\u6e90\uff0c\u4ee5\u4fbf\u670d\u52a1\u5668\u786e\u5b9a\u662f\u5426\u4e3a\u5176\u63d0\u4f9b\u54cd\u5e94\u3002<\/p>\n<pre><code>\u5728\u53d1\u9001\u8bf7\u6c42\u65f6,\u6d4f\u89c8\u5668\u6dfb\u52a0\u7684 Origin Origin:http:\/\/127.0.0.1:5500  \u6574\u4e2a\u8bf7\u6c42\u5934 GET \/menu\/all HTTP\/1.1 Host: 127.0.0.1:8080 User-Agent: Mozilla\/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko\/20100101 Firefox\/83.0 Accept: *\/* Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2 Accept-Encoding: gzip, deflate Referer: http:\/\/127.0.0.1:5500\/index.html Origin: http:\/\/127.0.0.1:5500 Connection: keep-alive Cache-Control: max-age=0  \u54cd\u5e94\u5934\uff1a Access-Control-Allow-Origin: http:\/\/127.0.0.1:5500 Access-Control-Allow-Origin \u8bbe\u7f6e\u8be5\u6e90\uff0c\u8868\u793a\u5141\u8bb8\u8be5\u6e90\u8bbf\u95ee\u3002Java \u4ee3\u7801\u91cc\u8bbe\u7f6e\u7684 <\/code><\/pre>\n<p>CORS \u9884\u68c0\u8bf7\u6c42,\u9664\u5f00 POST,GET \u8bf7\u6c42\u4ee5\u5916\u7684\u5176\u5b83\u9ad8\u7ea7\u8bf7\u6c42\u9700\u8981\u88ab\u54cd\u5e94\u7684\u8bdd.\u6d4f\u89c8\u5668\u4f1a\u5148\u53d1\u9001\u4e00\u4e2a OPTIONS \u8bf7\u6c42,\u5728\u8be5\u8bf7\u6c42\u4e2d\u8bbe\u7f6e\u8bf7\u6c42\u5934<code>Access-Control-Allow-Method<\/code>\u9700\u8981\u88ab\u54cd\u5e94\u7684\u65b9\u6cd5 <code>Access-Control-Allow-Headers<\/code>\u9700\u8981\u88ab\u54cd\u5e94\u7684\u8bf7\u6c42\u5305\u542b\u7684\u8bf7\u6c42\u5934<\/p>\n<p>\u670d\u52a1\u5668\u4f1a\u5728\u54cd\u5e94\u5934\u4e2d\u7ed9\u51fa\u652f\u6301\u7684\u4fe1\u606f<\/p>\n<pre><code>Access-Control-Allow-Origin: \u652f\u6301\u7684\u6e90 Access-Control-Allow-Methods: \u652f\u6301\u7684\u8bf7\u6c42\u65b9\u6cd5\u5217\u8868 Access-Control-Allow-Headers: \u652f\u6301\u7684\u81ea\u5b9a\u4e49\u8bf7\u6c42\u5934 Access-Control-Allow-Max-Age: \u7f13\u5b58\u9884\u68c0\u67e5\u8bf7\u6c42\u7684\u79d2\u6570  \u54cd\u5e94\uff1a Access-Control-Allow-Origin: http:\/\/127.0.0.1:5500 Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT Access-Control-Allow-Headers: origin Access-Control-Allow-Max-Age: 1728000 <\/code><\/pre>\n<p>MDN\uff1a@https:\/\/developer.mozilla.org\/zh-CN\/docs\/Glossary\/Preflight_request<\/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=\"3904833\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wanacry <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u5728\u6559\u6211\u505a\u4e8b?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3904834\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : haishiwuyuehao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wanacry \u661f\u4ed4\uff0c\u4f60\u5728\u6559\u6211\u505a\u4e8b\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3904835\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shenlanAZ <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8fd9\u4e0d\u662f\u76f8\u5f53\u4e8e\u628a\u8de8\u57df\u5173\u6389\u4e86\u4e48\uff0c\u90a3\u4f60 cookie \u5b89\u5168\u7684\u89e3\u51b3\u65b9\u6848\u5462\uff1f<\/p>\n<p>\u8fd8\u6709 WebMvcConfigurerAdapter \u8fc7\u65f6\u4e5f\u8981\u770b\u7248\u672c\u7684\uff0c\u81f3\u5c11\u4e5f\u8981\u628a\u76f8\u5e94\u7684 spring \u7248\u672c\u6807\u51fa\u6765\u5427\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3904836\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wysnylc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8de8\u57df\u8ba9\u8fd0\u7ef4\u7528 nginx \u641e,\u522b\u5728\u9879\u76ee\u91cc\u6298\u817e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3904837\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jzmws <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wysnylc \u7528 nginx \u641e\u5e94\u8be5\u662f\u6700\u5408\u9002\u7684\u4e00\u4e2a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3904838\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : w292614191 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             CSDN ?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3904839\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : w292614191 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;CORS \u9884\u68c0\u8bf7\u6c42,\u9664\u5f00 POST,GET \u8bf7\u6c42\u4ee5\u5916\u7684\u5176\u5b83\u9ad8\u7ea7\u8bf7\u6c42\u9700\u8981\u88ab\u54cd\u5e94\u7684\u8bdd.\u6d4f\u89c8\u5668\u4f1a\u5148\u53d1\u9001\u4e00\u4e2a OPTIONS \u8bf7\u6c42,&#8221;<\/p>\n<p>\u6211 POST \u8fd8\u662f\u4f1a\u53d1\u9001 OPTIONS \u8bf7\u6c42\u3002<\/p>\n<p>\u4f60\u6d4b\u8bd5\u4e86\u5417\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3904840\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : w292614191 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Livid                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3904841\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : napsterwu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Spring \u8fd9\u4e2a\u4e0d\u592a\u597d\u7528\uff0c\u9047\u5230\u5f02\u5e38\u6807\u5934\u5c31\u6ca1\u4e86\uff0c\u5b89\u5353\u6709\u7684\u8bbe\u5907\u6ca1\u6709 Origin \u5934\uff0c\u4ed6\u4e5f\u4e0d\u8fd4\u56de\u6807\u5934\u3002ng \u6bd4\u8f83\u5b9e\u5728\uff0c\u5982\u679c\u6ca1\u6709 ng\uff0c\u81ea\u5df1\u5199\u4e2a\u7b80\u5355\u7684 interceptor \u4e5f\u597d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3904842\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Boyce <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @w292614191 POST \u65f6 Content-Type \u4e3a application\/json \u4e5f\u4f1a\u53d1\u9001 OPTIONS \u9884\u8bf7\u6c42\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>SpringMvc \u5168\u5c40 CORS&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\/176851"}],"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=176851"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/176851\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=176851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=176851"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=176851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}