我有一个ElasticSearch集群,该集群位于配置了基本身份验证的代理后面。如何使用凭证从RestHighLevelClient拨打电话?在SpringBoot中,通常必须像这样配置HttpClientContext:
@Bean
public HttpClientContext configuredContext() {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS));
AuthCache authCache = new BasicAuthCache();
authCache.put(httpHost, new BasicScheme());
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
return context;
}
,但是在ClientBuilder或上下文请求中找不到任何合适的参数。有什么办法可以使该设置正常工作吗?
答案 0 :(得分:1)
您可以将 RestHighLevelClient 实例创建为
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("user", "password"));
RestClientBuilder builder = RestClient.builder(new HttpHost("server", port))
.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/_basic_authentication.html。希望这会有所帮助。