2017-06-21 91 views
0

我正在尝试调用其他应用程序的安全REST服务。还提供了客户端证书,但如下得到错误:节点JS 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, 
    .......... 

我试着用邮递员,得到响应REST客户端进行测试。 但不能通过上面的节点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配置设定了严格的SSL假”的命令,但不能工作。

我也试图与 rejectUnauthorized:虚假和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> 

我试过所有的可能性,但得到错误提及,请建议如果我错过任何东西或需要更新任何东西。谢谢。

回答

0

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