Node JS REST调用错误:证书链中的自签名证书

时间:2017-06-21 17:31:15

标签: node.js rest npm ssl-certificate

我正在尝试调用其他应用程序的安全REST服务。还提供客户端证书,但收到如下错误:

        request function
        Response:  IncomingMessage {
          _readableState: 
           ReadableState {
             objectMode: false,
             highWaterMark: 16384,
             buffer: BufferList { head: null, tail: null, length: 0 },
             length: 0,
        .........
        authorized: false,
             **authorizationError: 'SELF_SIGNED_CERT_IN_CHAIN',**
             encrypted: true,
    ..........

我尝试使用postman和rest客户端测试它并获得响应。 但是没有通过上面的Node JS程序/代码。 因此,根据我的理解,由于SSL拦截代理,这种情况正在发生; npm检测到此并抱怨。

我已经在Node JS应用程序中使用POST方法实现了Rest客户端以使用REST服务,如下所示。

    var https = require('https');
    var fs = require('fs');'

    process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

    var options = {
    host: '<HOSTNAME>',
    port: '<PORT>',
    path: '<PATH>',
    method: 'POST',
    headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': '',
    },
    ca: [ fs.readFileSync('<.jks file/ certificate name>') ],
    checkServerIdentity: function (host, cert) {
        },
     rejectUnauthorized:false,
    body: ''
    };

    var req = https.request(options, function(res) {
    console.log('request function')
    console.log("Response: ", res);
    res.on('data', function(d) {
    '' +
    '?' });
    });

    req.end();

    req.on('error', function(e) {
    console.error(e);
    });

我尝试了其他解决方案      “npm config set strict-ssl false”命令但无法正常工作。

我也试过了     rejectUnauthorized:false和process.env.NODE_TLS_REJECT_UNAUTHORIZED =“0”     你可以在我的代码中看到,但到目前为止没有任何工作。

Few required entries in .npmrc file are as below:
registry=https://registry.npmjs.org/
strict-ssl=false
cafile= <certificate path>

我尝试了所有可能性,但如上所述收到错误,请提示我是否遗漏了任何内容或需要更新任何内容。谢谢。

1 个答案:

答案 0 :(得分:0)

我找到的一个解决方案是我可以忽略主机名检查以使用Node JS来使用Rest服务,但是无法找到如何使用上面的Node JS代码忽略主机名。

相关问题