Set HttpBasicAuth credentials through code when using SolrJ


When using Solr Basic Authentication we need to set HttpBasicAuth credentials at JVM level(explained here) at the Client side to authenticate all requests. To do this we need to pass in JVM args

-Dsolr.httpclient.builder.factory=org.apache.solr.client.solrj.impl.PreemptiveBasicAuthClientBuilderFactory

and set following HttpBasicAuth credentials in the properties file

httpBasicAuthUser=my_username
httpBasicAuthPassword=secretPassword

But sometimes there is not an option to store above credentials in properties files but it can be pulled from other secure service like AWS Secrets Manager. In such cases we need to set the above properties in the code, Following is a piece of code which does exactly that.

final ModifiableSolrParams params = new ModifiableSolrParams();
params.set("httpBasicAuthUser", my_username);
params.set("httpBasicAuthPassword", secretPassword);
PreemptiveBasicAuthClientBuilderFactory.setDefaultSolrParams(params);
final CloseableHttpClient httpClient = HttpClientUtil.createClient(params);
final CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder(Arrays.asList(solrZkHosts), Optional.empty()).withHttpClient(httpClient).build();

Happy Searching!

Advertisement

About Dominic

J for JAVA more about me : http://about.me/dominicdsouza
This entry was posted in Thechy Stuff and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s