写操作时Solrj认证失败

刚刚在Jetty服务器上使用密码保护的solr。 我可以使用solrj从solr读取/访问数据: – >

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

但它给了我I / O例外如下。 有关身份validation的其他示例,但我不知道如何在Solrj中使用身份validation。 以下错误仅在我尝试更新记录时出现(并且可能添加记录,尚未测试)

 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 

类似的问题已经问过,你可以看看下面的链接,以获得一些想法。

  • Solr – 使用Httpclient实例化HttpSolrServer
  • Solr Change CommonsHttpSolrServer到HttpSolrServer

发生这种情况只是因为在执行POST或DELETE调用时没有发送身份validation参数,因此您需要在http客户端中解决此问题

我正在使用solr 6.2.0及其相应的java客户端

所以我创建了一个新的SolrHttp客户端,如下所示

 public class TEHttpSolrClient extends HttpSolrClient { private static final String UTF_8 = StandardCharsets.UTF_8.name(); public TEHttpSolrClient(String baseURL) { super(baseURL); } @Override public NamedList 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 request(final SolrRequest request, final ResponseParser processor, String collection) throws SolrServerException, IOException { HttpRequestBase method = createMethod(request, collection); String userPass = ":"; 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); } } 

另外要调用客户端,您应该像下面这样调用它

 private static SolrClient solr = new TEHttpSolrClient.Builder("").build();