[英]How to connect a SSL MQTT client with a CA signed server certificate?
I need to sign up for an MQTT address, but I get an exception. 我需要注册一个MQTT地址,但出现异常。 As far as I know, I do not need to upload any certificates, just use the server's.
据我所知,我不需要上传任何证书,只需使用服务器的证书即可。 What should I do?
我该怎么办?
Code: 码:
public void connect() throws MqttException{
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName("username");
options.setPassword("123456".toCharArray());
options.setAutomaticReconnect(true);
client = new MqttClient("ssl://myadress:1883", MqttClient.generateClientId());
client.setCallback(callback);
System.out.println(topic);
try {
client.connect(options);
client.subscribe(topic);
} catch (Exception e){
e.printStackTrace();
}
}
Exception: 例外:
MqttException (0) - javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
I'm using the Eclipse Paho library, and I did not find (or, if I did, I did not understand) anything very enlightening in the library's documentation. 我正在使用Eclipse Paho库,但在库的文档中没有发现(或者,如果我这样做,我不理解)任何很有启发性的东西。
You need to supply your own SSLSocketFactory
that has it's own TrustManager
that knows to trust your certificate. 您需要提供自己的
SSLSocketFactory
,它具有自己的TrustManager
,该TrustManager
知道信任您的证书。
You add the SSLSocketFactory to the MqttClientOptions
object that gets passed to MqttClient.connect()
call. 您将SSLSocketFactory添加到
MqttClientOptions
对象,该对象传递给MqttClient.connect()
调用。
There are plenty of examples online about settings up a SSLSocketFactory with your own CA certs. 在线上有很多示例,这些示例介绍如何使用自己的CA证书设置SSLSocketFactory。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.