windows 环境下使用 Node.js 遇到 unable to get local issuer certificate

JerryWang_汪子熙 -
windows 环境下使用 Node.js 遇到 unable to get local issuer certificate

我有一个 OData 服务,部署在 SAP S/4HANA 服务器上,浏览器里通过如下 url,可以正常访问到其元数据:

我编写了一个 node.js 应用,通过编程的方式访问这个 OData 服务的元数据:

var request = require('request');

var url = 'https://ldai2xxx:44356/sap/opu/odata/sap/ZBOOK_MANAGE_SRV/$metadata';

var oOptions = {
        url: url,
        method: 'GET'
    };

var oPromise = new Promise(function(resolve,reject){
    request.get(oOptions,function(error,response,body){
        if(error){
              console.log("error occurred: " + error);
              reject(error);
        }
        resolve(body);
    });
});

oPromise.then((data) => console.log(data));

使用 node 执行上面的代码,遇到如下错误消息:

error occurred: Error: unable to get local issuer certificate
(node:33376) UnhandledPromiseRejectionWarning: Error: unable to get local issuer certificate
at TLSSocket.onConnectSecure (_tls_wrap.js:1497:34)
at TLSSocket.emit (events.js:315:20)
at TLSSocket._finishInit (_tls_wrap.js:932:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:706:12)

(Use node --trace-warnings ... to show where the warning was created)
(node:33376) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.ht...). (rejection id: 1)
(node:33376) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

在应用程序生成过程中,当用户连接的系统使用 SSL 支持安全 HTTPS 流量时,可能会出现无效安全证书错误。 在某些情况下,证书是使用用户操作系统未知的本地证书颁发机构生成的。 如果发生这种情况,应用程序生成器会拒绝连接请求并报告错误。

解决方案

windows 环境下设置环境变量,使用 set 语法,命名后不加空格,直接附上两个 &&, 然后空格,跟上新的命令。

set NODE_TLS_REJECT_UNAUTHORIZED=0&& node ag3.js

这种解决方案的用意是,设置环境变量 NODE_TLS_REJECT_UNAUTHORIZED 的值为 0,暂时禁用 SSL certificate 的校验。

执行之后,看到警告消息:

Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.

提示我们,这个环境变量的设置,禁掉了 certificate 验证,让 TLS 连接和 HTTPS 请求变得不再安全。

收到 401 状态码,说明与服务器的连接终于通了:

特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

Tags 标签

node.jsexpress后端sapsaprfc

扩展阅读

加个好友,技术交流

1628738909466805.jpg