我有一个简单的 spring-cloud-gateway-mvc 网关,并通过配置对 cors 进行了配置,但是不生效。配置如下:
spring:
cloud:
gateway:
mvc:
routes:
- id: api-routes
uri: http://localhost:8081
predicates:
- Path=/api/v1.0/access-token,/api/v1.0/inquiry
- Method=POST,OPTIONS
filters:
- name: DedupeResponseHeader # Remove duplicate headers
args:
name: Access-Control-Allow-Credentials Access-Control-Allow-Origin
- name: StripPrefix # Remove the prefix '/my-gateway' from the request
args:
parts: 1
- name: PrefixPath # Add the prefix '/api-server' to the request
args:
prefix: /api-server
metadata:
cors:
allowedOrigins: '*'
allowedMethods: '*'
allowedHeaders: '*'
allowCredentials: true
maxAge: 30
我也试过按照 Spring 中文文档 中提到的方法添加 cors 配置,但还是不行。
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("**/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
}
OPTIONS 预检请求始终响应 403 Forbidden: