2

我尝试使用此类https://subversion.jfrog.org/jfrog/build-info/trunk/build-info-client/src/main/java/org/jfrog/build/为 Basic Auth 保护的 Solr 进行 PreEmptive Authentication client/PreemptiveHttpClient.java和 Solr ,但这些方法已被弃用,所以我不知道这是否是一个问题。情况是在 Solr 中查询很好,但是对于索引,我在与服务器交谈时发生 IOException:example.com:8983/solr/core1。

HttpSolrClient 构造函数需要一个 httpClient 作为参数来进行抢先授权,因此对于上面的类,因为 httpClient 存储在一个私有变量中,所以我在该变量上使用了一个 getter 来获取 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

4

修复 Paulius 的代码,HttpClient 4.3 的抢占式身份验证。在需要连接到 Solr 时调用 createHttpClient 的类中创建方法。

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(new PreemptiveAuthInterceptor());
    return clientBuilder.build();
}

static class PreemptiveAuthInterceptor implements HttpRequestInterceptor {
    @Override
    public void process (HttpRequest request, HttpContext context) throws HttpException {
        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);
        }
    }
}
于 2015-07-14T07:12:59.963 回答
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 回答