# Unable Connect Elastic From Java Rest Client using Certificate

**URL:** <https://discuss.elastic.co/t/unable-connect-elastic-from-java-rest-client-using-certificate/154107>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-security\
**Created:** [October 26, 2018, 6:23am UTC](https://discuss.elastic.co/t/unable-connect-elastic-from-java-rest-client-using-certificate/154107 "2018-10-26T06:23:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![prashantgupta048](https://avatars.discourse-cdn.com/v4/letter/p/ed655f/32.png) [@prashantgupta048](https://discuss.elastic.co/u/prashantgupta048)\
**Post date:** [October 26, 2018, 6:23am UTC](https://discuss.elastic.co/t/unable-connect-elastic-from-java-rest-client-using-certificate/154107/1 "2018-10-26T06:23:05Z")

</div>

Hello Elasticsearch Team,  
I am trying to connect the Elastic search 5.6 using Java rest client. I want to connect it using certificate not by using Username/Password below is the code I am Using can someone please help me

```auto
public static void main(String args[])
    {
       

        try {

            String host = " ****";
            int port = 1234;

            String protocol = "https";

            String keyStorePass = "no_passwd";
            String keystorePath="\\anywhere\\loga.client.truststore.jks";
       

            System.setProperty("javax.net.ssl.trustStore", keystorePath);
             System.setProperty("javax.net.ssl.trustStorePassword", keyStorePass);
            File keyStoreFile = new File(keystorePath);
            char[] keyStorePass_char = keyStorePass.toCharArray();

            SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(keyStoreFile,keyStorePass_char, null);
            final SSLContext sslContext = sslBuilder.build();

            RestClientBuilder builder = RestClient.builder(new HttpHost(host, port,protocol))
                    .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                        @Override
                        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                            return httpClientBuilder.setSSLContext(sslContext);
                        }
                    });
            RestClient client = builder.build();

            HttpEntity entity = new NStringEntity("{\n"
                    + " \"query\": {\n"
                    + " \"match_all\": {}\n"
                    + " }\n"
                    + "\n"
                    + "}", ContentType.APPLICATION_JSON);
  

            Response indexResponse = null;

            HttpEntity query = new NStringEntity(""
                    + "{\n"
                    + " \"query\": {\n"
                    + " \"bool\": {\n"
                    + " \"must\": [\n"
                    + " {\n"
                    + " \"match\": {\n"
                    + " \"type\": \"employee\"\n"
                    + " }\n"
                    + " },\n"
                    + " {\n"
                    + " \"match\": {\n"
                    + " \"empid\": 1234\n"
                    + " }\n"
                    + " }\n"
                    + " ]\n"
                    + " }\n"
                    + " }\n"
                    + "}");

            indexResponse = client.performRequest(
                    "POST",
                    "/emp_index*/logs/_search",
                    Collections.<String, String>emptyMap(),query);

            System.out.println(EntityUtils.toString(indexResponse.getEntity()));
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        }

    }

```

I am getting Below Error

{"error":{"root\_cause":[{"type":"security\_exception","reason":"action [indices:data/read/search] requires authentication","header":  
{"WWW-Authenticate":"Basic realm="security" charset="UTF-8""}}],"type":"security\_exception","reason":"action [indices:data/read/search]  
requires authentication","header":{"WWW-Authenticate":"Basic realm="security" charset="UTF-8""}},"status":401}

But when I am passing username/pwd it is working.

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [October 29, 2018, 1:13am UTC](https://discuss.elastic.co/t/unable-connect-elastic-from-java-rest-client-using-certificate/154107/2 "2018-10-29T01:13:23Z")

</div>

Have you configured Elasticsearch to accept authentication via PKI certificate?  
Can you provide your `elasticsearch.yml` file?

---

<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:** [November 26, 2018, 1:27am UTC](https://discuss.elastic.co/t/unable-connect-elastic-from-java-rest-client-using-certificate/154107/3 "2018-11-26T01:27:39Z")

</div>

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