2

I want to set authentication for my SolrCloud. I use SolrJ6.5.0 in java. My CloudSolrClient is defined by directly using Zookeeper host:

 CloudSolrClient serverCloud = new CloudSolrClient.Builder().
                               withZkHost(connectionStr).build();

I saw useful codes of Deepak Shrivastava and Brian teggart for authenticating solr.

@Deepak Shrivastava used HttpSolrClientand @Brian teggart used AuthScope.

I have zookeeper host connection string instead of solr core url, so I cannot use these codes.

How can I set authentication for my solrCloud?

Zahra Aminolroaya
  • 520
  • 1
  • 5
  • 28

1 Answers1

3

I have also research this issue for a week. You can try the following:

public HttpClient httpClient() {
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("solr username", "solr password");
    provider.setCredentials(AuthScope.ANY, credentials);
    return HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
}

public CloudSolrClient solrClient(HttpClient httpClient) {
    CloudSolrClient solrClient = new CloudSolrClient.Builder()
            .withZkHost("your zkHost").withHttpClient(httpClient()).build();
    solrClient.setDefaultCollection("your collection");
    return solrClient;
}
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Sudheesh Singanamalla Jan 29 '18 at 17:50
  • Thanks for the answer. when I use this code, I got the following error: `java.lang.UnsupportedOperationException at org.apache.http.impl.client.InternalHttpClient.getParams(InternalHttpClient.java:210) at org.apache.solr.client.solrj.impl.HttpClientUtil.setSoTimeout(HttpClientUtil.java:309) at org.apache.solr.client.solrj.impl.LBHttpSolrClient.setSoTimeout(LBHttpSolrClient.java:533) at org.apache.solr.client.solrj.impl.CloudSolrClient.setSoTimeout(CloudSolrClient.java:202) ` – Zahra Aminolroaya Jan 31 '18 at 05:53
  • @ZahraAminolroaya This error can be version httpclient not support. Please make sure you use httpclient version 4.5.3 – Nguyen Tri Nghia Feb 03 '18 at 16:20
  • @SudheeshSinganamalla Thank you for the comment. This case is quite simple to understand, I will explain in more detail for complex cases. – Nguyen Tri Nghia Feb 03 '18 at 16:29
  • @NguyenTriNghia Thanks. I cannot change the version due to our project is based on th httpclient 4.4. Besides, getParams is deprecated in the newer versions of httpclient. – Zahra Aminolroaya Feb 04 '18 at 08:45