Spring Cloud Gateway跨域配置

Spring Cloud Gateway跨域配置


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**

* 配置跨域

* @return

*/

@Bean

public CorsWebFilter corsFilter() {

CorsConfiguration config = new CorsConfiguration();

// cookie跨域

config.setAllowCredentials(Boolean.TRUE);

config.addAllowedMethod("*");

config.addAllowedOrigin("*");

config.addAllowedHeader("*");

// 配置前端js允许访问的自定义响应头

config.addExposedHeader("setToken");

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());

source.registerCorsConfiguration("/**", config);

return new CorsWebFilter(source);

}

配置不生效方法

官方推荐配置

注意

如果下游服务也配置了跨域处理,会发生配置重复造成错误