要是有密碼都是12345678
Common Name 的值 不能是ip,一定要是域名(切記)
先安裝 openssl
yum install openssl
yum install openssl-devel
cd /etc/mosquitto
mkdir ca
cd ca
-days 我就設定36500 一百年。。。
openssl req -newkey rsa:2045 -x509 -nodes -sha256 -days 36500 -extensions v3_ca -keyout ca.key -out ca.crt
輸入密碼
Country Name (2 letter code) [AU]:cn
State or Province Name (full name) [Some-State]:gd
Locality Name (eg, city) []:sz
Organization Name (eg, company) [Internet Widgits Pty Ltd]:fih
Organizational Unit Name (eg, section) []:yun
就是這個, 要是域名
Common Name (e.g. server FQDN or YOUR name) []:sdw1
Email Address [] [email protected]
這步走完生成了ca的兩個檔案
生成不加密的伺服器密鑰。
openssl genrsa -out server.key 2048
生成證書簽名請求以發送到CA。
openssl req -out server.csr -key server.key -new
注意
當提示您輸入CN(通用名稱)時,請輸入伺服器(或代理)的主機名或域名。
将CSR發送到CA,或用您的CA密鑰對其進行簽名:
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 36500
客戶
生成用戶端密鑰。
openssl genrsa -des3 -out client.key 2048
生成證書簽名請求以發送到CA。
openssl req -out client.csr -key client.key -new
将CSR發送到CA,或用您的CA密鑰對其進行簽名:
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 36500
此時在ca目錄 下已經生成了server和clent的證書
修改配置檔案 加上
ssl mosquitto配置檔案 修改
listener 8883
cafile /etc/mosquitto/ca/ca.crt
certfile /etc/mosquitto/ca/server.crt
keyfile /etc/mosquitto/ca/server.key
#這個打開表示雙向驗證
require_certificate true
#如果下面這個設為true 那麼資料庫的使用者名密碼就無效了
use_identity_as_username false
注意配置檔案裡預設是port 1883 這是預設端口
listener 表示額外端口 可以監聽多個端口
包換mqtt和websocket
如果服務端有配置ca.crt,那用戶端連接配接時也要這個ca證書
打開表示雙向驗證,用戶端也要帶上用戶端的crt,key檔案才行
用戶端連接配接裡要用 SSLContext context = SSLContext.getInstance(“TLSv1.2”);
TLSv1.2這個協定