Node.js trong>
这就是为什么我们需要创建自己的根CA.Use 然后,创建一个自定义的Node.js trong>,node.js,ssl-certificate,jira,Node.js,Ssl Certificate,Jira,这就是为什么我们需要创建自己的根CA.Use 然后,创建一个自定义的https代理,该代理使用我们的证书包(根证书和中间证书)进行配置。发出请求时,将此代理传递给axios // index.js const axios = require('axios'); const path = require('path'); const https = require('https'); const rootCas = require('ssl-root-cas').create(); rootCa
https
代理,该代理使用我们的证书包(根证书和中间证书)进行配置。发出请求时,将此代理传递给axios
// index.js
const axios = require('axios');
const path = require('path');
const https = require('https');
const rootCas = require('ssl-root-cas').create();
rootCas.addFile(path.resolve(__dirname, 'intermediate.pem'));
const httpsAgent = new https.Agent({ca: rootCas});
axios.get('https://incomplete-chain.badssl.com', { httpsAgent })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
您可以将证书放在https
全局代理上,而不是创建自定义https
代理并将其传递给axios
:
// Applies to ALL requests (whether using https directly or the request module)
https.globalAgent.options.ca = rootCas;
资源:
您可以全局禁用证书检查—无论您使用哪个软件包进行请求—如下所示:
// Disable certificate errors globally
// (ES6 imports (eg typescript))
//
import * as https from 'https'
https.globalAgent.options.rejectUnauthorized = false
或
当然,您不应该这样做-但对于调试和/或非常基本的脚本编写来说,它确实很方便,因为您根本不关心证书是否正确验证。几天前我遇到了这个问题,这是我所采用的方法,它对我很有效 对我来说,这是在我试图使用axios获取数据或获取库时发生的,因为我在公司防火墙下,所以我们有一些特定的证书,node js certificate store无法指向这些证书 因此,对于我的洛克拉何斯特,我采用了这种方法。 我在我的项目中创建了一个文件夹,并将整个证书链保存在该文件夹和我的dev server脚本(package.json)中。我将此添加到服务器脚本中,以便节点js可以引用路径
"dev-server":set NODE_EXTRA_CA_CERTS=certificates/certs-bundle.crt
对于我的服务器(不同的环境),我创建了一个新的环境变量,如下所示并添加了它
"name":NODE_EXTRA_CA_CERTS
"value":certificates/certs-bundle.crt
在我的案例中,我没有生成任何证书,因为整个证书链都已经为我提供了。我遇到了非常罕见的案例,但希望它能对某人有所帮助:创建一个代理服务,将请求代理到另一个服务。即使我添加了所有预期的证书,每个请求的错误都是“无法验证第一个证书” 原因很简单——我不小心又重新发送了“主机”标题。
请确保您没有显式发送“主机”标题。我可以通过mozilla或chrome等浏览器获取证书链
process.env.NODE_TLS_REJECT_UNAUTHORIZED='0'代码>你能解决这个问题吗?我使用了另一个过程,比如禁用证书验证,你能再详细说明一下吗?这将对下面的问题非常有帮助。我们需要在未经授权的情况下拒绝证书验证的答案。这个答案很危险。另一个更安全。通过这样做,您可以删除SSL提供的安全性,因此它应该仅用于开发。不检查证书意味着您无法确定另一方的身份,因此可能会受到欺骗主机的影响。然而,即使您不检查证书,您仍然可以得到加密的通信,无法(轻松)被监视。因此,添加这一行不会“消除”SSL的安全性,也不会像另一位评论者所说的那样“禁用[]SSL的全部好处”。禁用SSL验证并不是解决任何问题的方法。:-)如果您使用的是节点请求库,则此方法有效。我就是。谢谢,它解决了我当前的开发需求。在大多数情况下都应该使用这个答案,因为它实际上解决了问题,而不是禁用了SSL的全部好处。正如SSL根cas模块自述文件中所述,此问题最常见的原因之一是您的证书没有嵌入其中间CA证书。在尝试其他操作之前,请尝试修复您的证书;)您甚至可能不需要SSL根cas包。只需将globalAgents.option.cert设置为全链证书。这就解决了我的问题。mkcert不会创建“完整链”证书。您必须在一个新的证书文件中将您的证书与根证书连接在一起,根证书位于$(mkcert-CAROOT)/rootCA.pem
,并执行类似于https.globalAgent.options.ca=fs.readFileSync('fullchain.pem')
的操作,请参阅以了解出于安全考虑,ssl根ca
npm模块对mozilla.org的请求。它可能是安全的,因为Mozilla,但它看起来像一个攻击向量。我得到这个问题(链问题……不完整),我的证书是从DigiCelt Inc.授权的,什么是修复这个过程?@ imracuang总之,你的服务器需要服务不仅仅是你的域名证书,而且中间证书。我无法在此评论中提供更多细节,但希望这些信息足以为您指明正确的方向。非常感谢,我们通过梳理根证书tooThanks为您找到答案!我发现我的证书是不完整的,虽然它在chrome和firefox中工作得很好,但在electron应用程序中却不起作用,我在nginx端通过cat domainname.crt domainname.ca-bundle>domainname ssl bundle.crt
将其修复。这似乎与相同,同样危险。它不同,它不需要任何编码更改,因为env变量可以在源代码之外设置。这个答案很危险。您正在禁用TLS提供的任何安全性。这对我很有效,非常有用。在我的例子中,我只是在和你说话,所以安全性不是问题。只需测试localhost就可以了。请确保在测试后将其删除。这解决了我的问题,我使用的是“请求”模块而不是“http”。谢谢这对我来说很管用,只是在进口时我不得不向我们出口
var rootCas = require('ssl-root-cas/latest').create();
rootCas
.addFile(path.join(__dirname, '../config/ssl/gd_bundle-g2-g1.crt'))
;
// will work with all https requests will all libraries (i.e. request.js)
require('https').globalAgent.options.ca = rootCas;
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
}
npm install --save node_extra_ca_certs_mozilla_bundle
NODE_EXTRA_CA_CERTS=node_modules/node_extra_ca_certs_mozilla_bundle/ca_bundle/ca_intermediate_root_bundle.pem node your_script.js
// index.js
const axios = require('axios');
axios.get('https://incomplete-chain.badssl.com')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
openssl s_client -connect incomplete-chain.badssl.com:443 -servername incomplete-chain.badssl.com | tee logcertfile
openssl x509 -in logcertfile -noout -text | grep -i "issuer"
curl --output intermediate.crt http://cacerts.digicert.com/DigiCertSHA2SecureServerCA.crt
openssl x509 -inform DER -in intermediate.crt -out intermediate.pem -text
"start": "cross-env NODE_EXTRA_CA_CERTS=\"C:\\Users\\USERNAME\\Desktop\\ssl-connect\\intermediate.pem\" node index.js"
// index.js
const axios = require('axios');
const path = require('path');
const https = require('https');
const rootCas = require('ssl-root-cas').create();
rootCas.addFile(path.resolve(__dirname, 'intermediate.pem'));
const httpsAgent = new https.Agent({ca: rootCas});
axios.get('https://incomplete-chain.badssl.com', { httpsAgent })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
// Applies to ALL requests (whether using https directly or the request module)
https.globalAgent.options.ca = rootCas;
// Disable certificate errors globally
// (ES6 imports (eg typescript))
//
import * as https from 'https'
https.globalAgent.options.rejectUnauthorized = false
// Disable certificate errors globally
// (vanilla nodejs)
//
require('https').globalAgent.options.rejectUnauthorized = false
"dev-server":set NODE_EXTRA_CA_CERTS=certificates/certs-bundle.crt
"name":NODE_EXTRA_CA_CERTS
"value":certificates/certs-bundle.crt
----BEGIN CERTIFICATE-----
MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB
......
-----END CERTIFICATE-----
----BEGIN CERTIFICATE-----
MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB
......
-----END CERTIFICATE-----
import https from 'https'
import cas from 'ssl-root-cas'
interface CaList extends Buffer {
addFile(file: string): Buffer[]
}
const caList = cas.create() as CaList
caList.addFile(process.env.PROJECT_PATH + 'certs/first-chain.pem')
caList.addFile(process.env.PROJECT_PATH + 'certs/second-chain.pem')
this.client.connect(KtUrl, undefined, undefined, undefined, {
agent: new https.Agent({
ca: caList
})
})
declare module 'ssl-root-cas' {
function create(): string | Buffer | (string | Buffer)[] | undefined
}