hivemq-mqtt-client icon indicating copy to clipboard operation
hivemq-mqtt-client copied to clipboard

Subscriber message,CONNECT failed as CONNACK contained an Error Code: BAD_USER_NAME_OR_PASSWORD

Open zqb666 opened this issue 1 year ago • 3 comments

image

1.3.0 hello. how to set username and password when i connect ? with mqtt5 client

zqb666 avatar Sep 03 '24 03:09 zqb666

Hi @zqb666 - It depends on how the broker authorization is setup but for simple auth:

final Mqtt5BlockingClient client = Mqtt5Client.builder()
                        .identifier(getClientId())
                        .serverHost(HOST)
                        .serverPort(PORT)
                        .simpleAuth()
                            .username(USERNAME)
                            .password(ByteBuffer.wrap(PASSWORD.getBytes(StandardCharsets.UTF_8)))
                        .applySimpleAuth()

See also this blog post: Authentication with Username and Password

pglombardo avatar Sep 09 '24 09:09 pglombardo

Hi @zqb666 - did this help out at all? Did you find a solution that works for you?

pglombardo avatar Oct 21 '24 09:10 pglombardo

Hi @zqb666 - It depends on how the broker authorization is setup but for simple auth:

final Mqtt5BlockingClient client = Mqtt5Client.builder() .identifier(getClientId()) .serverHost(HOST) .serverPort(PORT) .simpleAuth() .username(USERNAME) .password(ByteBuffer.wrap(PASSWORD.getBytes(StandardCharsets.UTF_8))) .applySimpleAuth() See also this blog post: Authentication with Username and Password

i tyy your code ,fix a little problem, result like this: final Mqtt5BlockingClient client = (Mqtt5BlockingClient) Mqtt5Client.builder() .identifier(UUID.randomUUID().toString()) .serverHost(BROKER_HOST) .serverPort(1883) .simpleAuth() .username(USERNAME) .password(ByteBuffer.wrap(PASSWORD.getBytes(StandardCharsets.UTF_8))) .applySimpleAuth();

zqb666 avatar Feb 06 '25 02:02 zqb666