使用HttpClient 4.3和Solr 5进行抢先认证

时间:2015-07-08 09:23:27

标签: solr httpclient basic-authentication preemptive

我尝试使用此类https://subversion.jfrog.org/jfrog/build-info/trunk/build-info-client/src/main/java/org/jfrog/build/client/PreemptiveHttpClient.java和Solr对基本Auth保护的Solr进行PreEmptive身份验证,但方法已弃用,因此我不知道这是否是一个问题。在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示例4.6使用HttpClient 4.3进行抢先授权,但这是一个测试用例,我没有看到传递HttpClient的方法,因此我可以进行抢占式身份验证。< / p>

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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服务器,你的请求将被抢先认证。