# sslWithDefaultConfig

**URL:** <https://community.hivemq.com/t/sslwithdefaultconfig/946>\
**Category:** HiveMQ Client Library\
**Created:** [January 13, 2022, 2:43am UTC](https://community.hivemq.com/t/sslwithdefaultconfig/946 "2022-01-13T02:43:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ucallen](https://avatars.discourse-cdn.com/v4/letter/u/e79b87/32.png) [@ucallen](https://community.hivemq.com/u/ucallen)\
**Post date:** [January 13, 2022, 2:43am UTC](https://community.hivemq.com/t/sslwithdefaultconfig/946/1 "2022-01-13T02:43:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [January 13, 2022, 8:45am UTC](https://community.hivemq.com/t/sslwithdefaultconfig/946/2 "2022-01-13T08:45:08Z")

</div>

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](http://broker.hivemq.com:1883), a HiveMQ cloud broker :8883 or a self-hosted broker of yours?

Thanks,  
Dasha from HiveMQ Team

---

<div class="post-metadata">

**Author:** ![ucallen](https://avatars.discourse-cdn.com/v4/letter/u/e79b87/32.png) [@ucallen](https://community.hivemq.com/u/ucallen)\
**Post date:** [January 13, 2022, 5:31pm UTC](https://community.hivemq.com/t/sslwithdefaultconfig/946/3 "2022-01-13T17:31:27Z")

</div>

Thank you Daria, I have been using the free MQTT broker (“[test.mosquitto.org](http://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

```auto
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

```auto
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](http://broker.hivemq.com/3.65.154.195:8883)  
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

---

<div class="post-metadata">

**Author:** ![ucallen](https://avatars.discourse-cdn.com/v4/letter/u/e79b87/32.png) [@ucallen](https://community.hivemq.com/u/ucallen)\
**Post date:** [January 14, 2022, 1:15am UTC](https://community.hivemq.com/t/sslwithdefaultconfig/946/4 "2022-01-14T01:15:00Z")

</div>

Was able to connect to [test.mosquitto.org:8883](http://test.mosquitto.org:8883) by adding the sites certificate to the java cacerts file and running the code below.

found here [IBM Docs](https://www.ibm.com/docs/en/tnpm/1.4.2?topic=security-import-certificate-jre-keystore)

```auto
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();

```

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [January 14, 2022, 8:28am UTC](https://community.hivemq.com/t/sslwithdefaultconfig/946/5 "2022-01-14T08:28:18Z")

</div>

Hi Allen,

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

```auto
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

---

<div class="post-metadata">

**Author:** ![ucallen](https://avatars.discourse-cdn.com/v4/letter/u/e79b87/32.png) [@ucallen](https://community.hivemq.com/u/ucallen)\
**Post date:** [January 14, 2022, 4:36pm UTC](https://community.hivemq.com/t/sslwithdefaultconfig/946/6 "2022-01-14T16:36:39Z")

</div>

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.
