springcloud gateway 跨域
时间: 2023-07-01 13:14:45 浏览: 128
要解决Spring Cloud Gateway的跨域问题,可以在Gateway配置文件中添加如下代码:
```yaml
spring:
cloud:
gateway:
globalcors:
corsConfigurations:
'[/**]':
allowedOrigins: "*"
allowedMethods:
- GET
- POST
- PUT
- DELETE
allowedHeaders:
- "*"
allowCredentials: true
```
这段代码中,`allowedOrigins` 配置项表示允许所有来源的请求,`allowedMethods` 配置项表示允许的请求方法,`allowedHeaders` 配置项表示允许的请求头,`allowCredentials` 配置项表示是否允许发送 Cookie 等凭据信息。
另外,如果你想为某个具体的路由配置跨域,可以使用 `RouteLocator` 对象进行配置,例如:
```java
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/api/**")
.filters(f -> f.cors().configurationSource(request -> {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.setAllowCredentials(true);
return corsConfiguration;
}))
.uri("http://localhost:8080"))
.build();
}
```
这段代码中,我们为 `/api/**` 路径配置了跨域规则,允许所有来源、所有方法、所有请求头,并允许发送凭据信息。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)