Как правильно соединиться с аутентификацией в HiveMQ? - PullRequest
0 голосов
/ 24 июня 2019

Я пытаюсь подключить клиента к серверу с помощью простой аутентификации с использованием HiveMQ.В HiveMQ Client я создал клиента и использовал connectWith(), чтобы указать, что я хочу подключиться с простой аутентификацией.Когда я вводил имя пользователя и пароль, я получал MqttClientStateException, который я оставил ниже.Должен ли я хранить / настраивать имя пользователя и пароль вручную на сервере, поскольку я только что ввел username и password, как показано здесь:

https://hivemq.github.io/hivemq-mqtt-client/docs/mqtt_operations/connect.html#authenticationauthorization.

Код:

Mqtt5BlockingClient publisher = 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("Publisher1"))         // prints a string that the client is connected
    .addDisconnectedListener(context -> ClientConnectionRetreiver.printDisconnected("Publisher1"))  // prints a string that the client is disconnected
    .buildBlocking();  // creates the client builder                
    publisher.connectWith() // connects the client
        .simpleAuth()
            .username("Username")
            .password("Password".getBytes())
            .applySimpleAuth();

Исключение:

Exception in thread "SubThread1" com.hivemq.client.mqtt.exceptions.MqttClientStateException: MQTT client is not connected.
at com.hivemq.client.internal.mqtt.MqttBlockingClient.subscribe(MqttBlockingClient.java:101)
at com.hivemq.client.internal.mqtt.message.subscribe.MqttSubscribeBuilder$Send.send(MqttSubscribeBuilder.java:184)
at com.main.SubThread.run(SubThread.java:83)
at java.base/java.lang.Thread.run(Thread.java:834)

1 Ответ

1 голос
/ 25 июня 2019

Вы забыли отправить сообщение о подключении

publisher.connectWith()
        .simpleAuth()
            .username("Username")
            .password("Password".getBytes())
            .applySimpleAuth()
        .send();

Если вы действительно хотите аутентифицировать клиента, вам необходимо настроить сервер. С HiveMQ вы можете использовать расширение контроля доступа на основе ролей файлов (https://www.hivemq.com/extension/file-rbac-extension/) для простой аутентификации.

...