16. CORS 配置
您可以配置网关以控制 CORS 行为。“全局” CORS 配置是 URL 模式到Spring 框架CorsConfiguration
.
以下示例配置 CORS:
例 72.application.yml
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "https://docs.spring.io"
allowedMethods:
- GET
在前面的示例中,允许来自以下来源的请求的 CORS 请求docs.spring.io
对于所有 GET 请求的路径。
要为某些网关路由谓词未处理的请求提供相同的 CORS 配置,请将spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping
property 设置为true
.
当您尝试支持 CORS 预检请求,并且路由谓词未评估为true
因为 HTTP 方法是options
.