Add authentication in elasticsearch high level client for JAVA

后端 未结 1 1802
后悔当初
后悔当初 2020-12-14 18:55

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

相关标签:
1条回答
  • 2020-12-14 19:44

    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:

    • https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-initialization.html
    • https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_basic_authentication.html
    0 讨论(0)
提交回复
热议问题