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!