[英]Add authentication in elasticsearch high level client for Java
I am using an elasticsearch instance in elastic cloud instance secured with X-PACK.我在使用 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.我试图将凭据作为 URL 的一部分,但在这种情况下似乎无法连接。
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.您可以为 Java Low Level REST Client 指定用户名和密码,并将 Low Level REST Client 传递给RestHighLevelClient
实例。
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:参考:
Follow simple steps for making the RestHighLevelClient ready for connecting TLS+Auth Elastic Search按照简单的步骤使 RestHighLevelClient 准备好连接 TLS+Auth Elastic Search
Create a CredentialsProvider using BasicCredentialsProvider provided by Apache httpclient like below使用 Apache httpclient 提供的 BasicCredentialsProvider 创建一个 CredentialsProvider,如下所示
final CredentialsProvider credentialProvider = new BasicCredentialsProvider();
credentialProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(
ES_USERNAME,
ES_PASSWORD
));
Create a HttpHost provide by apache using Host, Port and Protocol like below使用主机、端口和协议创建 Apache 提供的 HttpHost,如下所示
HttpHost httpHost = new HttpHost("ELASTIC_SEARCH_HOST", 9200, "https");
Here I used "https" since TLS is enabled on ES.这里我使用了“https”,因为 ES 上启用了 TLS。 You can use "http" for normal ES.您可以将“http”用于普通 ES。
And the final step is to create RestHighLevelCLient like below最后一步是创建如下所示的 RestHighLevelCLient
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(nodes)
.setHttpClientConfigCallback(httpAsyncClientBuilder -> {
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialProvider);
return httpAsyncClientBuilder;
}
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.