I am using an elasticsearch instance in elastic cloud instance secured with X-PACK.
I had been using the high level rest client before without any problems but I am unable to find how to send the basic authentication header on it.
I have tried to put the credentials as part of the URL but it didn't seem to be able to connect in that case.
Has anyone succeed to connect to a secured elasticsearch with high level rest client?
You can specify the username and password to the Java Low Level REST Client and pass the Low Level REST Client to the RestHighLevelClient
instance.
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("user", "password"));
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
RestHighLevelClient client = new RestHighLevelClient(builder);
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With