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?

9
  • 1
    What is the problem? instead of CommonsHttpSolrServer just use HttpSolrServer. Commented Dec 21, 2012 at 9:59
  • @parvin CommonsHttpSolrServer is not in solrj4.jar so I need a new method using HttpSolrServer instead of CommonsHttpSolrServer. Commented Dec 21, 2012 at 10:00
  • @parvin In HttpSolrServer getHttpClient() method is not avilable Commented Dec 21, 2012 at 10:02
  • It is available. Check this out. lucene.apache.org/solr/api-4_0_0-BETA/org/apache/solr/client/… Commented Dec 21, 2012 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 Commented Dec 21, 2012 at 10:08

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
+1 for your effect. Thank you, Finally I find the Solution Myself
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);

Comments

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.

1 Comment

this works for read operations but not for solr write.(since the connection is not preemptive)
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.