解决RestHighLevelClient报错missing authentication credentials for REST request
2023-12-29 16:32:24
使用ElasticSearch Java API时遇到错误 "missing authentication credentials for REST request"
这是代码:
RestHighLevelClient esClient =
new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));
CreateIndexRequest createIndexRequest = new CreateIndexRequest("user");
CreateIndexResponse createIndexResponse = esClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("创建索引:" + acknowledged);
esClient.close();
?原因是请求没有发送用户名和密码,没有通过验证导致的。
解决方法,使用RestClientBuilder,将ES登录用户名和密码添加进去,修改如下:
RestClientBuilder build = RestClient.builder(new HttpHost("localhost",9200,"http"));
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "e5nTeAjoHTTPg--WgM49")); //用户名和密码
build.setHttpClientConfigCallback((HttpAsyncClientBuilder httpAsyncClientBuilder) -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
RestHighLevelClient esClient = new RestHighLevelClient(build);
CreateIndexRequest createIndexRequest = new CreateIndexRequest("user");
CreateIndexResponse createIndexResponse = esClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("创建索引:" + acknowledged);
esClient.close();
顺利解决:
文章来源:https://blog.csdn.net/y523648/article/details/135291887
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!