1、跨域CORS概念
表象看:瀏覽器上的 IP,域名,端口 和你頁面內(nèi)請求的IP,域名,端口 之間組合不一致。這說法不夠嚴(yán)謹(jǐn),但不是本文的重點(diǎn),更多概念自行檢索。
2、spring-cloud-gateway微服務(wù)api網(wǎng)關(guān)配置跨域
spring-cloud-gateway3.x.x為例
2.1 配置文件-推薦
官方說明?Spring Cloud Gateway
配置參數(shù)說明:CorsConfiguration (Spring Framework 5.0.20.RELEASE API)?
spring:
cloud:
gateway:
globalcors: # 全局的跨域處理
add-to-simple-url-handler-mapping: true # 解決options請求被攔截問題
corsConfigurations:
'[/**]':
allowedOrigins: # 允許哪些網(wǎng)站的跨域請求 allowedOrigins: “*” 允許所有網(wǎng)站
- "http://localhost:8001"
allowedMethods: # 允許的跨域ajax的請求方式
- "GET"
- "POST"
- "DELETE"
- "PUT"
- "OPTIONS"
allowedHeaders: "*" # 允許在請求中攜帶的頭信息
allowCredentials: true # 是否允許攜帶cookie
maxAge: 360000 # 這次跨域檢測的有效期
?
2.2 配置類方式
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
@Configuration
public class CorsConfiguration {
@Bean
public CorsWebFilter corsWebFilter(){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration configuration = new CorsConfiguration();
// 配置跨域的信息
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
// SpringBoot升級到2.4.0 之后需要使用該配置
configuration.addAllowedOriginPattern("*");
configuration.setAllowCredentials(true);
source.registerCorsConfiguration("/**",configuration);
return new CorsWebFilter(source);
}
}
附中文文檔說明文章來源:http://www.zghlxwxcb.cn/news/detail-540320.html
Spring Cloud Gateway 3.1.3最新版中文手冊官網(wǎng)2022_杏花怎釀酒的博客-CSDN博客_gateway最新版本文章來源地址http://www.zghlxwxcb.cn/news/detail-540320.html
到了這里,關(guān)于spring cloud gateway跨域配置CORS Configuration的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!