跨域? ajax要求同源策略 如果8080去访问 8081就不属于同源。
因为网关是微服务的边缘 所有的请求都要走网关 跨域的配置只需要写在网关即可
方式一:配置类
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}
方式二:配置文件
spring:
cloud:
gateway:
globalcors:
corsConfigurations:
'[/**]': // 针对哪些路径
allowCredentials: true // 这个是可以携带 cookie
allowedHeaders: '*'
allowedMethods: '*'
allowedOrigins: '*'