代码之家  ›  专栏  ›  技术社区  ›  Faling Dutchman

节点JS“DEPTH_ZERO_SELF_SIGNED_CERT”

  •  5
  • Faling Dutchman  · 技术社区  · 9 年前

    当我尝试连接到带有节点的Web服务器时,会出现上述错误。js客户端我现在正在写。

    但当我尝试连接到服务器时,我遇到了一个错误。

    'DEPTH_ZERO_SELF_SIGNED_CERT'

    这是发送请求的代码。并且关闭证书检查不是有效的选项。我需要确保连接安全。

    var options = 
        {
            hostname: baseUri,
            port: 443,
            path: 'oauth',
            method: 'POST',
            requestCert: true,
            ca:fs.readFileSync('Comodo_PositiveSSL_bundle.crt'),
            rejectUnauthorized: true,
            headers: {
                //'User-Agent': USER_AGENT,
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': data.length
            }
        };
    
        var req = http.request(options, (res) => {
          console.log('statusCode: ', res.statusCode);
          console.log('headers: ', res.headers);
    
          res.on('data', (d) => {
            process.stdout.write(d);
          });
        });
    
        req.write(data)
        req.end();
    
        req.on('error', (e) => {
          console.error(e);
        });
    

    如果有人能告诉我如何正确设置认证,这样就不会产生错误?


    证书包的图片。正如您所看到的,捆绑包只包含根证书,需要验证服务器自己提供的证书。

    Cert bundle

    3 回复  |  直到 9 年前
        1
  •  2
  •   Nallamachu    4 年前

    此解决方案与发布的问题无关,但错误代码为DEPTH_ZERO_SELF_SIGNED_CERT。

    下面的代码抛出了相同的错误代码

    const https = require('https');
    
    https.get('https://10.20.30.40:1234/v1/objects/storages', (res) => {
      console.log('statusCode:', res.statusCode);
      console.log('headers:', res.headers);
    
      res.on('data', (d) => {
        process.stdout.write(d);
      });
    
    }).on('error', (e) => {
      console.error(e);
    });
    

    为了解决这个问题,我们必须申请 process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0" 在https.get()中。请看下面几行中的问题修复代码。

    const https = require('https');
    
    https.get('https://10.20.30.40:1234/v1/objects/storages',
        process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0", (res) => {
      console.log('statusCode:', res.statusCode);
      console.log('headers:', res.headers);
    
      res.on('data', (d) => {
        process.stdout.write(d);
      });
    
    }).on('error', (e) => {
      console.error(e);
    });
    
        2
  •  0
  •   Faling Dutchman    9 年前

    下面的代码解决了这个问题。我已经添加了 options.agent = http.Agent(options); 这就解决了问题。

    因此,对于所有试图向安全服务器进行身份验证并遇到上述错误的人来说,这可能是您的解决方案。下面我添加了上面的编辑代码。

    var options = 
            {
                hostname: baseUri,
                port: 443,
                path: 'oauth',
                method: 'POST',
                requestCert: true,
                headers: {
                    //'User-Agent': USER_AGENT,
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': data.length
                }
            };
            options.agent = http.Agent(options);
    
            var req = http.request(options, (res) => {
              console.log('statusCode: ', res.statusCode);
              console.log('headers: ', res.headers);
    
              res.on('data', (d) => {
                process.stdout.write(d);
              });
            });
    
            req.write(data)
            req.end();
    
            req.on('error', (e) => {
              console.error(e);
            });
    
        3
  •  -1
  •   Anupam Mahapatra    7 年前

    npm配置设置严格的ssl false 为npm管理项目工作