【问题标题】:Getting ERR_CERT_AUTHORITY_INVALID with axios使用 axios 获取 ERR_CERT_AUTHORITY_INVALID
【发布时间】:2019-08-18 06:29:48
【问题描述】:

我正在尝试使用 vue-axios 通过 https 进行发布请求。 但是,由于我使用的是我创建的自签名证书,因此出现以下错误:

net::ERR_CERT_AUTHORITY_INVALID

经过搜索,我发现大多数人通过以下方式解决了这个问题

const instance = axios.create({
  httpsAgent: new https.Agent({  
    rejectUnauthorized: false
  })
});
instance.get('https://something.com/foo');

// At request level
const agent = new https.Agent({  
  rejectUnauthorized: false
});
axios.get('https://something.com/foo', { httpsAgent: agent });

我尝试了这两个选项,但都没有成功。 我为 https.Agent 使用了 npm https 模块。

有谁知道如何解决这个问题?还是我应该从 axios 更改为其他模块?

编辑:

我正在运行的代码段出现错误:

const axiosInstance = axios.create({
  baseURL: 'https://localhost:5000',
  httpsAgent: new https.Agent({
    rejectUnauthorized: false
  }),
});
axiosInstance.post('/user', LoginRequest, 
  { headers: { 'Content-Type': 'application/json' } })
    .then(response => this.assignLogin(response.data));

尝试更改为名为 needle 的模块并使用 https,但出现相同的错误:

针:

      const headers = { 'Content-Type': 'application/json' };
      const options = {
          method: 'POST',
          headers: headers,
          rejectUnauthorized: false,
          requestCert: true,
          agent: false,
          strictSSL: false,
      }
      needle.post('https://localhost:5000/user', LoginRequest, options).on('end', function() { })

https:

const options = {
         hostname: 'localhost',
         port: 5000,
         path: '/user',
         strictSSL: false,
         rejectUnauthorized: false,
         secureProtocol: 'TLSv1_method',
         method: 'POST',
         headers: {
           'Content-Type': 'application/json',
         },
       };
       const req = https.request(options, (res) => {
         console.log('statusCode:', res.statusCode);
         console.log('headers:', res.headers);

         res.on('data', (d) => {
           this.assignLogin(d);
         });
       });
       req.on('error', (e) => {
         console.error(e);
       });
       req.write(LoginRequest);
       req.end();

【问题讨论】:

  • 你的代码在哪里?
  • 在上面添加了axios片
  • 如果您要通过所有这些麻烦来允许无效证书,为什么不在开发时以 http 模式运行您的服务器,然后在生产时获得与域匹配的有效证书?跨度>
  • axios 的文档中说:“httpAgenthttpsAgent 分别定义了在执行 http 和 https 请求时使用的自定义代理,在 node.js 中.”。我已经看到他们在文档中区分浏览器和节点,所以这个配置可能不适合浏览器(Vue)。至少它对我也不起作用。

标签: vue.js vuejs2 axios


【解决方案1】:

由于您提到您“使用您创建的自签名证书”,我猜您正在使用它进行本地开发测试。在 Chrome 中进行本地测试时,我遇到了类似的问题。

由于此错误消息 (net::ERR_CERT_AUTHORITY_INVALID) 是 Chrome 阻止具有“不安全”证书的 URL 的一种方式,因此您需要通过 Chrome 解决此问题,告诉它您信任该证书。
我使用的解决方案是旧的thisisunsafe。 (仅当您真的信任证书时才使用此解决方案,即,这是您自己的证书):

解决方案:只需在 Chrome 中打开一个标签,尝试使用您的服务器地址(在您的情况下为 https://localhost:5000/)打开一个 URL。 Chrome 将显示一条警告消息,因此您可以单击窗口中的任意位置,然后输入thisisunsafe。 Chrome 现在将允许访问此证书。当您再次重新加载客户端并尝试请求服务器时,它将起作用。

【讨论】:

猜你喜欢
  • 2018-09-22
  • 2022-01-04
  • 2018-11-14
  • 2021-02-21
  • 2022-06-12
  • 2020-08-21
  • 2023-03-23
  • 2017-06-02
  • 2018-10-13
相关资源
最近更新 更多