Tho*_*ggi 118 javascript ssl request node.js
我正在使用node.js request.js来访问api.我收到了这个错误
[错误:UNABLE_TO_VERIFY_LEAF_SIGNATURE]
我的所有凭据都是准确有效的,服务器也很好.我跟邮递员提出了同样的要求.
request({
"url": domain+"/api/orders/originator/"+id,
"method": "GET",
"headers":{
"X-API-VERSION": 1,
"X-API-KEY": key
},
}, function(err, response, body){
console.log(err);
console.log(response);
console.log(body);
});
Run Code Online (Sandbox Code Playgroud)
此代码只是在可执行脚本ex中运行.node ./run_file.js那是为什么?它需要在服务器上运行吗?
Tho*_*ggi 137
注意:以下内容很危险,并允许在客户端和服务器之间拦截和修改API内容.
这也有效
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
And*_*nak 84
这不是应用程序的问题,而是由中间CA签署的证书.如果您接受该事实并仍想继续,请将以下内容添加到请求选项中:
rejectUnauthorized: false
Run Code Online (Sandbox Code Playgroud)
完整要求:
request({
"rejectUnauthorized": false,
"url": domain+"/api/orders/originator/"+id,
"method": "GET",
"headers":{
"X-API-VERSION": 1,
"X-API-KEY": key
},
}, function(err, response, body){
console.log(err);
console.log(response);
console.log(body);
});
Run Code Online (Sandbox Code Playgroud)
Coo*_*J86 71
安全的解决方案是向链中添加必要的证书.首先从npm 安装ssl-root-cas包:
npm install ssl-root-cas
Run Code Online (Sandbox Code Playgroud)
此程序包包含许多浏览器信任的中间证书,但节点不支持.
var sslRootCAs = require('ssl-root-cas/latest')
sslRootCAs.inject()
Run Code Online (Sandbox Code Playgroud)
将添加缺少的证书.有关详情,请参阅此处:
https://github.com/coolaj86/node-ssl-root-cas
Fer*_*ntl 42
CoolAJ86的解决方案是正确的,它不会损害您的安全性,例如使用rejectUnauthorized或禁用所有检查NODE_TLS_REJECT_UNAUTHORIZED.但是,您可能需要明确注入额外的CA证书.
我首先尝试了ssl-root-cas模块包含的根CA :
require('ssl-root-cas/latest')
.inject();
Run Code Online (Sandbox Code Playgroud)
我仍然最终得到了UNABLE_TO_VERIFY_LEAF_SIGNATURE错误.然后我发现谁为我通过COMODO SSL Analyzer连接的网站颁发了证书,下载了该权限的证书,并试图只添加那个:
require('ssl-root-cas/latest')
.addFile(__dirname + '/comodohigh-assurancesecureserverca.crt');
Run Code Online (Sandbox Code Playgroud)
我最后又出现了另一个错误:CERT_UNTRUSTED.最后,我注入了额外的根CA并包含了"my"(显然是中间的)CA,其工作原理如下:
require('ssl-root-cas/latest')
.inject()
.addFile(__dirname + '/comodohigh-assurancesecureserverca.crt');
Run Code Online (Sandbox Code Playgroud)
Tom*_*uer 16
对于Create React App(也发生此错误并且此问题是 #1 Google 结果),您可能正在使用HTTPS=true npm start和proxy(in package.json),它在开发时转到某些 HTTPS API,该 API 本身是自签名的。
如果是这种情况,请考虑proxy像这样更改:
"proxy": {
"/api": {
"target": "https://localhost:5001",
"secure": false
}
}
Run Code Online (Sandbox Code Playgroud)
secure 决定 WebPack 代理是否检查证书链并禁用以确保 API 自签名证书未经过验证,以便您获取数据。
做rejectUnauthorized: false或process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';不做可能很诱人!它使您暴露在中间人攻击面前。
其他答案是正确的,因为问题在于您的证书“由中间 CA 签名”。对此有一个简单的解决方案,它不需要第三方库,例如ssl-root-cas或将任何额外的 CA 注入节点。
节点中的大多数 https 客户端都支持允许您为每个请求指定 CA 的选项,这将解析UNABLE_TO_VERIFY_LEAF_SIGNATURE. 这是一个使用 node 内置https模块的简单示例。
import https from 'https';
const options = {
host: '<your host>',
defaultPort: 443,
path: '<your path>',
// assuming the bundle file is co-located with this file
ca: readFileSync(__dirname + '/<your bundle file>.ca-bundle'),
headers: {
'content-type': 'application/json',
}
};
https.get(options, res => {
// do whatever you need to do
})
Run Code Online (Sandbox Code Playgroud)
但是,如果您可以在托管服务器中配置 ssl 设置,最好的解决方案是将中间证书添加到您的托管服务提供商。这样客户端请求者不需要指定 CA,因为它包含在服务器本身中。我个人使用 namecheap + heroku。我的诀窍是用 .crt 文件创建一个 .crt 文件cat yourcertificate.crt bundle.ca-bundle > server.crt。然后我打开这个文件并在第一个证书后添加一个换行符。你可以阅读更多
您也可以尝试将strictSSL设置为false,如下所示:
{
url: "https://...",
method: "POST",
headers: {
"Content-Type": "application/json"},
strictSSL: false
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
123153 次 |
| 最近记录: |