sslWithDefaultConfig

Hello All,

I feel silly asking this question but how do you set the directory for where the library looks for the servers certificate?

Using MQTTv5Client with sslWithDefaultConfig

Thanks

Hi Allen,

welcome to our community! Usually you do not need to set a directory when using sslWithDefaultConfig. Hence my next question: what are you trying to achieve? What is the broker you are trying to connect to: broker.hivemq.com:1883, a HiveMQ cloud broker :8883 or a self-hosted broker of yours?

Thanks,
Dasha from HiveMQ Team

Thank you Daria, I have been using the free MQTT broker (“test.mosquitto.org”) and have been able to connect to port 1883. However, when I try to connect to port 1884 (unencrypted, authenticated) with the following code

Mqtt5BlockingClient client2 = Mqtt5Client.builder()
                .identifier(UUID.randomUUID().toString())
                .serverHost("test.mosquitto.org")
                .serverPort(1884)
                .sslWithDefaultConfig()
                .buildBlocking();

    

        client2.connectWith().simpleAuth()
                .username("rw")
                .password("readwrite".getBytes())
                .applySimpleAuth()
                .send();

I get the error

Exception in thread “main” com.hivemq.client.mqtt.exceptions.ConnectionFailedException: java.io.IOException: An existing connection was forcibly closed by the remote host
at com.hivemq.client.internal.mqtt.MqttBlockingClient.connect(MqttBlockingClient.java:101)
at com.hivemq.client.internal.mqtt.message.connect.MqttConnectBuilder$Send.send(MqttConnectBuilder.java:195)
at main.main(main.java:25)

I have the certificate in the root of my program.

I am able to connect to hivemq com:1883 as you stated. When I try port 8883 with the following code

Mqtt5BlockingClient client2 = Mqtt5Client.builder()
                .identifier(UUID.randomUUID().toString())
                .serverHost("broker.hivemq.com")
                .serverPort(8883)
                .sslWithDefaultConfig()
                .buildBlocking();

                client2.connect();

I get the error

Exception in thread “main” com.hivemq.client.mqtt.exceptions.ConnectionFailedException: io.netty.channel.ConnectTimeoutException: connection timed out: MQTT Dashboard
at com.hivemq.client.internal.mqtt.MqttBlockingClient.connect(MqttBlockingClient.java:101)
at com.hivemq.client.internal.mqtt.MqttBlockingClient.connect(MqttBlockingClient.java:92)
at main.main(main.java:19)

Not sure what I am doing wrong

Was able to connect to test.mosquitto.org:8883 by adding the sites certificate to the java cacerts file and running the code below.

found here IBM Docs

KeyStore keyStore = KeyStore.getInstance("JKS");
        // load default jvm keystore
        keyStore.load(new FileInputStream(
                System.getProperties()
                        .getProperty("java.home") + File.separator
                        + "lib" + File.separator + "security" + File.separator
                        + "cacerts"), "changeit".toCharArray());

        TrustManagerFactory tmf = TrustManagerFactory.getInstance(
                TrustManagerFactory.getDefaultAlgorithm());

        tmf.init(keyStore);

        Mqtt5BlockingClient client2 = Mqtt5Client.builder()
                .identifier(UUID.randomUUID().toString())
                .serverHost("test.mosquitto.org")
                .serverPort(8883)
                .sslConfig()
                    .trustManagerFactory(tmf)
                    .applySslConfig()
                .buildBlocking();

        client2.connect();

Hi Allen,

I am able to connect to the test.mosquitto.org:1884 with the following code:

public class AsyncDemoMosquitto {
    private static final String MQTT_SERVER     = "test.mosquitto.org";
    private static final int MQTT_PORT     = 1884;
    private static final String  MQTT_USERNAME     = "rw";
    private static final String  MQTT_PASSWORD     = "readwrite";

    public static void main(final String[] args) throws InterruptedException {
        final Mqtt5SimpleAuth simpleAuth = Mqtt5SimpleAuth.builder()
                .username(MQTT_USERNAME)
                .password(MQTT_PASSWORD.getBytes())
                .build();

        final Mqtt5AsyncClient client = Mqtt5Client.builder()
                .serverHost(MQTT_SERVER)
                .serverPort(MQTT_PORT)
                .simpleAuth(simpleAuth)
                .buildAsync();

        client.connect()
                .thenAccept(connAck -> System.out.println("connected " + connAck))
                .thenCompose(v -> client.publishWith().topic("hivemqclient").qos(MqttQos.EXACTLY_ONCE).send())
                .thenAccept(publishResult -> System.out.println("published " + publishResult))
                .thenCompose(v -> client.disconnect())
                .thenAccept(v -> System.out.println("disconnected"));

        System.out.println("see that everything above is async");
        for (int i = 0; i < 5; i++) {
            TimeUnit.MILLISECONDS.sleep(50);
            System.out.println("...");
        }
    }
}

Let me know if this works for you too.

Kind regards,
Dasha from HiveMQ Team

awesome! I think my issue is that user name and password is really authorization. When I hear the term authentication, I’m thinking of the process of authenticating the server using the public key. If you look at the site, it refers to the username and password as authentication which through me off.