gpt4 book ai didi

spring - 为 Spring Cloud Gateway 配置 CORS 策略

转载 作者:行者123 更新时间:2023-12-05 01:05:37 25 4
gpt4 key购买 nike

我在单独的服务器上运行 Spring Cloud 网关,配置如下:

spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/*]': (I also tried '[/**]':)
allowedOrigins: "http://localhost:3000"
allowedMethods:
- GET
- POST

但每次从 React 应用程序我得到:

Access to XMLHttpRequest at 'http://11.1.1.1:8080/api/support/tickets/create' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

您知道 Spring Boot 2.6.2/Spring cloud 2021.0.0 如何解决此问题吗?

完整代码:http://www.github.com/rcbandit111/Spring_Cloud_Gateway_POC

POST 请求:

Request URL: http://1.1.1.1:8080/api/merchants/onboarding/bank_details
Referrer Policy: strict-origin-when-cross-origin
Accept: application/json, text/plain, */*
Content-Type: application/json
Referer: http://localhost:3000/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Post request Payload:
{"currency":"HNL","iban":"dssvsdvsdv"}

OPTIONS 请求:

Request URL: http://1.1.1.1:8080/api/merchants/onboarding/bank_details
Request Method: OPTIONS
Status Code: 403 Forbidden
Remote Address: 1.1.1.1:8080
Referrer Policy: strict-origin-when-cross-origin

回复:

HTTP/1.1 403 Forbidden
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
content-length: 0

OPTIONS 请求 header :

OPTIONS /api/merchants/onboarding/bank_details HTTP/1.1
Host: 1.1.1.1:8080
Connection: keep-alive
Accept: */*
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36
Sec-Fetch-Mode: cors
Referer: http://localhost:3000/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9

最佳答案

您是否正在通过 --proxy-conf 运行您的 FE 应用程序来重定向?我不是精通FE,但不要使用changeOrigin:true。如果您必须使用 changeOrigin: true,它只适用于 GET,而对于其他人,您可能需要执行类似 this 的操作.要使用 proxy-conf,我们通常有一个 proxy.conf.json 类似这样的内容:

{
"/api/*": {
"target": "http://external-gateway-url",
"secure": false,
"logLevel": "debug"
}
}

然后在运行应用程序时使用 --proxy-config proxy.conf.json。我的 FE 知识已经过时了。您可能希望看起来像 this .

如果不是,并且调用是直接的,那么网关中的以下配置(代理也需要)应该可以工作:

spring:
cloud:
gateway:
globalcors:
corsConfigurations:
'[/**]':
allowedOrigins: "http://localhost:3000"
allowedHeaders: "*"
allowedMethods:
- GET
- POST

关于spring - 为 Spring Cloud Gateway 配置 CORS 策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70561543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com