我试图从jira服务器下载一个文件使用URL,但我得到一个错误。 如何在代码中包含证书进行验证?

错误:

Error: unable to verify the first certificate in nodejs

at Error (native)
    at TLSSocket.<anonymous> (_tls_wrap.js:929:36)
   
  at TLSSocket.emit (events.js:104:17)

at TLSSocket._finishInit (_tls_wrap.js:460:8)

我的Nodejs代码:

var https = require("https");
var fs = require('fs');
var options = {
    host: 'jira.example.com',
    path: '/secure/attachment/206906/update.xlsx'
};

https.get(options, function (http_res) {
    
    var data = "";

  
    http_res.on("data", function (chunk) {
       
        data += chunk;
    });

   
    http_res.on("end", function () {
      
        var file = fs.createWriteStream("file.xlsx");
        data.pipe(file);
      
    });
});

当前回答

Axios Request:此问题的根本原因是,您的代码无法处理证书管理。要解决此问题,请添加以下代码。

import * as https from "https";

...

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

//现在将httpagent传递给axios请求。像下面。

const { data } = await axios.get(url, { httpsAgent });

其他回答

@sch提供的答案对我帮助很大。我还想补充一些东西。 在开发环境中工作时,您的SSL证书是由您自己的一个自签名证书颁发的(因此不存在中间证书),NODE_EXTRA_CA_CERTS环境变量需要引用这个自签名证书。自签名证书需保存为PEM格式。导出证书后,我做了以下操作:

set NODE_EXTRA_CA_CERTS=C:\rootCert.pem

(值得注意的是,我在Windows上运行node,到PEM的路径没有引用)

使用命令行中的{{node}},我能够确认是否已经解决了这个问题,通过调用:

https.get("https://my.dev-domain.local")

我可以通过mozilla或chrome等浏览器获得证书链。

打开网站,进入网页的证书设置,下载证书链作为文件名(first-chain. txt)。Pem, second-chain.pem),应该是Pem格式

——开始证书 MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB ...... ——结束证书 ——开始证书 MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB ...... ——结束证书

然后在你的nodejs代码中,我在typescript上做了,我添加了2个cas,因为我有2个webserver请求

从“HTTPS”导入HTTPS 从“ssl-root-cas”导入cas

......

扩展缓冲区 addFile(file: string): Buffer[] } const caList = cas.create()作为caList caList.addFile (process.env。` ` certs/first-chain.pem ` ` caList.addFile (process.env。'certs/second-chain.pem')

然后,当我需要使websocket wss连接,我添加代理与新cas列表请求

this.client。connect(KtUrl, undefined, undefined, undefined, { 代理:新的https。代理({ ca: caList }) })

还必须添加ssl-root-cas文件名ssl-root-cas.d的定义文件。这样打字稿就不会报错

声明模块'ssl-root-cas' { function create(): string | Buffer | (string | Buffer)[] | undefined }

对于无法验证nodejs中的第一个证书,需要拒绝未经授权的证书

 request({method: "GET", 
        "rejectUnauthorized": false, 
        "url": url,
        "headers" : {"Content-Type": "application/json",
        function(err,data,body) {
    }).pipe(
       fs.createWriteStream('file.html'));

尝试添加适当的根证书

这总是比盲目接受未经授权的端点要安全得多,后者只能作为最后的手段。

这可以像添加一样简单

require('https').globalAgent.options.ca = require('ssl-root-cas/latest').create();

到你的应用程序。

SSL根ca npm包(这里使用的)对于这个问题是一个非常有用的包。

这对我很有效

做下面的事情

如果你没有这些包https和axios

您可以通过 NPM安装——保存axios HTTPS

import axios from 'axios';
import https from 'https';
const httpsAgent = new https.Agent({
    rejectUnauthorized: false,
})

axios.defaults.httpsAgent = httpsAgent

这样做你就会得到你的回应。