【问题标题】:Issue with CORS configuration on Spring Cloud GatewaySpring Cloud Gateway 上的 CORS 配置问题
【发布时间】:2019-06-15 06:49:17
【问题描述】:

我正在尝试使用 Spring Cloud Gateway 构建网关代理。

我可以使用 Spring Cloud 中的 RouteLocator 将我的请求路由到相应的服务。但我无法为通过 RouteLocator 路由的路径配置 CORS。

我尝试了 Spring Cloud 文档中提到的所有可能性。

我的网页出现以下错误:

我的代码看起来像 Application.java

@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();
}

}

application.yml

server:
  port: 8080

spring:
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "*"
            allowedMethods:
              - GET

buils.gradle

    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")
}

【问题讨论】:

  • 默认情况下,API Gateway 为所有源启用 CORS,如果您只想允许列入白名单的源,请使用 globalcors 配置。 spring: cloud: gateway: globalcors: corsConfigurations: '[/**]': allowedOrigins: "*" allowedMethods: - GET

标签: spring cors spring-cloud-gateway


【解决方案1】:

同样的问题。 使用 Ilford (Spring Cloud 2020.0.1) 和 Spring Cloud Gateway 3.0.0

预检请求 (OPTIONS) 总是被配置拒绝

唯一的解决方案是按照here 所述,通过添加新 Bean 的代码来完成它

【讨论】:

    【解决方案2】:

    默认情况下,API Gateway 为所有来源启用 CORS,如果您只想允许列入白名单的来源,请使用 globalcors 配置。
    spring: cloud: gateway: globalcors: corsConfigurations: '[/**]': allowedOrigins: "*" allowedMethods: - GET

    【讨论】:

    • 您应该使用指定来源作为白名单,“*”表示任何来源:)
    猜你喜欢
    • 2020-06-22
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 2020-02-10
    • 2021-06-04
    • 2018-11-15
    相关资源
    最近更新 更多