# X-pack security using java code

**URL:** <https://discuss.elastic.co/t/x-pack-security-using-java-code/102851>\
**Category:** Elasticsearch\
**Created:** [October 5, 2017, 2:29pm UTC](https://discuss.elastic.co/t/x-pack-security-using-java-code/102851 "2017-10-05T14:29:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pravin1](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@pravin1](https://discuss.elastic.co/u/pravin1)\
**Post date:** [October 5, 2017, 2:29pm UTC](https://discuss.elastic.co/t/x-pack-security-using-java-code/102851/1 "2017-10-05T14:29:46Z")

</div>

We are creating restclient for connection elastic server, and using HTTPentity for fetching data using index name  
check below code

RestClient restClient = RestClient.builder(new HttpHost(Constents.ELK\_URL, Constents.ELK\_PORT, "http")).build();

HttpEntity entity = null;  
entity = new NStringEntity(  
"{ \n"+  
""query": { \n"+  
""bool": { \n"+  
""must": [ \n"+  
" { \n"+

```
					            "\"match\": { \n"+
					             
					              "\"bank_client_entity_name.keyword\": { \n"+
					               " \"query\": \""+bankClientEntityName+"\" \n"+
					              "} \n"+
					              "} \n"+
					          "} \n"+
					        "] \n"+
					      "} \n"+
					      "}, \n"+
					      
						 "\"sort\": [ \n"+
						"{ \n"+
						"\"timeperiod_id\": {\"order\" : \"desc\"}, \n"+
						"\"date_status\": {\"order\" : \"desc\"} \n"+
						"} \n"+
						"], \n"+
						" \"size\" : 1 \n"+
					"}", ContentType.APPLICATION_JSON);

```

Response response = restClient.performRequest("GET", "/"+eLK\_HIST\_INDEX+"/\_search",Collections.\<String, String\>emptyMap(),entity);  
BufferedInputStream br = new BufferedInputStream(response.getEntity().getContent());

please tell me how to add security X-pack in this above code

---

<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:** [October 5, 2017, 2:46pm UTC](https://discuss.elastic.co/t/x-pack-security-using-java-code/102851/2 "2017-10-05T14:46:10Z")

</div>

Please format your code using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

Here we go: [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/\_basic\_authentication.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_basic_authentication.html)

---

<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 2, 2017, 2:46pm UTC](https://discuss.elastic.co/t/x-pack-security-using-java-code/102851/3 "2017-11-02T14:46:17Z")

</div>

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