In our previous article Setup Basic Auth plugin  we have discussed about how to enable solr base authentication plugin.In this article we will discuss how to access basic authentication enable solr from solrj java client api.

Access Solr without any settings

We have execute simple match all docs query to check whether solr give result without any settings.Refer below code.

System.setProperty("javax.net.ssl.keyStore", "D:\\solr-7.1.0\\solr-7.1.0\\bin\\solr-ssl.keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "secret");
System.setProperty("javax.net.ssl.trustStore", "D:\\solr-7.1.0\\solr-7.1.0\\bin\\solr-ssl.keystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "secret");
HttpSolrClient httpSolrClient = new HttpSolrClient.Builder("https://localhost:8983/solr/SecureSolrTest").build();
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("*:*");
solrQuery.set("fl","*");
solrQuery.setRows(10);
try {
    
} catch (SolrServerException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Above code will throw RemoteSolrException with HTTP error code 401.Refer complete stack trace as below.

Exception in thread "main" org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at https://localhost:8983/solr/SecureSolrTest: Expected mime type application/octet-stream but got text/html. <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 401 require authentication</title>
</head>
<body><h2>HTTP ERROR 401</h2>
<p>Problem accessing /solr/SecureSolrTest/select. Reason:
<pre>    require authentication</pre></p>
</body>
</html>
  at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:590)
  at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:253)
  at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:242)
  at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:178)
  at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:942)
  at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:957)
  at com.javadeveloperzone.SolrClientDemo.pingRequestDemo(SolrClientDemo.java:30)
  at com.javadeveloperzone.SolrClientDemo.main(SolrClientDemo.java:42)

Possible Solutions

Possible solution to avoid such exception is set basic authentication credentials to every solr request as below.

SolrRequest req ;//create a new request object
req.setBasicAuthCredentials(userName, password);
solrClient.request(req);

Here is the complete example of solr with basic authentication enable. we have set use username and password as solr and SolrRocks respectively.

System.setProperty("javax.net.ssl.keyStore", "D:\\solr-7.1.0\\solr-7.1.0\\bin\\solr-ssl.keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "secret");
System.setProperty("javax.net.ssl.trustStore", "D:\\solr-7.1.0\\solr-7.1.0\\bin\\solr-ssl.keystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "secret");
HttpSolrClient httpSolrClient = new HttpSolrClient.Builder("https://localhost:8983/solr/SecureSolrTest").build();
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("*:*");
solrQuery.set("fl","*");
solrQuery.setRows(10);
try {
    QueryRequest queryRequest = new QueryRequest(solrQuery);
    queryRequest.setBasicAuthCredentials("solr","SolrRocks");
    QueryResponse solrResponse = queryRequest.process(httpSolrClient);
    System.out.println(solrResponse);
    System.out.println("Total Documents : "+solrResponse.getResults().getNumFound());
} catch (SolrServerException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Output

{responseHeader={status=0,QTime=559,params={q=*:*,fl=*,rows=10,wt=javabin,version=2}},response={numFound=10,start=0,docs=[]}}
Total Documents : 10

Refer Solr Reference Guide for more details.

 

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *