【问题标题】:https.request ignoring rejectUnauthorizedhttps.request 忽略拒绝未授权
【发布时间】:2015-04-06 16:02:07
【问题描述】:

我正在尝试使用 nodejs 0.12 连接到远程服务器,但我不断收到 SELF_SIGNED_CERT_IN_CHAIN 响应。我看过类似的问题12,但不知何故,他们的解决方案在我的服务器上不起作用。

我正在连接到一个不受我控制且使用自签名证书设置的测试环境。这是我的要求:

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

start();

function start()
{
    var listadebancos = 
    {
        language:"es",
        command:"GET_BANKS_LIST",

        merchant:
        {
            apiLogin:"111111111111111",
            apiKey:"11111111111111111111111111",
        },

        test:true,
        bankListInformation:
        {
            paymentMethod:"PSE",
            paymentCountry:"CO"

        }
    };

    var listadebancosString = JSON.stringify(listadebancos);

    var headers = 
    {
        'Content-Type': 'application/json',
        'Content-Length': listadebancosString.length
    };

        var options= {
            host: 'stg.api.payulatam.com',
            rejectUnauthorized: false,
            agent:false,
            path: '/payments-api/4.0/service.cgi',
            method: 'POST',
            cert: fs.readFileSync('./stg.gateway.payulatam.crt'),

        }

        process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

        var req= https.request(options, funcionRespuesta);
        req.write(listadebancosString); 
        req.end();



        function funcionRespuesta(res) 
        {   console.log(res);
        }

    }

我错过了什么明显的东西吗?

【问题讨论】:

  • 删除和撤销 api 密钥。您需要让证书颁发者修复证书或告诉您的 http 代码忽略该问题。我不知道如何使用 stock http 模块来做到这一点;但是使用request,您可以传递一个选项strictSSL: false。https.request 中的证书选项也是一个客户端证书。
  • 我决定使用另一个名为 needle 的库,它解决了这个问题。无论如何感谢您的建议
  • 老兄,你的代码中仍然有一个与金融相关的 api 密钥,我将为你编辑它,但它仍然在编辑历史中永远对互联网上的每个人可见,所以 REVOKE尽快!!
  • 干杯!不用担心,这是公共测试密钥 :)

标签: javascript node.js ssl


【解决方案1】:

我决定使用库调用 needle 来发出请求,这一次我能够收到没有 SSL 错误的响应。以防万一有人遇到同样的情况,这里是我使用的代码:

var listadebancos = 
{
    "language":"es",
       "command":"GET_BANKS_LIST",
       "merchant":{
          "apiLogin:"111111111111111",
          "apiKey:"11111111111111111111111111",
       },
       "test":false,
       "bankListInformation":{
          "paymentMethod":"PSE",
          "paymentCountry":"CO"
       }
};

};

// var listadebancosString = JSON.stringify(listadebancos);

var headers = 
{
    'Content-Type': 'application/json'
};

    var options = {
        host: 'stg.api.payulatam.com',
        **json:true,**
        path: '/payments-api/4.0/service.cgi',
        method: 'GET',
        headers: headers,
        rejectUnauthorized: false,
        requestCert: true,
        agent: false,
        strictSSL: false,
    }       
    needle
      .post('stg.api.payulatam.com/payments-api/4.0/service.cgi',listadebancos, options, funcionRespuesta)
       .on('end', function() {
        console.log('Ready-o, friend-o.');
      })


    function funcionRespuesta(err, resp, body)
    {
        console.log(err);
        console.log(body);                  
    }

【讨论】:

    猜你喜欢
    • 2011-04-21
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    相关资源
    最近更新 更多