elasticsearch-java
elasticsearch-java copied to clipboard
Elasticsearch token service demo code not working
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_other_authentication_methods.html. The following code:
RestClientBuilder builder = RestClient.builder( new HttpHost("localhost", 9200, "http")); Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization", "Bearer u6iuAxZ0RG1Kcm5jVFI4eU4tZU9aVFEwT2F3")}; builder.setDefaultHeaders(defaultHeaders);
It should be:
RestClientBuilder builder = RestClient.builder( new HttpHost("localhost", 9200, "http")); Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization", "ApiKey u6iuAxZ0RG1Kcm5jVFI4eU4tZU9aVFEwT2F3")}; builder.setDefaultHeaders(defaultHeaders);
The "Bearer" is wrong according to the following code on the same page:
String apiKeyId = "uqlEyn8B_gQ_jlvwDIvM"; String apiKeySecret = "HxHWk2m4RN-V_qg9cDpuX"; String apiKeyAuth = Base64.getEncoder().encodeToString( (apiKeyId + ":" + apiKeySecret) .getBytes(StandardCharsets.UTF_8)); RestClientBuilder builder = RestClient.builder( new HttpHost("localhost", 9200, "http")); Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization", "ApiKey " + apiKeyAuth)}; builder.setDefaultHeaders(defaultHeaders);
It should be ApiKey.
Thanks & best regards,
Xiaoguo
Thanks, @liu-xiao-guo . I'm going to transfer this to the Java client library for the developers to take a look at.