mqtt使用WebSocket over TLS(wss)握手失敗

由于網(wǎng)頁(yè)運(yùn)行在https上,所以連接mqtt只能用wss,但是使用自簽證書一直顯示1015 TLS_HANDSHAKE,可以判斷為認(rèn)證階段不通過。在MQTT.fx上面則提示證書非法。后面找了很多資料,終于在一個(gè)回到里面找到答案,就記錄下來。

自簽?zāi)_本如下:

#/bin/sh
# 生成自簽名的CA key和證書
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -sha256 -days 3650 -subj "/CN=192.168.100.1" -out ca.pem

# 生成服務(wù)器端的key和證書
openssl genrsa -out server.key 2048
openssl req -new -key ./server.key -out server.csr -subj "/CN=192.168.100.1"
openssl x509 -req -in ./server.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out server.pem -days 3650 -sha256

# 生成客戶端key和證書
openssl genrsa -out client.key 2048
openssl req -new -key ./client.key -out client.csr -subj "/CN=192.168.100.1"
openssl x509 -req -in ./client.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out client.pem -days 3650 -sha256
  • 這里的關(guān)鍵在于里面的幾個(gè)IP地址,需要替換成mosquitto服務(wù)器所在的IP或者域名,否則會(huì)認(rèn)證失敗

mosquitto 配置如下:

persistence true
persistence_location /mosquitto/data
log_dest file /mosquitto/log/mosquitto.log

allow_anonymous true

# mqtt協(xié)議
listener 1883
protocol mqtt

# mqtts 協(xié)議
listener 8883
cafile /mosquitto/config/ca.pem
certfile /mosquitto/config/server.pem
keyfile /mosquitto/config/server.key
tls_version tlsv1.2

# mqtt wss協(xié)議
listener 8084
protocol websockets
cafile /mosquitto/config/ca.pem
certfile /mosquitto/config/server.pem
keyfile /mosquitto/config/server.key
tls_version tlsv1.2
  • 這里則用到了上面生成的幾個(gè)文件
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容