成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

跨域配置

Forelax / 1673人閱讀

摘要:而前端跨域請求的需求不減,于是乎??缬蛞脖容^簡單,只需添加以下配置即可。參考文檔,阿里云中文檔描述到也可通過配置文件跨域,不過筆者并未采用這種方式。瀏覽器設(shè)置跨域本身是可以通過配置支持跨域請求的。跨域參考文檔跨域參考文檔

SpringBoot跨域配置

我們的后端使用Spring Boot。Spring Boot跨域非常簡單,只需書寫以下代碼即可。

@Configuration
public class CustomCORSConfiguration {
  private CorsConfiguration buildConfig() {
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.addAllowedOrigin("*");
    corsConfiguration.addAllowedHeader("*");
    corsConfiguration.addAllowedMethod("*");
    return corsConfiguration;
  }
  @Bean
  public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", buildConfig());
    return new CorsFilter(source);
  }
}
Nginx跨域配置

某天,我們將Spring Boot應(yīng)用用Nginx反向代理。而前端跨域請求的需求不減,于是乎。

Nginx跨域也比較簡單,只需添加以下配置即可。

location / {
    proxy_pass http://localhost:8080;
    if ($request_method = "OPTIONS") {
        add_header "Access-Control-Allow-Origin" "*";
        add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
        add_header "Access-Control-Allow-Headers" "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token";
        add_header "Access-Control-Max-Age" 1728000;
        add_header "Content-Type" "text/plain; charset=utf-8";
        add_header "Content-Length" 0;
        return 204;
    }
    if ($request_method = "POST") {
        add_header "Access-Control-Allow-Origin" "*";
        add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
        add_header "Access-Control-Allow-Headers" "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token";
        add_header "Access-Control-Expose-Headers" "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token";
    }
    if ($request_method = "GET") {
        add_header "Access-Control-Allow-Origin" "*";
        add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
        add_header "Access-Control-Allow-Headers" "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token";
        add_header "Access-Control-Expose-Headers" "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token";
    }
}

其中: add_header "Access-Control-Expose-Headers" 務(wù)必加上你請求時(shí)所帶的header。例如本例中的“Token”,其實(shí)是前端傳給后端過來的。如果記不得也沒有關(guān)系,瀏覽器的調(diào)試器會(huì)有詳細(xì)說明。

參考文檔:https://enable-cors.org/serve...

B.T.W,阿里云中文檔描述到Nginx也可通過crossdomain.xml配置文件跨域:https://helpcdn.aliyun.com/kn... ,不過筆者并未采用這種方式。

CORS on Nginx

The following Nginx configuration enables CORS, with support for preflight requests.

#
# Wide-open CORS config for nginx
#
location / {
     if ($request_method = "OPTIONS") {
        add_header "Access-Control-Allow-Origin" "*";
        add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
        #
        # Custom headers and headers various browsers *should* be OK with but aren"t
        #
        add_header "Access-Control-Allow-Headers" "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range";
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header "Access-Control-Max-Age" 1728000;
        add_header "Content-Type" "text/plain; charset=utf-8";
        add_header "Content-Length" 0;
        return 204;
     }
     if ($request_method = "POST") {
        add_header "Access-Control-Allow-Origin" "*";
        add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
        add_header "Access-Control-Allow-Headers" "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range";
        add_header "Access-Control-Expose-Headers" "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range";
     }
     if ($request_method = "GET") {
        add_header "Access-Control-Allow-Origin" "*";
        add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
        add_header "Access-Control-Allow-Headers" "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range";
        add_header "Access-Control-Expose-Headers" "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range";
     }
}
瀏覽器設(shè)置跨域

Chrome、Firefox本身是可以通過配置支持跨域請求的。

Chrome跨域:參考文檔:http://www.cnblogs.com/laden6...

Firefox跨域:參考文檔:https://segmentfault.com/q/10...

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/75857.html

相關(guān)文章

  • 20K前端大佬面試(關(guān)于如何回答ajax跨域問題)

    摘要:在接觸前端開發(fā)起,跨域這個(gè)詞就一直以很高的頻率在我們學(xué)習(xí)工作中重復(fù)出現(xiàn),最近在工作中遇到了跨域的相關(guān)問題,這里我把它總結(jié)記錄一下。 在接觸前端開發(fā)起,跨域這個(gè)詞就一直以很高的頻率在我們學(xué)習(xí)工作中重復(fù)出現(xiàn),最近在工作中遇到了跨域的相關(guān)問題,這里我把它總結(jié)記錄一下。關(guān)于跨域,有N種類型,現(xiàn)在我只專注于ajax請求跨域(ajax跨域只是屬于瀏覽器同源策略中的一部分,其它的這里不做介紹),內(nèi)容...

    Yangyang 評(píng)論0 收藏0
  • ajax跨域,這應(yīng)該是最全的解決方案了

    摘要:關(guān)于,強(qiáng)烈推薦閱讀跨域資源共享詳解阮一峰另外,這里也整理了一個(gè)實(shí)現(xiàn)原理圖簡化版如何判斷是否是簡單請求瀏覽器將請求分成兩類簡單請求和非簡單請求。 前言 從剛接觸前端開發(fā)起,跨域這個(gè)詞就一直以很高的頻率在身邊重復(fù)出現(xiàn),一直到現(xiàn)在,已經(jīng)調(diào)試過N個(gè)跨域相關(guān)的問題了,16年時(shí)也整理過一篇相關(guān)文章,但是感覺還是差了點(diǎn)什么,于是現(xiàn)在重新梳理了一下。 個(gè)人見識(shí)有限,如有差錯(cuò),請多多見諒,歡迎提出iss...

    ytwman 評(píng)論0 收藏0
  • 跨域解決方案(史上最易懂)

    摘要:跨域總結(jié)跨域思路跨域解決方案一般分為兩種前端解決,后端解決前端解決方案通過前端解決的思想就是,通過設(shè)置中間件把跨域的請求轉(zhuǎn)發(fā)一下,其實(shí)就是反向代理,比如想要訪問豆瓣的接口很會(huì)有跨域問題,但是如果請求的是就不存在跨域反向代理就是截取之后的請求 跨域總結(jié) 1.跨域思路 跨域解決方案一般分為兩種:前端解決,后端解決 1.1 前端解決方案 通過前端解決的思想就是,通過設(shè)置中間件把跨域的請求轉(zhuǎn)發(fā)...

    wh469012917 評(píng)論0 收藏0
  • 第二十五章:SpringBoot添加支持CORS跨域訪問

    摘要:本章目標(biāo)基于項(xiàng)目搭建可以站外請求訪問的跨域資源服務(wù)器。允許所有的請求域名訪問我們的跨域資源,可以固定單條或者多條內(nèi)容,如,只有百度可以訪問我們的跨域資源。 CORS(Cross-Origin Resource Sharing)跨域資源共享,是一個(gè)W3C標(biāo)準(zhǔn),它允許瀏覽器向跨域服務(wù)器發(fā)送Ajax請求,打破了Ajax只能訪問本站內(nèi)的資源限制,CORS在很多地方都有被使用,微信支付的JS支付...

    simpleapples 評(píng)論0 收藏0
  • 跨域問題的一次深入研究

    摘要:前言最近在業(yè)務(wù)代碼中深受跨域問題困擾,因此特別寫一篇博客來記錄一下自己對跨域的理解以及使用到的參考資料。內(nèi)嵌式跨域通常也是允許的。而我使用時(shí)因?yàn)檫@個(gè)響應(yīng)報(bào)文最后被認(rèn)為是跨域問題,無法從中獲得的狀態(tài)碼。它代表服務(wù)器支持跨域時(shí)攜帶認(rèn)證信息。 前言 最近在業(yè)務(wù)代碼中深受跨域問題困擾,因此特別寫一篇博客來記錄一下自己對跨域的理解以及使用到的參考資料。本文的項(xiàng)目背景基于vue+vuex+axio...

    X_AirDu 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

Forelax

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<