繁体   English   中英

SSL 证书 - 在 axios 中禁用验证并做出反应

[英]SSL certificate - disable verification in axios and react

我正在尝试使用 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)

我尝试了以下但没有成功:

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;

有没有办法禁用证书验证?

首先,原因错误不在您的代码中。 这是因为您的浏览器的安全限制。 默认情况下,浏览器将阻止对自签名的请求,因为它不是来自有效证书颁发机构 (CA) 的证书。

从 CA 升级 SSL 证书,或者您需要在浏览器中禁用网络安全。

例如,对于 chrome 以 cmd 行 args 开头,如下所示

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --disable-gpu --user-data-dir=~/chromeTemp

这是一种获得自签名和接受的本地主机证书的方法。 基本上,您生成证书并在使用之前手动将其添加到 Chrome。

Let's Encrypt 的指南: https : //letsencrypt.org/docs/certificates-for-localhost/ (似乎有点高级)。

也有这个答案,它解释了如何在网站上添加证书: https : //stackoverflow.com/a/18602774

这些似乎都没有真正回答这个问题。 问题不在于如何自签名证书或如何禁用浏览器中的安全性。 问题是:特别是使用 axios,您如何禁用 SSL 验证?

这应该与在 cURL 命令中添加-k--insecure标志相同。 如果你可以用 cURL 来做,那么它应该可以用 axios 来做。

请注意,这不会禁用加密,因为如果您正确设置了 https,服务器仍然可以控制它。 它只是禁用检查您是否正在与正确的服务器交谈。

无论如何,就我而言,我通过更改为自己解决了这个问题:

const agent = new https.Agent({
    rejectUnauthorized: false,
});

const agent = new https.Agent({
   rejectUnauthorized: false,
   requestCert: false,
   agent: false,
});

按照Philippe Sultan 链接的类似问题的答案 我实际上并没有得到每个选项单独的作用。 如果有人可以在评论中回答,那就太好了。

您始终可以使用“ 让我们加密”来获取签名的SSL证书,然后在Web服务器上使用它。 据我所知(如果我错了,请纠正我)是当存在自签名SSL证书时,Chrome Web浏览器阻止您访问请求的资源。 它与Axios没有任何关系。

我正在尝试使用axios在我的React应用程序中使用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)

我尝试了以下方法,但没有成功:

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;

有什么方法可以禁用证书验证吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM