Solr的4基本身份验证(Solr 4 with basic authentication)

2019-07-21 16:15发布

我尝试连接使用solrj到Solr。 我Solr的实例在运行码头,并与基本身份验证保护。 我发现,包含相关信息的这些链接。

http://grokbase.com/t/lucene/solr-user/1288xjjbwx/http-basic-authentication-with-httpsolrserver

与Apache HttpClient的4先发制人的基本身份验证不过,我仍然得到以下异常:

Caused by: org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:822)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:352)
... 5 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:625)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:464)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
... 8 more

我还附上我使用的代码片段。

public static void main(String[] args) throws SolrServerException, IOException {

    HttpSolrServer server = new HttpSolrServer("http://localhost:8983/solr/");

    DefaultHttpClient m_client =(DefaultHttpClient)server.getHttpClient(); 
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(USERNAME, PASSWORD);

    m_client.addRequestInterceptor(new PreemptiveAuthInterceptor(),0);
    (((DefaultHttpClient)m_client).getCredentialsProvider()).setCredentials(new AuthScope("localhost",8983), credentials);

    SolrInputDocument document = new SolrInputDocument();
    document.addField("id",123213);
    server.add(document);
    server.commit();

}

}

class PreemptiveAuthInterceptor implements HttpRequestInterceptor {

    public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
        AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

        // If no auth scheme avaialble yet, try to initialize it
        // preemptively
        if (authState.getAuthScheme() == null) {
            AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
            CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
            HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
            if (authScheme != null) {
                Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
                if (creds == null) {
                    throw new HttpException("No credentials for preemptive authentication");
                }
                authState.setAuthScheme(authScheme);
                authState.setCredentials(creds);
            }
        }

    }

任何人都可以点我在正确的方向? 谢谢 !!

Answer 1:

实现部分文档更新时,我有同样的问题。 我通过实施PreemptiveAuthInterceptor解决了这个问题。 看下面的代码

PoolingClientConnectionManager cxMgr = new PoolingClientConnectionManager(
                        SchemeRegistryFactory.createDefault());
                cxMgr.setMaxTotal(100);
                cxMgr.setDefaultMaxPerRoute(20);

                DefaultHttpClient httpclient = new DefaultHttpClient(cxMgr);
                httpclient.addRequestInterceptor(
                        new PreemptiveAuthInterceptor(), 0);
                httpclient.getCredentialsProvider().setCredentials(
                        AuthScope.ANY,
                        new UsernamePasswordCredentials(solrDto.getUsername(),
                                solrDto.getPassword()));

                HttpSolrServer solrServerInstance = new HttpSolrServer(solrDto.getUrl(),
                        httpclient);
                solrServerInstance.setRequestWriter(new BinaryRequestWriter());
                solrServerInstance.setAllowCompression(true);

您还需要:

    private class PreemptiveAuthInterceptor implements HttpRequestInterceptor {

            public void process(final HttpRequest request, final HttpContext context)
                    throws HttpException, IOException {
                AuthState authState = (AuthState) context
                        .getAttribute(ClientContext.TARGET_AUTH_STATE);

                // If no auth scheme avaialble yet, try to initialize it
                // preemptively
                if (authState.getAuthScheme() == null) {
                    CredentialsProvider credsProvider = (CredentialsProvider) context
                            .getAttribute(ClientContext.CREDS_PROVIDER);
                    HttpHost targetHost = (HttpHost) context
                            .getAttribute(ExecutionContext.HTTP_TARGET_HOST);
                    Credentials creds = credsProvider.getCredentials(new AuthScope(
                            targetHost.getHostName(), targetHost.getPort()));
                    if (creds == null)
                        throw new HttpException(
                                "No credentials for preemptive authentication");
                    authState.setAuthScheme(new BasicScheme());
                    authState.setCredentials(creds);
                }

            }

        }


Answer 2:

按照Solr的安全- SolrJ你应该能够做到以下几点上Solr的维基部分:

 public static void main(String[] args) throws SolrServerException, IOException {

     HttpSolrServer server = new HttpSolrServer("http://localhost:8983/solr/");

     HttpClientUtil.setBasicAuth(server.getHttpClient(), USERNAME, PASSWORD);

     SolrInputDocument document = new SolrInputDocument();
     document.addField("id",123213);
     server.add(document);
     server.commit();

 }


Answer 3:

您需要添加的JAR Solr的-solrj-4.0.0.jar为HttpClientUtil。

然后使用下面的代码:

HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/"+url);     
HttpClientUtil.setBasicAuth((DefaultHttpClient) solrServer.getHttpClient(), "USERNAME", "PASSWORD");

这为我工作在JDK 1.6和Tomcat 6



文章来源: Solr 4 with basic authentication