原創(chuàng)作者:周立 周立SpringCloud
跨域的玩法有很多,例如服務(wù)器端設(shè)置、瀏覽器端設(shè)置、Jsonp等等。本文只描述具體組件的具體做法,不談?wù)撥钕愣沟能钭钟袔追N寫法。
一、Spring Boot跨域配置
我們的后端使用Spring Boot。Spring Boot跨域非常簡(jiǎn)單,只需書寫以下代碼即可。
@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);
? ?}
}
代碼非常簡(jiǎn)單,不做贅述。該代碼在Spring Boot 1.5.4中測(cè)試通過。
二、Nginx跨域配置
某天,我們將Spring Boot應(yīng)用用Nginx反向代理。而前端跨域請(qǐng)求的需求不減,于是乎。
Nginx跨域也比較簡(jiǎn)單,只需添加以下配置即可。
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ù)必加上你請(qǐng)求時(shí)所帶的header。例如本例中的“Token”,其實(shí)是前端傳給后端過來的。如果記不得也沒有關(guān)系,瀏覽器的調(diào)試器會(huì)有詳細(xì)說明。
參考文檔:https://enable-cors.org/server_nginx.html
B.T.W,阿里云中文檔描述到Nginx也可通過crossdomain.xml配置文件跨域:https://helpcdn.aliyun.com/knowledge_detail/41123.html,不過筆者并未采用這種方式。
三、瀏覽器設(shè)置跨域
? ? Chrome、Firefox本身是可以通過配置支持跨域請(qǐng)求的。
? ? Chrome跨域:參考文檔:http://www.cnblogs.com/laden666666/p/5544572.html
? ? Firefox跨域:參考文檔:https://segmentfault.com/q/1010000002532581/a-1020000002533699
注:通過瀏覽器設(shè)置實(shí)現(xiàn)跨域的玩法,個(gè)人沒有親測(cè)過。