Solrj Authentication Fails On Write Operation

一个人想着一个人 提交于 2019-12-09 03:48:41

问题


Just password protected solr on Jetty server. I am able to read/access data from solr using solrj :->

HttpSolrServer solr = new HttpSolrServer("http://111.111.111:8983/solr/mysolr");
   HttpClientUtil.setBasicAuth((DefaultHttpClient) solr.getHttpClient(), "admin", "akshaybhatt");

but it gives me I/O Exception as below. There are other examples on SO about authentication but I have no idea how do I use authentication in Solrj. The below error comes only when I try to update a record (and possibly add a record, not tested yet)

org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://111.111.111.138:8983/solr/mysolrcore
    at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:507)
    at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:199)
    at org.apache.solr.client.solrj.request.AbstractUpdateRequest.process(AbstractUpdateRequest.java:118)
    at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:116)
    at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:102)
    at UpdateSolrTesting.AddToSolr(UpdateSolrTesting.java:228)
    at UpdateSolrTesting.performaction(UpdateSolrTesting.java:141)
    at UpdateSolrTesting.main(UpdateSolrTesting.java:101)
Caused by: org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:867)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:395)
    ... 7 more
Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity.
    at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:660)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:486)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
    ... 11 more

回答1:


Similar question asked already, you can look at the below links to get some idea.

  • Solr - instantiate HttpSolrServer with Httpclient
  • Solr Change CommonsHttpSolrServer To HttpSolrServer



回答2:


It is happening only because authentication parameter is not being sent while doing POST or DELETE calls, so so solution is you need to fix that in your http client

I am using solr 6.2.0 and its corresponding java client

So i created a new SolrHttp client which looks like below

public class TEHttpSolrClient extends HttpSolrClient {

private static final String UTF_8 = StandardCharsets.UTF_8.name();

public TEHttpSolrClient(String baseURL) {
    super(baseURL);
}

@Override
public NamedList<Object> request(final SolrRequest request, String collection) throws SolrServerException, IOException {
    ResponseParser responseParser = request.getResponseParser();
    if (responseParser == null) {
        responseParser = parser;
    }
    return request(request, responseParser, collection);
}

public NamedList<Object> request(final SolrRequest request, final ResponseParser processor, String collection)
        throws SolrServerException, IOException {
    HttpRequestBase method = createMethod(request, collection);
    String userPass = "<username>:<password>";
    String encoded = Base64.byteArrayToBase64(userPass.getBytes(UTF_8));
    // below line will make sure that it sends authorization token every time in all your requests
    method.setHeader(new BasicHeader("Authorization", "Basic " + encoded));
    return executeMethod(method, processor);
}

}

Also to call the client you should call it like below

private static SolrClient solr = new TEHttpSolrClient.Builder("<solr core url>").build();


来源:https://stackoverflow.com/questions/25852164/solrj-authentication-fails-on-write-operation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!