Spring云网关上的CORS配置问题

Spring云网关上的CORS配置问题,spring,cors,spring-cloud-gateway,Spring,Cors,Spring Cloud Gateway,我正在尝试使用SpringCloudGateway构建网关代理 我能够使用SpringCloud中的RouteLocator将请求路由到相应的服务。但我无法为通过RouteLocator路由的路径配置CORS 我尝试了SpringCloud文档中提到的所有可能性 我的网页中出现以下错误: 我的代码看起来像 Application.java application.yml 格拉德尔大厦 默认情况下,API网关为所有来源启用CORS,如果您希望只允许白名单来源,则使用globalcors配置。 春

我正在尝试使用SpringCloudGateway构建网关代理

我能够使用SpringCloud中的RouteLocator将请求路由到相应的服务。但我无法为通过RouteLocator路由的路径配置CORS

我尝试了SpringCloud文档中提到的所有可能性

我的网页中出现以下错误:

我的代码看起来像 Application.java

application.yml

格拉德尔大厦


默认情况下,API网关为所有来源启用CORS,如果您希望只允许白名单来源,则使用globalcors配置。 春天: 云: 网关: 环球公司: 公司配置: '[/**]': 允许的来源:* 允许的方法: -这里有同样的问题。 使用Ilford Spring Cloud 2020.0.1 和Spring cloud Gateway 3.0.0

飞行前请求选项始终被配置拒绝


唯一的解决方案是通过代码添加一个新的Bean,如所述

默认情况下,API网关为所有源代码启用CORS,如果您希望只允许白名单源代码,则使用globalcors配置。spring:cloud:gateway:globalcors:CorsConfiguration:“[/**]”:AllowedOriginates:*allowedMethods:-Get您应该为白名单使用指定的原点,*表示任何原点:
@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

// tag::route-locator[]
@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
    return builder.routes()
            .route(p -> p
                    .path("/order/**")
                    .filters(f -> f.setResponseHeader("Access-Control-Allow-Origin", "http://localhost:8081")
                    )
                    .uri("http://localhost:9090"))
            .route(p -> p
                    .path("/priority-model/selection/**")
                    .filters(f -> f.addResponseHeader("Access-Control-Allow-Origin", "http://localhost:8081")
                    )
                    .uri("http://localhost:9090"))
            .build();
}

}
server:
  port: 8080

spring:
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "*"
            allowedMethods:
              - GET
    buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'gs-gateway'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.SR2"
    }
}

dependencies {
    compile("org.springframework.cloud:spring-cloud-starter-gateway")
    compile("org.springframework.cloud:spring-cloud-starter-netflix-hystrix")
    compile("org.springframework.cloud:spring-cloud-starter-contract-stub-runner"){
        exclude group: "org.springframework.boot", module: "spring-boot-starter-web"
    }
    compile group: 'org.springframework.cloud', name: 'spring-cloud-gateway-webflux', version: '2.0.2.RELEASE'
    testCompile("org.springframework.boot:spring-boot-starter-test")
}