使用react not working为Spring Cloud Gateway配置CORS



我有一个React应用程序,它使用Spring Cloud Gateway在本地运行。我得到这个错误:

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

如何解决此问题?

react应用程序面临同样的问题。似乎您缺少默认筛选器。我已经修复了在春季云网关中使用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云版本v2021.0.4

相关内容

  • 没有找到相关文章

最新更新