gpt4 book ai didi

node.js - Nodejs 安全 MQTT 连接

转载 作者:太空宇宙 更新时间:2023-11-03 13:49:18 24 4
gpt4 key购买 nike

在我的服务器上,我使用的是基于 node.js 的 mqtt 代理 (mosca),它具有来自 Certbot 的 SSL 证书。

在客户端,我也在使用基于 node.js 的解决方案 (mqtt.js)

现在我的问题是:为了获得安全连接,我是否需要拥有属性 rejectUnauthorized: true?因为我只有在拥有自签名证书时才能让它工作,这当然不是很安全。在服务器端拥有常规 SSL 证书时,我无法使用 rejectUnauthorized: true,为什么?

我的服务器选项:

const settings = {
port: 8883,
secure: {
keyPath: '/etc/letsencrypt/live/XXX/privkey.pem',
certPath: '/etc/letsencrypt/live/XXX/cert.pem',
},
https: {
port: 443,
bundle: true,
static: './',
},
};

我在客户端的连接选项:

const options = {
host: "XXXXXX",
port: 8883,
protocol: "mqtts",
rejectUnauthorized: true,
username: process.env.USERNAME,
password: process.env.PASSWORD,
};

接收错误:

Error: unable to verify the first certificate
at TLSSocket.onConnectSecure (_tls_wrap.js:1318:34)
at TLSSocket.emit (events.js:210:5)
at TLSSocket._finishInit (_tls_wrap.js:792:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:606:12) {
code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'
}

最佳答案

更新您的服务器端设置以指向 fullchain.pem 而不是 cert.pem 文件

这将包括服务器呈现的完整信任链,然后应该包括根 CA 证书,该证书将位于 NodeJS 中的受信任集中。

关于node.js - Nodejs 安全 MQTT 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58587671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com