2

このクラスhttps://subversion.jfrog.org/jfrog/build-info/trunk/build-info-client/src/main/java/org/jfrog/build/を使用して、基本認証で保護されたSolrのプリエンプティブ認証を試みましたclient/PreemptiveHttpClient.javaおよび Solr ですが、メソッドは廃止されたため、これが問題であったかどうかはわかりません。状況は、Solr でクエリを実行しても問題ありませんが、インデックス作成の場合、example.com:8983/solr/core1 でサーバーと通信しているときに IOException が発生します。

HttpSolrClient コンストラクターは、プリエンプティブ認証を行うためのパラメーターとして httpClient を必要とします。そのため、上記のクラスでは、httpClient がプライベート変数に格納されているため、その変数でゲッターを使用して httpClient を取得し、HttpSolrClient コンストラクターに渡します。私がそれを正しく行ったかどうかもわかりません。

PreemptiveAuthenticate preemp = new PreemptiveAuthenticate("username", "password", 1);
    DefaultHttpClient httpClient = preemp.getHttpClient();
    System.out.println("Made it to connectSolr after set Authentication");
    SolrClient solr = new HttpSolrClient(urlString, httpClient);

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html Example 4.6のような HttpClient 4.3 でプリエンプティブ認証を行う例は知っていますが、これはテストケースであり、わかりませんプリエンプティブ認証を行えるように HttpClient を渡す方法。

4

2 に答える 2

0

次のように HttpClient を作成する必要があります。

public static HttpClient createHttpClient(String username, String password) {
    if (username == null) {
        username = "";
    }
    if (password == null) {
        password = "";
    }
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();

    BasicCredentialsProvider provider = new BasicCredentialsProvider();
    provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

    clientBuilder.setDefaultCredentialsProvider(provider);
    clientBuilder.addInterceptorFirst((HttpRequest request, HttpContext context) -> {
        AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
        if (authState.getAuthScheme() == null) {
            CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
            HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
            Credentials credentials = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (credentials == null) {
                throw new HttpException("No credentials provided for preemptive authentication.");
            }
            authState.update(new BasicScheme(), credentials);
        }
    });
    return clientBuilder.build();
}

次に、それをsolrサーバーに渡す必要があり、リクエストはプリエンプティブに認証されます.

于 2015-07-16T11:11:47.897 に答える