Gor*_*nov 15 ssl node.js reactjs axios
我正在尝试使用axios在我的反应应用程序中使用API.API使用自签名证书通过HTTPS工作.到目前为止,连接时出现以下错误:
net::ERR_INSECURE_RESPONSE
bundle.js:65253 HTTP Failure in Axios Error: Network Error
at createError (bundle.js:2188)
at XMLHttpRequest.handleError (bundle.js:1717)
Run Code Online (Sandbox Code Playgroud)
我尝试了以下但没有成功:
import axios from 'axios';
const https = require('https');
const agent = new https.Agent({
rejectUnauthorized: false,
});
const client = axios.create({ //all axios can be used, shown in axios documentation
baseURL: process.env.REACT_APP_API_URL,
responseType: 'json',
withCredentials: true,
httpsAgent: agent
});
export default client;
Run Code Online (Sandbox Code Playgroud)
有没有办法禁用证书验证?
这些似乎都没有真正回答这个问题。问题不在于如何自签名证书或如何禁用浏览器中的安全性。问题是:特别是对于 axios,如何禁用 SSL 验证?
这应该与向 cURL 命令添加-k或标记相同。--insecure如果你可以用 cURL 做到这一点,那么用 axios 也应该可以做到。
请注意,这不会禁用加密,因为如果您正确设置了 https,服务器仍然可以控制加密。它只是禁止检查您是否正在与正确的服务器通信。
无论如何,就我而言,我通过更改自己解决了这个问题:
const agent = new https.Agent({
rejectUnauthorized: false,
});
Run Code Online (Sandbox Code Playgroud)
到
const agent = new https.Agent({
rejectUnauthorized: false,
requestCert: false,
agent: false,
});
Run Code Online (Sandbox Code Playgroud)
以下是Philippe Sultan 链接的类似问题的答案。我实际上不明白每个选项各自的作用。如果有人能在评论中回答那就太好了。
这是一种拥有自签名和接受的本地主机证书的方法。基本上,您需要生成证书并在使用之前手动将其添加到 Chrome。
Let's Encrypt 的指南: https: //letsencrypt.org/docs/certificates-for-localhost/(似乎有点高级)。
还有这个答案,解释了如何在站点上添加证书:/sf/answers/1302194211/
| 归档时间: |
|
| 查看次数: |
9826 次 |
| 最近记录: |