@Test
    public void restClientSecuredClusterHealth() throws IOException {
        // httpClientSecuredContainer {
        // Create the elasticsearch container.
        try (
            ElasticsearchContainer container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE)
                // With a password
                .withPassword(ELASTICSEARCH_PASSWORD)
        ) {
            // Start the container. This step might take some time...
            container.start();

            // Create the secured client.
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(
                AuthScope.ANY,
                new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD)
            );

            client =
                RestClient
                    .builder(HttpHost.create(container.getHttpHostAddress()))
                    .setHttpClientConfigCallback(httpClientBuilder -> {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    })
                    .build();

            Response response = client.performRequest(new Request("GET", "/_cluster/health"));
            // }}
            assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
            assertThat(EntityUtils.toString(response.getEntity())).contains("cluster_name");
            // httpClientSecuredContainer {{
        }
        // }
    }
From 6.8, we can optionally activate security with a default password.
modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java