6

For Basic Authentication in solr 3.5 I am using the following code,

String url = "http://192.168.192.11:8080/solr/FormResponses";
CommonsHttpSolrServer server = new CommonsHttpSolrServer( url );
String username = "user";
String password = "user123";
Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
server.getHttpClient().getState().setCredentials(AuthScope.ANY, defaultcreds);
server.getHttpClient().getParams().setAuthenticationPreemptive(true);

In solr 4.0 CommonsHttpSolrServer is not available, so I want to replace it with HttpSolrServer. Can anyone help me to fix this?

Jayamurugan
  • 1,757
  • 2
  • 14
  • 34
  • 1
    What is the problem? instead of CommonsHttpSolrServer just use HttpSolrServer. – Parvin Gasimzade Dec 21 '12 at 09:59
  • @parvin CommonsHttpSolrServer is not in solrj4.jar so I need a new method using HttpSolrServer instead of CommonsHttpSolrServer. – Jayamurugan Dec 21 '12 at 10:00
  • @parvin In HttpSolrServer getHttpClient() method is not avilable – Jayamurugan Dec 21 '12 at 10:02
  • It is available. Check this out. http://lucene.apache.org/solr/api-4_0_0-BETA/org/apache/solr/client/solrj/impl/HttpSolrServer.html#getHttpClient() – Parvin Gasimzade Dec 21 '12 at 10:03
  • @parvin In CommonsHttpSolrServer if I use server.getHttpClient().getState() it is giving some value BUT IN HttpSolrServer if I use server.getHttpClient().getState() it shows error. Please run the above simple code and help me to fix – Jayamurugan Dec 21 '12 at 10:08
  • Can you add the stack trace of the exception? – Parvin Gasimzade Dec 21 '12 at 10:10
  • The type org.apache.http.client.HttpClient cannot be resolved. It is indirectly referenced from required .class files ---It is a compilation error – Jayamurugan Dec 21 '12 at 10:12
  • Add apache httpclient,httpcore and httpmime jars to your classpath. – Parvin Gasimzade Dec 21 '12 at 10:17
  • Still Not solved: Credentials defaultcreds = new UsernamePasswordCredentials("user", "user123"); HttpClient client = new HttpClient(); client .getState().setCredentials(AuthScope.ANY, defaultcreds); client.getParams().setAuthenticationPreemptive(true); HttpSolrServer serverNEW = new HttpSolrServer( url,client ); The constructor HttpSolrServer(String, HttpClient) is undefined – Jayamurugan Dec 21 '12 at 10:27

4 Answers4

8

Change the code as follows :

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.solr.client.solrj.impl.HttpSolrServer;

String url = "http://192.168.192.11:8080/solr/FormResponses";
HttpSolrServer server = new HttpSolrServer( url );
DefaultHttpClient client = (DefaultHttpClient) server.getHttpClient();

UsernamePasswordCredentials defaultcreds = new UsernamePasswordCredentials("user", "user123");
client.getCredentialsProvider().setCredentials(AuthScope.ANY, defaultcreds);

For server.getHttpClient().getParams().setAuthenticationPreemptive(true) in HttpClient 4 you can use the solution described here.

Community
  • 1
  • 1
Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
  • It shows java.lang.ClassCastException: org.apache.http.impl.client.DefaultHttpClient cannot be cast to org.apache.commons.httpclient.HttpClient ---Can you provide your import statements – Jayamurugan Dec 21 '12 at 12:29
  • 2
    +1 for your effect. Thank you, Finally I find the Solution Myself – Jayamurugan Dec 21 '12 at 12:54
2

Finally I find the answer my self,

String url = "http://192.168.192.11:8080/solr/FormResponses";
DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getCredentialsProvider().setCredentials(
    AuthScope.ANY, new UsernamePasswordCredentials("user", "user123"));
SolrServer solrServer = new HttpSolrServer(url, httpclient);
Jayamurugan
  • 1,757
  • 2
  • 14
  • 34
1

You need to add the JAR solr-solrj-4.0.0.jar for HttpClientUtil .

Then use below code :

HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/"+url);     
HttpClientUtil.setBasicAuth((DefaultHttpClient) solrServer.getHttpClient(),
                            "USERNAME", "PASSWORD");

That worked for me.

josliber
  • 43,891
  • 12
  • 98
  • 133
ThmHarsh
  • 601
  • 7
  • 7
  • this works for read operations but not for solr write.(since the connection is not preemptive) – Mani Aug 16 '17 at 07:03
1

This is the only way that works for me:

String url = "192.168.192.11:8080/solr/FormResponses";
String username = "user";
String password = "user123";
HttpSolrServer server = new HttpSolrServer("http://" + username + ":" + password + "@" + url);
Nadine
  • 1,620
  • 2
  • 15
  • 27