gpt4 book ai didi

java - 使用 RestHighLevelClient 使用 Basic-Auth-Proxy 后面的 ElasticSearch 端点

转载 作者:行者123 更新时间:2023-11-30 05:51:07 26 4
gpt4 key购买 nike

我有一个 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 或上下文请求中找不到任何合适的参数。有什么办法可以让这个设置起作用吗?

最佳答案

您可以创建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 。希望这会有所帮助。

关于java - 使用 RestHighLevelClient 使用 Basic-Auth-Proxy 后面的 ElasticSearch 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53903941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com