当前位置: 首页 > 知识库问答 >
问题:

使用HttpClient4.3和Solr5进行抢占式身份验证

陶博涉
2023-03-14

我尝试使用https://subversion.jfrog.org/jfrog/build-info/trung/build-info-client/src/main/java/org/jfrog/build/client/preemptiveHttpClient.java和Solr类对一个受Auth保护的基本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示例4.6使用HttpClient 4.3进行抢先授权,但这是一个测试用例,我看不到通过HttpClient进行抢先授权的方法

共有1个答案

公良渝
2023-03-14

修正Paulius的代码,对HttpClient4.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);
        }
    }
}
 类似资料:
  • 问题内容: 是否有比此处描述的更简单的方法来设置HTTP客户端以进行抢占式基本身份验证? 在以前的版本(3.x)中,它曾经是一个简单的方法调用(例如 我要避免的主要事情是将BasicHttpContext添加到我执行的每个方法中。 问题答案: 不每次都传递上下文很难做到这一点,但是你可以使用请求拦截器来做到这一点。这是我们使用的一些代码(从JIRA iirc中找到):

  • 问题内容: 使用HttpUrlConnection使用抢占式基本HTTP身份验证的最佳方法是什么。(假设现在我不能使用HttpClient)。 编辑以澄清问题:我使用Base64编码在请求标头中正确设置了un / pw。是否需要设置任何其他标志或属性,或者我是否在为请求的基本身份验证头设置抢先式基本身份验证所需的全部信息? 问题答案: 如果您使用的是Java 8或更高版本,则可以使用: 然后正常使

  • 问题内容: 我想为在Raspberry Pi上运行并像本地服务器一样工作的软件制作一些更新脚本。该服务器应连接到Web上的主服务器,以获取软件更新并验证软件的许可证。为此,我设置了两个python脚本。我希望它们通过TLS套接字连接。然后,客户端检查服务器证书,然后服务器检查它是否是授权的客户端之一。我在此页面上找到了解决方案。 现在还有一个问题。我想知道 哪个客户端 (取决于证书)正在建立连接。

  • 但请求呢?和是用户的属性,但应将它们发送到endpoint。如果我将资源发送到endpoint,则没有多大意义。 对此有没有办法,遵循JSONAPI并保持API的意义?

  • 我正在尝试使用urllib3连接到网页。代码如下所示。 如果我们假设url是需要使用用户名和密码进行身份验证的某个网页,那么我是否使用正确的代码进行身份验证? 我使用urllib2做这件事很舒服,但使用urllib3做不到同样的事情。 非常感谢

  • jwt不应该仅仅用于认证用户吗?我读到过可以在里面存储非敏感的东西,比如用户ID。将权限级别之类的东西存储在令牌中可以吗?这样我可以避免数据库调用。