首页 > 解决方案 > 无法通过 ssl 连接到 mqtt 代理

问题描述

我有一个运行 mqtt 代理和 java 后端的树莓。因为我已经实现了 ssl,所以我无法建立从后端到代理的连接。我可以从 MacBook 上运行的 IDE 将后端连接到 raspberry 上的代理,例如:

client = new MqttAsyncClient(
            "ssl://my-domain.com:1883", "backend");

如果后端在覆盆子上,我试过:

client = new MqttAsyncClient(
            "ssl://localhost:1883", "backend");


client = new MqttAsyncClient(
            "ssl://127.0.0.1:1883", "backend");

没有成功。在这种情况下,我从未使用过 ssl 连接。我监督了什么?

错误:

Exception in thread "main" No connection to client (32104)
    at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:143)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.subscribe(MqttAsyncClient.java:721)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.subscribe(MqttAsyncClient.java:681)
    at com.cdh.Service.mqttManager.subscribe(mqttManager.java:243)
    at com.cdh.main.main(main.java:14)

编辑:

我将端口更改为 8883。如果我在其他设备上运行后端,它可以工作,但如果代理和后端在树莓上,则不能。我还尝试使用我的证书中的域名。

标签: javalinuxsslmqtt

解决方案


The hostname you use to connect needs to match the CN or SAN entries in the certificate presented by the broker or else it will fail validation.

Unless you included 127.0.0.1 or localhost in the certificate the the client will reject the connection because the certificate doesn't validate for that address.

p.s. you should probably use a different port for MQTT of TLS rather than 1883 as that is the standard port for MQTT without TLS.


推荐阅读