我正在寻找使用MQTT和TLS-PSK而不使用任何证书的示例代码。我有一个psk身份和一把钥匙。
这是我所拥有的样本不起作用..
int qos = 1;
String broker = "tcp://xxx.example.in:5633";
String clientId = "JavaExample";
String user = "my_identity"; (my psk identity)
String pass = "6874e899071b909a2268b020ef9ff046cydfernjn90546n546nklkj565"; (my psk key)
MemoryPersistence persistence = new MemoryPersistence();
MqttAsyncClient sampleClient = new MqttAsyncClient(broker,clientId,persistence);
sampleClient.setCallback(new SimpleMqttCallBack());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(user);
connOpts.setCleanSession(false);
System.out.println("Connecting to broker: " + broker);
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null,null,null);
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
connOpts.setSocketFactory(socketFactory);
System.out.println("SSL Added");
sampleClient.connect(connOpts);
sampleClient.subscribe("#", qos);
如何将我的psk标识和密钥与TLS1.2和MQTT连接?
答案 0 :(得分:0)
也许这个答案会有所帮助:https://stackoverflow.com/a/49431225/8495341
您可以将自定义SSLSocketFactory传递给使用充气城堡进行TLS-PSK的paho MQTT客户端。