hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
847 stars 158 forks source link

Cannot connect MQTT broker with pre-signed url #507

Closed younny closed 2 years ago

younny commented 2 years ago

Hi

I'm currently trying to connect to MQTT broker with pre-signed url which looks like, wss://a1dcli...ts.iot.eu-central-1.amazonaws.com/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=...&X-Amz-Date=20211004T212754Z&X-Amz-SignedHeaders=host&X-Amz-Signature=...&X-Amz-Security-Token=FwoG...

I checked it works with MQTT Explorer, but with my client implementation, keep throwing 403 forbidden error. I might be missing something.

This is my implementation.

val mqttClient: Mqtt5AsyncClient = com.hivemq.client.mqtt.mqtt5.Mqtt5Client.builder()
            .serverHost("a1dc...s.iot.eu-central-1.amazonaws.com")
            .serverPort(443)
            .identifier(deviceUtils.getDeviceInfo().id)
            .webSocketConfig(
                MqttWebSocketConfig.builder()
                    .serverPath("mqtt")
                    .queryString("X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=...")
                    .subprotocol("ws")
                    .build()
            )
            .sslWithDefaultConfig()
            .addConnectedListener {
                Timber.v("<------------------ connected !")
            }
            .addDisconnectedListener {
                Timber.v("<------------------ disconnected ! ${it.cause}")
            }
            .buildAsync()

        mqttClient.connect()
            .whenComplete { connAct, throwable ->
                if (throwable != null) {
                    Timber.v("<------------------ failed ! ${throwable.message}")
                } else {
                    Timber.v("<------------------ success !")
                }
            }

Would there be additional set up needed? I'd appreciate a lot if anyone could help me here..