我正在尝试使用Java控制台应用程序连接到MQTT代理,而代理仅支持使用预共享密钥(不是SSL或其他客户端证书文件选项)的TLS。我有以下依赖项:
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.56</version>
</dependency>
我使用以下页面作为起点: http://www.hivemq.com/blog/mqtt-client-library-encyclopedia-eclipse-paho-java
他们有以下部分进行SSL连接:
MqttClient client = new MqttClient("ssl://yourbroker:8883", MqttClient.generateClientId(), new MemoryPersistence());
SSLContext sslContext = SSLContext.getInstance("SSL");
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore keyStore = readKeyStore();
trustManagerFactory.init(keyStore);
sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
MqttConnectOptions options = new MqttConnectOptions();
options.setSocketFactory(sslContext.getSocketFactory());
client.connect(options);
我真正需要的是一个使用TLS和预共享密钥的示例,我已经做了很多搜索并且可以找到TLS PSK示例(http://tiebing.blogspot.com.au/2013/09/java-bouncy-castle-tls-psk-example.html)但不知道如何连接MQTT客户端到该TLS PSK示例。
非常感谢任何帮助,谢谢!