我在由X-PACK保护的弹性云实例中使用elasticsearch实例。
之前我一直在使用高级Rest客户端,没有任何问题,但是我找不到如何在其上发送基本身份验证标头。
我试图将凭据作为URL的一部分,但在这种情况下似乎无法连接。
有谁能成功连接到具有高级其余客户的安全elasticsearch?
干杯,
您可以将用户名和密码指定给Java Low Level REST Client,然后将Low Level REST Client传递给RestHighLevelClient实例。
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);
参考文献: