elasticsearchjava-clientelasticsearch-x-pack

How to use username and password (no SSL) in Java client to connect to Elasticsearch?


I am learning Elasticsearch (6.1.3) to use it in a Java-based web application.

I have installed X-Pack. Now I need to know how to program in the Java client by simply providing username and password without using SSL. I found out this page is helpful, but lacking:

https://www.elastic.co/guide/en/x-pack/current/java-clients.html

I am particularly interested in the following code snippet found in the above link:

TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
        .put("cluster.name", "myClusterName")
        .put("xpack.security.user", "transport_client_user:changeme")
        ...
        .build())
    .build()
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300))
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9301))

String token = basicAuthHeaderValue("test_user", new SecureString("changeme".toCharArray()));

client.filterWithHeader(Collections.singletonMap("Authorization", token))
    .prepareSearch().get();

Does this code snippet apply to my situation? Where is the password included?

I googled a lot, but not able to find a single complete example. I would really appreciate it for any info or links.


Solution

  • According to the XPack documentation on configuring the transport client to work with a secured ES cluster, the line you car about is this one:

        .put("xpack.security.user", "transport_client_user:changeme")
    

    and in there you can see that you have a username (transport_client_user) and a password (changeme), so you're good to go.