Apache HTTPClient DigestAuth不会转发质询中的“不透明”值

时间:2018-10-28 02:55:55

标签: apache-httpclient-4.x digest-authentication

我正在尝试对不受控制的第三方Web服务使用HTTP客户端的摘要式身份验证。

我从这里的示例代码开始:

http://hc.apache.org/httpcomponents-client-4.5.x/httpclient/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java 

在尝试执行下面所述的下一步之前,我已经针对httpbin.org进行了努力。

看来,我正在使用的目标第三方服务要求将opaque值从初始响应的WWW-Authentication标头复制到Authorization标头上下一个请求,如下所述:

https://security.stackexchange.com/questions/24425/what-is-the-opaque-field-in-http-digest-access-authentication-used-for

但是,我打开了电汇记录并逐步执行了代码(同样,这实际上只是上面链接的示例代码,无需在此处复制/粘贴),并且我发现opaque不是复制。

有什么想法阻止它被复制吗?

我什至尝试覆盖processChallenge方法:

    DigestScheme digestAuth = new DigestScheme() {
        @Override
        public void processChallenge(
                Header header) throws MalformedChallengeException {

,但是似乎在下一个请求中将忽略此时引入参数的任何值。

1 个答案:

答案 0 :(得分:0)

最后通过显式覆盖Authorize头进行修复,而不是依靠HttpClient的内部方法自动完成它:

package [...];

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.DigestScheme;
import org.apache.http.impl.client.*;
import org.testng.Assert;

public class DigestTest {

    private static final String URL
            = "https://...";

    private static final String PASSWORD = ...;

    private static final String USER = ...;

    public static void main(String[] args) throws Exception {

        new DigestTest().run();
    }

    public void run() throws Exception {

        HttpGet httpget = new HttpGet(URL);

        HttpHost target
                = new HttpHost(httpget.getURI().getHost(), 443, "https");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();

        UsernamePasswordCredentials credentials
                = new UsernamePasswordCredentials(USER, PASSWORD);
        credsProvider.setCredentials(
                new AuthScope(target.getHostName(), target.getPort()),
                credentials);

        CookieStore cookieStore = new BasicCookieStore();

        CloseableHttpClient httpclient
                = HttpClients.custom().setDefaultCookieStore(cookieStore)
                        .setDefaultCredentialsProvider(credsProvider).build();

        try {

            DigestScheme digestAuth = new DigestScheme();

            digestAuth.overrideParamter("qop", "auth");
            digestAuth.overrideParamter("nc", "0");
            digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce());

            AuthCache authCache = new BasicAuthCache();
            authCache.put(target, digestAuth);

            HttpClientContext localContext = HttpClientContext.create();
            localContext.setAuthCache(authCache);

            CloseableHttpResponse response;

            response = httpclient.execute(target, httpget, localContext);
            Map<String, String> wwwAuth = Arrays
                    .stream(response.getHeaders("WWW-Authenticate")[0]
                            .getElements())
                    .collect(Collectors.toMap(HeaderElement::getName,
                            HeaderElement::getValue));

            // the first call ALWAYS fails with a 401
            Assert.assertEquals(response.getStatusLine().getStatusCode(), 401);

            digestAuth.overrideParamter("opaque", wwwAuth.get("opaque"));
            digestAuth.overrideParamter("nonce", wwwAuth.get("nonce"));
            digestAuth.overrideParamter("realm", wwwAuth.get("Digest realm"));
            Header authenticate = digestAuth.authenticate(credentials, httpget,
                    localContext);
            httpget.addHeader(authenticate);

            response = httpclient.execute(target, httpget, localContext);

            // the 2nd call is the real deal
            Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);

            System.out.println(IOUtils
                    .toString(response.getEntity().getContent(), "utf-8"));

        } finally {
            httpclient.close();
        }
    }

}