Nodejs中用 https模块请求restful api遇到认证库不通过的问题

Nodejs中用 https模块请求restful api遇到认证库不通过的问题

连接参数代码:

var options = { host: ‘em1.localdomain’, port: 7799, path: ‘/em/cloud’, method: ‘GET’, rejectUnauthorized: false, headers: { ‘Authorization’ : ‘basic U1NBX1VTRVIxOldlbGNvbWUx’, ‘Accept’: ‘application/oracle.com.cloud.common.Cloud+json’ }

如果options不加rejectUnauthorized: false, 会提示:

ERROR SELF_SIGNED_CERT_IN_CHAIN

如果加了会提示:

Some issues detected while trying to authenticate the user ‘USER1’. This may be intermittent" .Failed to login using repository authentication for user: USER1"

谁知道怎么解决吗?


3 回复

Node.js 中使用 https 模块请求 RESTful API 遇到认证问题

背景

在使用 Node.js 的 https 模块进行 HTTPS 请求时,可能会遇到一些认证相关的问题。比如在尝试连接到一个自签名证书的服务器时,可能会遇到 SELF_SIGNED_CERT_IN_CHAIN 错误。为了解决这个问题,我们通常会在请求配置中添加 rejectUnauthorized: false,但这可能会导致其他认证问题。

问题描述

在这个例子中,我们遇到了以下问题:

  • 错误 1: 如果不设置 rejectUnauthorized: false,会提示 ERROR SELF_SIGNED_CERT_IN_CHAIN
  • 错误 2: 如果设置了 rejectUnauthorized: false,则会提示认证失败,例如 “Some issues detected while trying to authenticate the user ‘USER1’.”

示例代码

const https = require('https');

// 连接参数配置
var options = {
    host: 'em1.localdomain',
    port: 7799,
    path: '/em/cloud',
    method: 'GET',
    rejectUnauthorized: false, // 如果服务器使用自签名证书,则需要设置此选项
    headers: {
        'Authorization': 'basic U1NBX1VTRVIxOldlbGNvbWUx', // 基本认证头
        'Accept': 'application/oracle.com.cloud.common.Cloud+json' // 接受的内容类型
    }
};

// 发起 HTTPS 请求
const req = https.request(options, (res) => {
    let data = '';

    res.on('data', (chunk) => {
        data += chunk;
    });

    res.on('end', () => {
        console.log(JSON.parse(data));
    });
});

req.on('error', (e) => {
    console.error(`问题发生: ${e.message}`);
});

req.end();

解决方案

  1. 确保正确配置认证信息

    • 确认 Authorization 头中的基本认证字符串是否正确。
    • 确认 Accept 头是否与服务器期望的内容类型匹配。
  2. 处理自签名证书

    • 在开发环境中,可以临时设置 rejectUnauthorized: false 来绕过证书验证问题。但在生产环境中,建议使用有效的证书以提高安全性。
  3. 检查认证服务器

    • 如果问题仍然存在,可能是因为认证服务器本身有问题。请检查认证服务器的日志或文档,确认是否有任何特定的认证要求或限制。

希望这些步骤能帮助你解决认证问题。如果问题依然存在,建议联系 API 提供商获取进一步的帮助和支持。


如果options不加rejectUnauthorized: false, 会提示 ERROR SELF_SIGNED_CERT_IN_CHAIN

估计你们使用了自签名证书. rejectUnauthorized缺省为true, 即如果服务器端证书通不过验证, 则不使用服务器提供的服务了, 同时会给出SELF_SIGNED_CERT_IN_CHAIN错误. 官网的解释如下: rejectUnauthorized: If true, the server certificate is verified against the list of supplied CAs. An ‘error’ event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent. Default true.

如果加了会提示: Some issues detected while trying to authenticate the user 'USER1’. This may be intermittent" .Failed to login using repository authentication for user: USER1"

这表示你的认证信息(‘Authorization’ : 'basic U1NBX1VTRVIxOldlbGNvbWUx’)服务器已经接受了, 应该是服务器在处理认证的过程中出了错, 这个你需要和负责服务器Basic认证的人联系. 同时建议把认证信息中的basic改为Basic, 有些服务器比较挑剔. 另一个表明服务器接受了认证信息的证据是: 你的认证信息base64deconde之后为 SSAUSER1:Welcome1, 看起来你们的服务端还截取了SSA前缀部分, 从而给出了只包含了’USER1’的错误信息.

根据你的描述,你在使用 Node.js 的 https 模块请求 RESTful API 时遇到了认证问题。具体来说,在禁用证书验证 (rejectUnauthorized: false) 时可以成功连接,但认证失败;而启用证书验证时则提示证书错误。

这可能是因为服务器使用的是自签名证书,导致客户端无法验证其有效性。为了正确处理这种情况,你可以考虑以下几种方法:

方法一:使用 request

request 是一个流行的 HTTP 客户端库,支持自动处理认证和证书验证。首先需要安装 request

npm install request

然后使用它来发起请求:

const request = require('request');

const options = {
  url: 'https://em1.localdomain:7799/em/cloud',
  method: 'GET',
  headers: {
    'Authorization': 'basic U1NBX1VTRVIxOldlbGNvbWUx',
    'Accept': 'application/oracle.com.cloud.common.Cloud+json'
  }
};

request(options, (error, response, body) => {
  if (error) {
    console.error(error);
  } else {
    console.log(body);
  }
});

方法二:使用 https 模块并信任自签名证书

如果你坚持使用 https 模块,可以通过加载根证书的方式来信任自签名证书。你需要将服务器的自签名证书保存为文件(例如 ca.crt),然后将其内容加载到 https 请求选项中:

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

const options = {
  host: 'em1.localdomain',
  port: 7799,
  path: '/em/cloud',
  method: 'GET',
  rejectUnauthorized: true,
  ca: fs.readFileSync('ca.crt')
};

const req = https.request(options, res => {
  let data = '';

  res.on('data', chunk => {
    data += chunk;
  });

  res.on('end', () => {
    console.log(data);
  });
});

req.end();

确保你将 ca.crt 文件路径设置为正确的文件位置。这样可以信任该自签名证书,并且不需要禁用证书验证。

回到顶部