【问题标题】:Configure CORS for Spring Cloud Gateway using react not working使用反应为 Spring Cloud Gateway 配置 CORS 不起作用
【发布时间】:2022-10-19 18:03:32
【问题描述】:

我有一个使用 Spring Cloud Gateway 在本地运行的 React 应用程序。我收到此错误:

adminLogin:1 Access to XMLHttpRequest at 'http://localhost:9191/admin/authenticate' from origin 'http://localhost:3000' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:3000, http://localhost:3000', but only one is allowed.

9191是我的云网关端口号 我的云网关yml文件:

server:
  port: 9191

spring:
  application:
    name: API-GATEWAY
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "http://localhost:3000/"
            allowedMethods:
            - GET
            - POST
            - PUT
            - DELETE
        
      routes:
        - id: ADMIN-SERVICE
          uri: lb://ADMIN-SERVICE
          predicates:
            - Path=/admin/**
        - id: CUSTOMER-SERVICE
          uri: lb://CUSTOMER-SERVICE
          predicates:
            - Path=/customer/**
        - id: DEALER-SERVICE
          uri: lb://DEALER-SERVICE
          predicates:
            - Path=/dealer/**
        - id: VENUE-SERVICE
          uri: lb://VENUE-SERVICE
          predicates:
            - Path=/venue/**
      

eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    hostname: localhost

我该如何解决这个问题?

【问题讨论】:

    标签: java reactjs spring spring-boot spring-cloud-gateway


    【解决方案1】:

    反应应用程序面临同样的问题。好像您缺少默认过滤器。 我已经在 spring 云网关中使用以下配置修复了 cors :-

        spring:
      cloud:
        gateway:
          default-filters:
            - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
          globalcors:
            corsConfigurations:
              '[/**]':
                allowedOrigins: "*"
                allowedMethods: "*"
                allowedHeaders: "*"
          routes:
            - id: portalModule
    

    Spring-Boot v2.7.4, Java v11, spring-cloud-version v2021.0.4

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 2019-06-15
      • 2021-11-19
      • 2018-11-14
      • 2021-03-27
      • 2019-08-16
      • 2020-06-22
      相关资源
      最近更新 更多