3

solrj を使用して solr に接続しようとしています。私の solr インスタンスは jetty で実行され、基本認証で保護されています。関連情報を含むこれらのリンクを見つけました。

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

Apache HttpClient 4 を使用したプリエンプティブ Basic 認証 ただし、次の例外が引き続き発生します。

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);
            }
        }

    }

誰かが私を正しい方向に向けることができますか? ありがとう !!

4

3 に答える 3

6

部分的なドキュメントの更新を実装するときに同じ問題が発生しました。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);
                }

            }

        }
于 2013-06-26T08:14:02.823 に答える
0

Solr Wiki のSolr Security - SolrJセクションによると、次のことができるはずです。

 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();

 }
于 2013-03-15T12:16:47.643 に答える
0

HttpClientUtil の JAR solr-solrj-4.0.0.jarを追加する必要があります。

次に、以下のコードを使用します。

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

それはJdk 1.6とtomcat 6でうまくいきました

于 2015-09-18T10:23:28.013 に答える