Adding BasicAuth Credentials to TransportClient

Hi, I have been trying to use TransportClient instead of HttpClient to connect to ElasticSearch. However I am not sure how basic credentials can be supplied to TransportClient to connect.
I know how this can be achieved with HttpClient. Could you please reply.

       Optional<Header> basicAuthHeader = Optional.empty();
    	
        @SuppressWarnings("resource")
		TransportClient transportClient = new PreBuiltTransportClient(Settings.builder()
                .put("cluster.name", "elasticsearch")
                .put("client.transport.sniff", true)
                .build())
                .addTransportAddress(new TransportAddress(InetAddress.getByName("myremotehost"), 9300));
        
               String credentials = "Basic "
                + Base64.getEncoder()
                        .encodeToString(new String(ESProperties.ES_USER_ID + ":" + ESProperties.ES_PWD).getBytes());

        basicAuthHeader = Optional.of(new BasicHeader(HttpHeaders.AUTHORIZATION, credentials));

Don't do that as TransportClient will be removed in the future. But if you really want it, read:

Thanks David,
I have tried the same. However I get the following exception when new PreBuiltXPackTransportClient is being called.

Exception in thread "main" java.lang.NoSuchMethodError: org.elasticsearch.common.settings.Settings$Builder.put([Ljava/lang/Object;)Lorg/elasticsearch/common/settings/Settings$Builder;
at org.elasticsearch.xpack.notification.email.Account.(Account.java:67)
at org.elasticsearch.xpack.XPackPlugin.(XPackPlugin.java:191)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.elasticsearch.plugins.PluginsService.loadPlugin(PluginsService.java:601)
at org.elasticsearch.plugins.PluginsService.(PluginsService.java:113)
at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:110)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:135)
at org.elasticsearch.client.transport.TransportClient.(TransportClient.java:288)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:128)
at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.(PreBuiltXPackTransportClient.java:55)
at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.(PreBuiltXPackTransportClient.java:50)
at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.(PreBuiltXPackTransportClient.java:46)

Here is my code snipped

     TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
    .put("cluster.name", "elasticsearch")
    .put("client.transport.sniff", false)
    .build())
    .addTransportAddress(new TransportAddress(InetAddress.getByName("MyRemoteHost"), 9300));

String token = basicAuthHeaderValue("myuserid", new SecureString("Mypwd".toCharArray()));

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

and my gradle config goes this way. I am not sure where am I making mistake.

compile group: 'org.elasticsearch.plugin', name: 'transport-netty4-client', version: '6.6.1'
compile group: 'org.elasticsearch.client', name: 'x-pack-transport', version: '6.0.0-alpha2'
compile group: 'org.elasticsearch.client', name: 'transport', version: '6.6.1'
compile group: 'org.elasticsearch.client', name: 'elasticsearch-rest-high-level-client', version: '6.6.1'

Thanks in advance
Arul.

You are mixing versions.
Just follow the guide and you should be good.

But again, don't do that and use the REST client.

Thank you. Rest Client works for me.

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