【Spring Gateway】 CORS Configuration

Metadata

title: 【Spring Gateway】 CORS Configuration
date: 2023-02-14 10:11
tags:
  - 行动阶段/完成
  - 主题场景/组件
  - 笔记空间/KnowladgeSpace/ProgramSpace/ModuleSpace
  - 细化主题/Module/SpringGateway
categories:
  - SpringGateway
keywords:
  - SpringGateway
description: 【Spring Gateway】 CORS Configuration

【Spring Gateway】 CORS Configuration

您可以配置网关来控制CORS行为。“全局”CORS配置是一个URL模式到Spring Framework CorsConfiguration的映射。配置CORS的示例如下:

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "https://docs.spring.io"
            allowedMethods:
            - GET

在前面的示例中,对于所有GET请求的路径,允许来自docs.spring.io的请求的CORS请求。
要为没有由某个网关路由谓词处理的请求提供相同的CORS配置,请设置spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping属性为true。当您尝试支持CORS预飞请求,而由于HTTP方法是选项,所以路由谓词的计算结果不为true时,这很有用。