|
| 1 | +package io.emqx.mqtt; |
| 2 | + |
| 3 | +import org.eclipse.paho.client.mqttv3.*; |
| 4 | + |
| 5 | +import javax.net.ssl.SSLContext; |
| 6 | +import javax.net.ssl.SSLSocketFactory; |
| 7 | +import javax.net.ssl.TrustManagerFactory; |
| 8 | +import java.io.FileInputStream; |
| 9 | +import java.security.*; |
| 10 | +import java.security.cert.Certificate; |
| 11 | +import java.security.cert.CertificateFactory; |
| 12 | +import java.security.cert.X509Certificate; |
| 13 | +import java.text.MessageFormat; |
| 14 | +import java.util.Arrays; |
| 15 | + |
| 16 | +public class MqttOneWayTlsSample { |
| 17 | + private static final String BROKER = "broker.emqx.io"; |
| 18 | + private static final String PORT = "8883"; |
| 19 | + private static final String CLIENT_ID = MqttClient.generateClientId(); |
| 20 | + private static final String USERNAME = "emqx"; |
| 21 | + private static final String PASSWORD = "public"; |
| 22 | + private static final int CONNECT_TIMEOUT = 300; |
| 23 | + private static final boolean CLEAN_SESSION = true; |
| 24 | + private static final String TOPIC = "java-mqtt/tls"; |
| 25 | + private static final int QoS = 1; |
| 26 | + private static final String PAYLOAD = "Enjoy the sample"; |
| 27 | + private static final String CA_CERT_PATH = MqttOneWayTlsSample.class.getResource("").getPath()+"./broker.emqx.io-ca.crt"; |
| 28 | + |
| 29 | + public static void main(String args[]) { |
| 30 | + MqttClient client = null; |
| 31 | + try { |
| 32 | + String server = "ssl://" + BROKER + ":" + PORT; |
| 33 | + client = new MqttClient(server, CLIENT_ID); |
| 34 | + |
| 35 | + client.setCallback(new MqttCallback() { |
| 36 | + |
| 37 | + @Override |
| 38 | + public void connectionLost(Throwable cause) { |
| 39 | + System.out.println(MessageFormat.format("Connection lost. Cause: {0}", cause)); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void messageArrived(String topic, MqttMessage message) throws Exception { |
| 44 | + System.out.println(MessageFormat.format("Callback: received message from topic {0}: {1}", |
| 45 | + topic, message.toString())); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void deliveryComplete(IMqttDeliveryToken token) { |
| 50 | + try { |
| 51 | + System.out.println(MessageFormat.format("Callback: delivered message to topics {0}", |
| 52 | + Arrays.asList(token.getTopics()))); |
| 53 | + } catch (Exception e) { |
| 54 | + e.printStackTrace(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + }); |
| 59 | + |
| 60 | + MqttConnectOptions options = new MqttConnectOptions(); |
| 61 | + options.setUserName(USERNAME); |
| 62 | + options.setPassword(PASSWORD.toCharArray()); |
| 63 | + options.setConnectionTimeout(CONNECT_TIMEOUT); |
| 64 | + options.setCleanSession(CLEAN_SESSION); |
| 65 | + options.setSocketFactory(getSocketFactory(CA_CERT_PATH)); |
| 66 | + |
| 67 | + System.out.println("Connecting to broker: " + server); |
| 68 | + client.connect(options); |
| 69 | + |
| 70 | + if (!client.isConnected()) { |
| 71 | + System.out.println("Failed to connect to broker: " + server); |
| 72 | + return; |
| 73 | + } |
| 74 | + System.out.println("Connected to broker: " + server); |
| 75 | + |
| 76 | + client.subscribe(TOPIC, QoS); |
| 77 | + System.out.println("Subscribed to topic: " + TOPIC); |
| 78 | + |
| 79 | + MqttMessage msg = new MqttMessage(PAYLOAD.getBytes("UTF-8")); |
| 80 | + msg.setQos(QoS); |
| 81 | + client.publish(TOPIC, msg); |
| 82 | + |
| 83 | + System.out.println("Disconnect from broker: " + server); |
| 84 | + client.disconnect(); |
| 85 | + } catch (Exception ex) { |
| 86 | + ex.printStackTrace(); |
| 87 | + } finally { |
| 88 | + if (client != null) { |
| 89 | + try { |
| 90 | + client.close(); |
| 91 | + } catch (MqttException e) { |
| 92 | + e.printStackTrace(); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public static SSLSocketFactory getSocketFactory(String caCertPath) throws Exception { |
| 99 | + CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| 100 | + |
| 101 | + // load CA certificate into keystore to authenticate server |
| 102 | + Certificate caCert = certFactory.generateCertificate(new FileInputStream(caCertPath)); |
| 103 | + X509Certificate x509CaCert = (X509Certificate) caCert; |
| 104 | + |
| 105 | + KeyStore caKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 106 | + caKeyStore.load(null, null); |
| 107 | + caKeyStore.setCertificateEntry("cacert", x509CaCert); |
| 108 | + |
| 109 | + TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 110 | + tmFactory.init(caKeyStore); |
| 111 | + |
| 112 | + SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); |
| 113 | + sslContext.init(null, tmFactory.getTrustManagers(), null); |
| 114 | + |
| 115 | + return sslContext.getSocketFactory(); |
| 116 | + } |
| 117 | +} |
0 commit comments