站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        SpringBoot 與 Nginx跨域配置

        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ù)必加上你請求時所帶的header。例如本例中的“Token”,其實是前端傳給后端過來的。如果記不得也沒有關(guān)系,瀏覽器的調(diào)試器會有詳細(xì)說明。

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

        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本身是可以通過配置支持跨域請求的。

         

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號
        主站蜘蛛池模板: 精品99久久aaa一级毛片| 国产成人精品一区二三区在线观看| 亚洲天堂久久精品| 亚洲国产精品久久久久婷婷老年| 欧美一区二区精品久久| 一区二区精品在线| 精品国产婷婷久久久| 男人的天堂精品国产一区| 日韩欧美国产精品第一页不卡 | 国产精品成人观看视频| 久久99精品久久久久久秒播| 亚洲综合精品网站| aaa级精品久久久国产片| 国产区精品福利在线观看精品| 中文字幕久久精品| 99麻豆久久久国产精品免费| 久久国产香蕉一区精品| 亚洲精品乱码久久久久久久久久久久 | 欧美成人精品一级高清片| 日本精品少妇一区二区三区| 99热国内精品| 欧美精品黑人粗大欧| 国产精品青草久久久久婷婷| 手机日韩精品视频在线看网站| 国产成人精品日本亚洲网址| 久久午夜无码鲁丝片午夜精品| 6一12呦女精品| 伊人精品视频在线| 惠民福利中文字幕人妻无码乱精品| 欧美精品hdvideosex4k| 久久久久99精品成人片牛牛影视 | 国产精品区AV一区二区| 国产精品黄网站| 亚洲精品无码不卡在线播放HE| 97久久精品人人做人人爽| 99精品国产一区二区三区2021| 久久久久亚洲精品天堂| 久久亚洲精品成人AV| 91精品国产91热久久久久福利 | 亚洲精品成人片在线播放 | 亚洲精品tv久久久久久久久|