简体   繁体   English

如何将SSL MQTT客户端与CA签名的服务器证书连接?

[英]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.

相关问题 使用自签名证书在 SSL 上使用 Mosquitto 设置 Java MQTT 客户端 - Setup Java MQTT client with Mosquitto over SSL with a self signed certificate 可以将CA(客户端)颁发的证书与“自签名”(服务器)证书进行通信吗? - Can communicate a certificate issued by a CA (client) with a certificate Self Signed (server)? 如何与CA连接以签署ssl证书? - How to connect with CA to sign ssl certificate? 由自己的CA签名的有效SSL证书 - Valid SSL certificate signed by own CA 在 Java 客户端接受服务器的自签名 ssl 证书 - Accept server's self-signed ssl certificate in Java client Java SSL / TLS客户端使用在运行时加载的自签名证书连接到并验证服务器? - Java SSL/TLS client to connect to & verify server using self-signed certificate which is loaded at run-time? 将无别名的自签名 SSL CA 证书列入白名单 - Whitelist self-signed SSL CA certificate without aliases java http客户端如何验证服务器的CA证书? - How does java http client validate Server's CA Certificate? 当服务器具有来自CA的有效证书时,用于Web服务(https)的客户端JVM是否会抛出SSL异常? - Will client JVM for a web service(https) throw an SSL Exception when the server is having a valid certificate from a CA? Java服务器自签名证书+客户端证书和SSL - 连接重置 - Java server self-signed certificate + client certificate and SSL - connection reset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM