在弹性搜索高级客户端中为JAVA添加身份验证 [英] Add authentication in elasticsearch high level client for JAVA

查看:464
本文介绍了在弹性搜索高级客户端中为JAVA添加身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用X-PACK保护的弹性云实例中使用弹性搜索实例。

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.

我试图将凭据作为URL的一部分,但在这种情况下它似乎无法连接。

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?

干杯,

推荐答案

您可以为Java低级别REST客户端指定用户名和密码,并将低级别REST客户端传递给 RestHighLevelClient 实例。

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);

参考文献:

  • 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

这篇关于在弹性搜索高级客户端中为JAVA添加身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆