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


and set following HttpBasicAuth credentials in the properties file


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);
final CloseableHttpClient httpClient = HttpClientUtil.createClient(params);
final CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder(Arrays.asList(solrZkHosts), Optional.empty()).withHttpClient(httpClient).build();

Happy Searching!


About Dominic

J for JAVA more about me :
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: Logo

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

Facebook photo

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

Connecting to %s