簡體   English   中英

在React中通過axios調用API(帶有自簽名證書)時發生CORS問題

[英]CORS issue when calling API (with a self-signed certificate) via axios in React

我將我的React Web應用程序托管在一個群集https://example1.com AWS上,並且我試圖通過React中的axios調用我的API,該API托管在另一個群集https://example2.com/api/login並且當前正在使用自簽名證書。 我想測試是否一切都可以在HTTPS上運行。 該API用Java編寫,並設置了Access-Control-Allow-Origin: *標頭。 調用API時收到CORS錯誤。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example2.com/api/login. (Reason: CORS request did not succeed).

當我也在https://localhost上測試我的React應用程序時,它也不起作用。

我的React Web應用程序是使用webpack捆綁在一起的。 對於本地開發,我使用webpack-dev-server,對於生產版本,Node.js服務器用於提供頁面。 Node.js服務器不是HTTPS。 通過Docker在自定義端口上部署https://example1.com上的Node.js服務器以進行生產構建。

其他外部API(例如Eventbrite,Meetup,Google地方信息和Google圖書)可以正常工作。

我需要在React / Node.js中進行任何特殊配置才能使其正常工作嗎?

當運行curl -X POST https://example2.com/api/login時,cURL也給出以下錯誤

curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle" of Certificate Authority (CA) public keys (CA certs). If the default bundle file isn't adequate, you can specify an alternate file using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in the bundle, the certificate verification probably failed due to a problem with the certificate (it might be expired, or the name might not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use the -k (or --insecure) option.

運行curl -X POST https://example2.com/api/login -k可以運行。

同樣,在郵遞員中,只有關閉SSL驗證后,我才能使API正常工作。

注意:當所有主機都位於HTTP上時,一切正常。

因此,瀏覽器阻止了對可疑服務器的請求。

解決方法可能是在https://example1.com上運行服務器,該服務器會將/api/login類的請求重定向到https://example2.com而忽略證書的錯誤。

但最好獲得受信任的證書。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM