Java配置带密码的ElasticSearch客户端
程序员文章站
2022-07-01 15:31:10
...
@Configuration
public class ElasticSearchConfiguration {
public static final String username = "elastic";
public static final String password = "你的密码";
//通用设置项
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esRestClient() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(username, password)); //es账号密码
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("ip地址", 端口号, "http")
).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
})
);
return client;
}
}
此方案在6.x以及7.x版本下测试成功
推荐阅读
-
ElasticSearch配置需要权限认证的jest客户端
-
Elasticsearch关于Java客户端的基本操作 (RestHighLevelClient)
-
elasticsearch系列-Java客户端的查询操作
-
ELASTICSEARCH的java客户端和springboot整合(二)
-
Elasticsearch Java 客户端的选择
-
Java配置带密码的ElasticSearch客户端
-
Elasticsearch Java API 的使用(1)—创建客户端
-
ElasticSearch的java高级客户端demo
-
ElasticSearch-常用的几种Java客户端
-
ElasticSearch的java客户端demo