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

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

问题描述

我在用 X-PACK 保护的弹性云实例中使用了一个 elasticsearch 实例.

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 Low Level REST Client 的用户名和密码,并将 Low Level REST Client 传递给 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);

参考:

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

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