Configure Elasticsearch rest high level client with Basic credential provider

爷,独闯天下 提交于 2020-07-10 10:27:55

问题


This question was asked in the follow-up question to my this SO answer of how to secure JHLRC in this comment, where I explained how to send Basic Credential in Elasticsearch JHLRC request but it was not at the client level and was at the request level.

I feel configure at the client level, would avoid repeating the same code again in cases, where you are dealing with only a few users it would be helpful to configure the credentials in the client itself.


回答1:


You can follow the below steps as mentioned in this official link.

Create CredentialsProvider using the BasicCredentialsProvider.

final CredentialsProvider credentialsProvider =
                new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "elastic"));

Now use the CredentialsProvider while building the rest client(JHLRC).

RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost(scannerConfiguration.getElasticsearchConfig().getHost(),
                        scannerConfiguration.getElasticsearchConfig().getPort(),
                        "http")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider);
                    }
                }));


来源:https://stackoverflow.com/questions/62547260/configure-elasticsearch-rest-high-level-client-with-basic-credential-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!