mosquitto提供SSL支持加密的网络连接和身份验证、本章节讲述次功能的实现、 在此之前需要一些准备工作。
准本工作: 一台 Linux 服务器、 安装好 openssl (不会明白怎么安装 openssl 的可以在网上搜索下、 我就不在这讲解了)
准本工作完成后我们开始来制作所需要的证书。
注:在生成证书过程 在需要输入 【Common Name 】 参数的地方 输入主机ip
一、Certificate Authority
Generate a certificate authority certificate and key.
openssl req -newkey rsa:2045 -x509 -nodes -sha256 -days 36500 -extensions v3_ca -keyout ca.key -out ca.crt
二、Server
Generate a server key.
openssl genrsa -des3 -out server.key 2048
Generate a server key without encryption.
openssl genrsa -out server.key 2048
Generate a certificate signing request to send to the CA.
openssl req -out server.csr -key server.key -new
Send the CSR to the CA, or sign it with your CA key:
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days <duration>
三、Client
Generate a client key.
openssl genrsa -des3 -out client.key 2048
Generate a certificate signing request to send to the CA.
openssl req -out client.csr -key client.key -new
Send the CSR to the CA, or sign it with your CA key:
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days <duration>
到此处证书基本生成完成、 此时会在对应的目录下生成如下证书
ca.crt ca.key ca.srl client.crt client.csr client.key server.crt server.csr server.key
Server 端:ca.crt、 client.crt 、server.key
Client 端:ca.crt、 client.csr client.key
其中 ca.crt 是同一个证书。
四、Mosquitto 服务进行配置证书
1> 打开配置文件 mosquitto.conf 修改如下配置
cafile /home/mosquitto-CA/ssl/ca.crt // 对应上述生成证书的绝对路劲
certfile /home/mosquitto-CA/ssl/server.crt
certfile /home/mosquitto-CA/ssl/server.key
require_certificate true
use_identity_as_username true
配置完成后 保存退出 到这 SSL/TLS 功能基本完成。
2> 启动 Mosquitto 服务
mosquitto -c mosquitto.conf –v
五、Java 端实现
Java 端的 SSL 客户端的实现和<<Mosquitto Java 客户端实现>> 讲解的基本一致 在这就不多做解释了、直接放代码。
需要引入依赖
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.45</version>
</dependency>
1> ClientMQTT
1 import java.util.concurrent.ScheduledExecutorService; 2 3 import javax.net.ssl.SSLSocketFactory; 4 5 import org.eclipse.paho.client.mqttv3.MqttClient; 6 import org.eclipse.paho.client.mqttv3.MqttConnectOptions; 7 import org.eclipse.paho.client.mqttv3.MqttException; 8 import org.eclipse.paho.client.mqttv3.MqttTopic; 9 import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; 10 11 public class ClientMQTT { 12 13 public static final String HOST = "ssl://172.16.192.102:1883"; 14 public static final String TOPIC = "root/topic/123"; 15 private static final String clientid = "client11"; 16 private MqttClient client; 17 private MqttConnectOptions options; 18 private String userName = "admin"; 19 private String passWord = "admin"; 20 public static String caFilePath = "D:/keystore/Mosquitto-ca/ssl/ca.crt"; 21 public static String clientCrtFilePath = "D:/keystore/Mosquitto-ca/ssl/client.crt"; 22 public static String clientKeyFilePath = "D:/keystore/Mosquitto-ca/ssl/client.key"; 23 24 private ScheduledExecutorService scheduler; 25 26 private void start() { 27 try { 28 // host为主机名,clientid即连接MQTT的客户端ID,一般以唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存 29 client = new MqttClient(HOST, clientid, new MemoryPersistence()); 30 // MQTT的连接设置 31 options = new MqttConnectOptions(); 32 // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接 33 options.setCleanSession(true); 34 // 设置连接的用户名 35 options.setUserName(userName); 36 // 设置连接的密码 37 options.setPassword(passWord.toCharArray()); 38 // 设置超时时间 单位为秒 39 options.setConnectionTimeout(10); 40 // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制 41 options.setKeepAliveInterval(20); 42 // 设置回调 43 client.setCallback(new PushCallback()); 44 MqttTopic topic = client.getTopic(TOPIC); 45 // setWill方法,如果项目中需要知道客户端是否掉线可以调用该方法。设置最终端口的通知消息 46 options.setWill(topic, "close".getBytes(), 2, true); 47 SSLSocketFactory factory = SslUtil.getSocketFactory(caFilePath, clientCrtFilePath, clientKeyFilePath, 48 "111111"); 49 options.setSocketFactory(factory); 50 client.connect(options); 51 // 订阅消息 52 int[] Qos = { 1 }; 53 String[] topic1 = { TOPIC }; 54 client.subscribe(topic1, Qos); 55 56 } catch (Exception e) { 57 e.printStackTrace(); 58 } 59 } 60 61 public static void main(String[] args) throws MqttException { 62 ClientMQTT client = new ClientMQTT(); 63 client.start(); 64 } 65 }