How to authenticate to elastic search from java

Hi,
I am trying to authenticate to ES in java. But getting malformed host header exception.
Someone please help here.

            final CredentialsProvider credentialsProvider =
                    new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials("", ""));

        RestClientBuilder builder = RestClient.builder(
                new HttpHost(elasticSearchHost, elasticSearchPort, "https"))
                .setDefaultHeaders(new Header[]{new BasicHeader("Authorization","ApiKey "+"")})
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(
                            HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider);
                    }
                });

        RestHighLevelClient client = new RestHighLevelClient(
                builder);

Welcome. This is how I'm doing it:

Note that clusterUrl can be https://myinstance

1 Like

Great. It's working. Thanks

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.