在Elasticsearch高级客户端中为JAVA添加身份验证

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

之前我一直在使用高级Rest客户端,没有任何问题,但是我找不到如何在其上发送基本身份验证标头。

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

有谁能成功连接到具有高级其余客户的安全elasticsearch?

干杯,

回答:

您可以将用户名和密码指定给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);

  • https://www.elastic.co/guide/zh-CN/elasticsearch/client/java-rest/current/java-rest-high-getting-started-initialization.html
  • https://www.elastic.co/guide/zh-CN/elasticsearch/client/java-rest/current/_basic_authentication.html

以上是 在Elasticsearch高级客户端中为JAVA添加身份验证 的全部内容, 来源链接: utcz.com/qa/424180.html

回到顶部