简体   繁体   中英

Node JS REST call Error: self signed certificate in certificate chain

I am trying to call secure REST service of other application. Also providing client certificate, but getting error as below:

        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,
    ..........

I tried to test it with rest client using postman and getting the response. But not working through above Node JS program/ code. So as per my understanding this is happening due to SSL-intercepting proxy; npm detects this and complains.

I have implemented the Rest client in the Node JS application with POST method to consume the REST service is as below.

    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);
    });

I tried other solutions like "npm config set strict-ssl false" command but could not work.

I also tried with rejectUnauthorized:false and process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0" as you can see in my code but nothing is working so far.

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

I tried all the possibilities but getting error as mentioned, please suggest if I missed anything or need to update anything. Thank you.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM