# Authenticate Elasticsearch from spring boot

**URL:** <https://discuss.elastic.co/t/authenticate-elasticsearch-from-spring-boot/226196>\
**Category:** Elasticsearch\
**Created:** [April 2, 2020, 10:32am UTC](https://discuss.elastic.co/t/authenticate-elasticsearch-from-spring-boot/226196 "2020-04-02T10:32:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AshishUser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishuser/32/58854_2.png) [@AshishUser](https://discuss.elastic.co/u/AshishUser)\
**Post date:** [April 2, 2020, 10:32am UTC](https://discuss.elastic.co/t/authenticate-elasticsearch-from-spring-boot/226196/1 "2020-04-02T10:32:41Z")

</div>

I have setup open distro version 1.4, ES version 7.4 with LDAP authentication and authorization and it is working perfectly fine.  
Kibana/logstash able to connect to my elasticsearch using ldap service account (which has assigned permissions).

I also have one spring boot app which i need to connect to my elasticsearch, I am using High level rest client for this and following this link  
[https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/\_encrypted\_communication.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_encrypted_communication.html)

I want to implement authentication from my spring boot app using LDAP user.

This is my code from connection.

> ```auto
> final CredentialsProvider provider = new BasicCredentialsProvider();
> 
> ```

```
        provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(ldapSecurityPrincipal, ldapPrincipalPassword));
        Path caCertificatePath = Paths.get("/path/truststore.p12");
        KeyStore truststore = KeyStore.getInstance("pkcs12");
        try (InputStream is = Files.newInputStream(caCertificatePath)) {
            truststore.load(is, "password".toCharArray());
        }
        SSLContextBuilder sslContextBuilder = SSLContexts.custom()
                .loadTrustMaterial(truststore, null);
        final SSLContext sslContext = sslContextBuilder.build();
        return new RestHighLevelClient(RestClient.builder(
            new HttpHost(config.getHostname(), config.getPort(), config.getSchemeName())).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
                httpAsyncClientBuilder.setDefaultCredentialsProvider(provider);
                httpAsyncClientBuilder.setSSLContext(sslContext);
                return httpAsyncClientBuilder;
            }
        }));

```

> ```auto
> 
> ```

Looks like ssl handshake is happening with my elastic node but it is not authenticating my LDAP user.

org.elasticsearch.ElasticsearchStatusException: Unable to parse response body  
at \> org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:2030) ~[elasticsearch-rest-high-level-client]

> Caused by: org.elasticsearch.client.ResponseException: method [POST], host [[https://hostname:9200](https://hostname:9200/)], URI [/api/myendpoint\*/\_search?typed\_keys=true&ignore\_unavailable=false&expand\_wildcards=open&allow\_no\_indices=true&search\_type=query\_then\_fetch&batched\_reduce\_size=512], status line [HTTP/1.1 401 Unauthorized]  
> Can someone please help here?  
> Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 2, 2020, 1:00pm UTC](https://discuss.elastic.co/t/authenticate-elasticsearch-from-spring-boot/226196/2 "2020-04-02T13:00:58Z")

</div>

We don't support here this project. I think they have a dedicated forum for this.

But the URL looks weird here:

```
/api/myendpoint

```

Are you running behind a proxy?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 30, 2020, 1:01pm UTC](https://discuss.elastic.co/t/authenticate-elasticsearch-from-spring-boot/226196/3 "2020-04-30T13:01:04Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
