使用HttpClient 4.3和Solr 5进行抢占式身份验证

使用HttpClient 4.3和Solr 5进行抢占式身份验证,solr,httpclient,basic-authentication,preemptive,Solr,Httpclient,Basic Authentication,Preemptive,我尝试使用这个类和Solr对一个基本的受身份验证保护的Solr进行抢占式身份验证,但是这些方法被弃用,所以我不知道这是否是一个问题。情况是,查询在Solr中很好,但对于索引,我在与服务器(example.com:8983/Solr/core1)对话时遇到了IOException HttpSolrClient构造函数需要一个httpClient作为参数来执行抢占式授权,因为httpClient存储在一个私有变量中,所以我使用该变量的getter来获取httpClient并传递给HttpSolrCl

我尝试使用这个类和Solr对一个基本的受身份验证保护的Solr进行抢占式身份验证,但是这些方法被弃用,所以我不知道这是否是一个问题。情况是,查询在Solr中很好,但对于索引,我在与服务器(example.com:8983/Solr/core1)对话时遇到了IOException

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

我知道示例4.6这样的示例可以使用HttpClient 4.3进行抢占式授权,但这是一个测试用例,我看不到通过HttpClient以便进行抢占式身份验证的方法。

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

公共静态HttpClient createHttpClientString用户名、字符串密码{ 如果username==null{ 用户名=; } 如果密码==null{ 密码=; } HttpClientBuilder clientBuilder=HttpClientBuilder.create; BasicCredentialsProvider=新的BasicCredentialsProvider; provider.setCredentialAuthScope.ANY,新用户名PasswordCredentialsUserName,密码; clientBuilder.setDefaultCredentialsProviderprovider; clientBuilder.addInterceptorFirstnew PreemptiveAuthInterceptor; 返回clientBuilder.build; } 静态类PreemptiveAuthInterceptor实现HttpRequestInterceptor{ @凌驾 public void进程HttpRequest请求,HttpContext上下文抛出HttpException{ AuthState AuthState=AuthState context.getAttributeHttpClientContext.TARGET\u AUTH\u STATE; 如果authState.getAuthScheme==null{ CredentialsProvider credsProvider=CredentialsProvider context.getAttributeHttpClientContext.CREDS\u PROVIDER; HttpHost targetHost=HttpHost context.getAttributeHttpCoreContext.HTTP\u TARGET\u HOST; 凭据凭据=credsProvider.GetCredentials新建AuthScopetargetHost.getHostName,targetHost.getPort; 如果凭据==null{ 抛出新的HttpException没有为抢占式身份验证提供凭据。; } authState.updatenew基本方案,凭证; } } }
您必须像这样创建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服务器,您的请求将被预先验证。

这看起来是更新版本的最佳代码,但我发现addInterceptorFirst参数的错误测试似乎是错误的,它说它接受HttpRequestInterceptor!也可与solrj 6.5.0和CloudSolrClient配合使用!ex:CloudSolrClient.Builder scb=新的CloudSolrClient.Builder;scb.WithHttpClient您的定制HTTPCLIENT;CloudSolrClient solr_client=scb.withZkHostZK_ADDRESS.build;这比文档要好。docs提供的仅有两种解决方案是为每个请求设置的,您不能使用典型的帮助器方法,如查询或更新。或者设置不总是方便的复杂系统属性。为什么我们不在SolrClient/Builder上提供一个简单的API setBasicAuthx,y?这感觉就像这应该是一行,使用自动完成找到它,完成。不要阅读大量的文档和堆垛溢出的帖子来找出随机的东西。