【问题标题】:Disable SSL in Axios在 Axios 中禁用 SSL
【发布时间】:2020-08-05 17:27:25
【问题描述】:

有没有一种简单的方法可以在 Axios 中禁用 SSL 验证。我试过这个process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';,但它不起作用。

这是我的代码示例”

const postPosts = () => {
  axios
    .post("https://xxx.dev.lab", {
      Username: "xxx",
      Password: "xxx"
    })
    .then(response => {
      console.log(response);
    })
    .catch(error => {
      console.error(error);
    });
};
postPosts();

【问题讨论】:

标签: javascript axios


【解决方案1】:

到目前为止,Axios 还没有解决这种情况 - 你可以尝试:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

但这是一个非常糟糕的主意,因为它会在整个节点服务器上禁用 SSL..

或者您可以将 axios 配置为使用自定义代理并将该代理的 rejectUnauthorized 设置为 false,如此处所述

示例:

// At instance level
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?我添加了导入 import https = require('https'); 但我的 TypeScript 找不到它
  • 从 'https' 导入 * 作为 https;你应该导入这个
  • 我需要在哪里使用此代码?我是否需要编写中间件节点服务器才能使其正常工作?
【解决方案2】:

[如果您在 Docker 中运行您的应用程序]

我在我的项目中通过 2 个步骤解决了这个问题:

1.更改 Docker SSL 设置

我在容器内编辑了/etc/ssl/openssl.cnf

替换字符串:

TLSv1.2 => TLSv1
SECLEVEL=2 => SECLEVEL=1

2。为您的请求设置最低 TLS 版本

import * as https from 'https';

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

axios.post(
  "https://xxx.dev.lab", 
  { Username: "xxx", Password: "xxx" },
  { httpsAgent: agent }
)

【讨论】:

    猜你喜欢
    • 2018-05-17
    • 2021-07-24
    • 2017-04-22
    • 2016-09-12
    • 2020-07-12
    • 2019-03-02
    • 2011-01-29
    • 1970-01-01
    • 2016-02-28
    相关资源
    最近更新 更多