用于基本认证的Solr6.3.0 SolrJ API

问题描述 投票:1回答:2

我正在尝试使用SolrJ API使用以下代码连接SolrCloud:

  String zkHostString = "localhost:9983"; 
  String USER = "solr"; 
  String PASSWORD = "SolrRocks"; 


  CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); 
  credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER, PASSWORD)); 
  CloseableHttpClient httpClient =    HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build(); 

  CloudSolrClient solr = new CloudSolrClient.Builder().withZkHost(zkHostString).withHttpClient(httpClient).build(); 
  ((CloudSolrClient)solr).setDefaultCollection("gettingstarted"); 

但得到错误为:

异常线程 “main” org.apache.solr.client.solrj.impl.CloudSolrClient $ RouteException:在org.apache.solr.client.solrj.impl.CloudSolrClient.directUpdate(CloudSolrClient http://192.168.0.104:8983/solr/gettingstarted_shard2_replica1:IOException异常在谈论服务器时发生。 java:767)org.apache.solr.client.solrj.impl.CloudSolrClient.sendRequest(CloudSolrClient.java:1173)at org.apache.solr.client.solrj.impl.CloudSolrClient.requestWithRetryOnStaleState(CloudSolrClient.java:1062) org.apache.solr.client.solrj.impl.CloudSolrClient.request(CloudSolrClient.java:1004)位于org.apache.solr的org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:149)位于com.app.graphiti.TextParser.main(TextParser.java)的org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:190)中的.client.solrj.SolrClient.add(SolrClient.java:173) :92)产生的原因:org.apache.solr.client.solrj.SolrServerException:在org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java http://192.168.0.104:8983/solr/gettingstarted_shard2_replica1:IOException异常在交谈服务器时发生:607)在org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:262)在org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:251)在org.apache中的org.apache.solr.client.solrj.impl.LBHttpSolrClient.doRequest(LBHttpSolrClient.java:435)位于org.apache的org.apache.solr.client.solrj.impl.LBHttpSolrClient.request(LBHttpSolrClient.java:387)。 solr.client.solrj.impl.CloudSolrClient.lambda $ directUpdate $ 0,以org.apache.solr.common.util.ExecutorUtil $ MDCAwareThreadPoolExecutor(CloudSolrClient.java:742)在java.util.concurrent.FutureTask.run(来源不明)。 lambda $执行$ 0(ExecutorUtil.java:229)java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)java.lang.Thread.run(未知来源)由org.apache.http.impl.cl执行中的org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)中的org.apache.http.client.ClientProtocolException引起:org.apache.http.impl.client.CloseableHttpClient。执行(CloseableHttpClient.java:82)org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java: 498)... 10更多引起:org.apache.http.client.NonRepeatableRequestException:无法使用不可重复的请求实体重试请求。 at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:225)org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)org.apache.http.impl .execchain.RetryExec.execute(RetryExec.java:88)在org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)在org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient .java:184)... 13更多16:55:40.289 [main-SendThread(0:0:0:0:0:0:0:1:9983)] DEBUG org.apache.zookeeper.ClientCnxn - 得到ping对于sessionid的响应:0x15a3bc76e1f000e在1ms之后16:55:43.624 [main-SendThread(0:0:0:0:0:0:0:1:9983)] DEBUG org.apache.zookeeper.ClientCnxn - 获得了sessionid的ping响应:0x15a3bc76e1f000e后1ms的16:55:46.958 [主SendThread(0:0:0:0:0:0:0:1:9983)] DEBUG org.apache.zookeeper.ClientCnxn - 为的sessionid GOT ping响应:后0x15a3bc76e1f000e 1ms

请帮忙。 Vrinda Davda

authentication solr basic-authentication solrj
2个回答
1
投票

我相信这里发生的事情是SolrJ正在使用POST来添加你的文档,并且没有进行抢占式身份验证。这曾经更容易,但现在似乎要求你使用HttpClientBuilder提供CredentialsProviderHttpRequestInterceptor,必要时,用新的TARGET_AUTH_STATEBasicScheme更新请求上下文的UsernamePasswordCredentials

我一天大部分时间都在和自己打架,直到我发现Brian Teggart在2015年3月12日的帖子中发帖:

Preemptive Basic authentication with Apache HttpClient 4

这对我来说对Solr 6.4.1服务器和SolrJ 5.3.1客户端起作用。


0
投票

我找到了一种稍微简单的方法。

您可以像这样添加请求拦截器,这样您就不必担心自己创建一个正确配置的HttpClient实例。这只会将拦截器添加到Solrj创建的默认HttpClient中。

org.apache.solr.client.solrj.impl.HttpClientUtil.addRequestInterceptor(new SolrPreemptiveAuthInterceptor());

RequestInterceptor看起来像这样:

public class SolrPreemptiveAuthInterceptor implements HttpRequestInterceptor {

    final static Logger log = LoggerFactory.getLogger(SolrPreemptiveAuthInterceptor.class); 

    @Override
    public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
         AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
            // If no auth scheme available yet, try to initialize it preemptively
            if (authState.getAuthScheme() == null) {
                log.info("No AuthState: set Basic Auth");

                HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
                AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());

                CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);

                Credentials creds = credsProvider.getCredentials(authScope);
                if(creds == null){
                    log.info("No Basic Auth credentials: add them");
                    creds = getCredentials(authScope);
                }
                authState.update(new BasicScheme(), creds);
            }

    }


    private Credentials getCredentials(AuthScope authScope) {


        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password);

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(authScope, creds);
        log.info("Creating Basic Auth credentials for user {}", user);

        return credsProvider.getCredentials(authScope);
    }

}

0
投票
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, password);
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
solrClient = new HttpSolrClient.Builder().withBaseSolrUrl(endpoint).withHttpClient(client).build();
© www.soinside.com 2019 - 2024. All rights reserved.