gpt4 book ai didi

java - 如何在 HiveMQ 客户端中正确使用 SSL 配置? (MQTT)

转载 作者:行者123 更新时间:2023-12-02 09:44:49 24 4
gpt4 key购买 nike

我创建了一个客户端来使用安全连接和加密负载进行测试,因此我想使用默认的 SSL 配置。我尝试这样做,但我得到了一个 ConnectionClosedException 并且服务器立即关闭。我应该在服务器上配置一些东西吗?我在下面留下了代码和异常。

HiveMQ:

https://github.com/hivemq/hivemq-community-edition https://github.com/hivemq/hivemq-mqtt-client

代码:

    // Creates the client object using Blocking API 

subscriber = Mqtt5Client.builder()
.identifier(UUID.randomUUID().toString()) // the unique identifier of the MQTT client. The ID is randomly generated between
.serverHost("localhost") // the host name or IP address of the MQTT server. Kept it localhost for testing. localhost is default if not specified.
.serverPort(1883) // specifies the port of the server
.addConnectedListener(context -> ClientConnectionRetreiver.printConnected("Subscriber1")) // prints a string that the client is connected
.addDisconnectedListener(context -> ClientConnectionRetreiver.printDisconnected("Subscriber1")) // prints a string that the client is disconnected
.sslWithDefaultConfig()
.buildBlocking(); // creates the client builder
subscriber.connect();

异常:

com.hivemq.client.mqtt.exceptions.ConnectionClosedException: Server closed connection without DISCONNECT.
at com.hivemq.client.internal.mqtt.MqttBlockingClient.connect(MqttBlockingClient.java:91)
at com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient.connect(Mqtt5BlockingClient.java:64)
at com.main.SubThread.run(SubThread.java:71)
at java.base/java.lang.Thread.run(Thread.java:834)

最佳答案

为了使用 Ssl 通信,我需要设置 HiveMQ 服务器来使用 TLS。服务器未预先配置,必须手动完成。我点击了这个链接:

https://github.com/hivemq/hivemq-community-edition/wiki/HowTos#howto-configure-server-side-tls-with-hivemq-and-keytool-self-signed

关于java - 如何在 HiveMQ 客户端中正确使用 SSL 配置? (MQTT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56743420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com