Образец HTTP-клиента Apache не проходит дайджест-аутентификацию

Я запускаю образец Apache hc (http-клиент) для дайджест-аутентификации. Я ничего не менял, просто использовал предоставленный образец:

public static void main(String[] args) throws Exception {
    HttpHost target = new HttpHost("httpbin.org", 80, "http");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials("user", "passwd"));
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();
    try {

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local
        // auth cache
        DigestScheme digestAuth = new DigestScheme();
        // Suppose we already know the realm name
        digestAuth.overrideParamter("realm", "[email protected]");
        // Suppose we already know the expected nonce value
        digestAuth.overrideParamter("nonce", "b2c603bb7c93cfa197945553a1044283");
        authCache.put(target, digestAuth);

        // Add AuthCache to the execution context
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

        HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd");

        System.out.println("Executing request " + httpget.getRequestLine() + " to target " + target);
        for (int i = 0; i < 3; i++) {
            CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
            } finally {
                response.close();
            }
        }
    } finally {
        httpclient.close();
    }
}

И я получаю: HTTP / 1.1 401 UNAUTHORIZED.

Если я перейду прямо на http://httpbin.org/digest-auth/auth/user/passwd запрашивает у меня пользователя / passwd, а затем предоставляет страницу. Значит, сайт работает правильно.

Есть идеи, что не так? У меня последняя версия библиотеки.

Fiddler Auth для браузера (успешно):

Заголовок авторизации прокси отсутствует.

Заголовок авторизации присутствует: дайджест username = "user", realm = "[email protected]", nonce = "8ada87344eb5a10bf810bcc211205c24", uri = "/ digest-auth / auth / user / passwd", response = "ad22423e5591d14cd90c6paeque64" = "361645844d957289c4c8f3479f76269f", qop = auth, nc = 00000001, cnonce = "260d8ddfe64bf32e"

Fiddler Auth для моего кода (не удалось):

Заголовок авторизации прокси отсутствует.

Заголовок авторизации присутствует: Digest username = "user", realm = "[email protected]", nonce = "76af6c9c0a1f57ee5f0fcade2a5f758c", uri = "http://httpbin.org/digest-auth/auth/user/passwd", response = "745686e3f38ab40ce5907d41f91823e6", qop = auth, nc = 00000001, cnonce = "634b618d5c8ac9af", алгоритм = MD5, opaque = "fe84ce11c48a7b258490600800e5e6df"


person David Thielen    schedule 27.12.2016    source источник
comment
Возможный дубликат аутентификации Apache HttpClient Digest   -  person Alex R    schedule 28.10.2018


Ответы (2)


В этот код digestAuth.overrideParamter("realm", "some realm") должны быть внесены некоторые изменения. Чтобы заменить "some realm" областью вашего сервера. Просмотрите этот вопрос

person cvi    schedule 27.12.2016
comment
Как я могу программно определить область? Этот код предназначен для библиотеки, которая будет работать на многих компьютерах, подключенных ко многим серверам. Так что я не могу это жестко запрограммировать. - person David Thielen; 27.12.2016
comment
См. Здесь, чтобы программно установить область stackoverflow.com/questions/53028003/ - person Alex R; 28.10.2018

Хорошо, у меня все заработало. Вы также должны установить cookie. Спасибо этому сообщению за помощь. Приведенный ниже код работает, но только если вы не используете Fiddler.

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

        CookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie("fake", "fake_value");
        cookie.setDomain("httpbin.org");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);

        // https://stackoverflow.com/questions/27291842/digest-auth-with-java-apache-client-always-401-unauthorized

        HttpHost target = new HttpHost("httpbin.org", 80, "http");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials("user", "passwd"));

        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCookieStore(cookieStore)
                .setDefaultCredentialsProvider(credsProvider)
//              .setProxy(new HttpHost("127.0.0.1", 8888))
                .build();
        try {

            // Create AuthCache instance
            AuthCache authCache = new BasicAuthCache();
            // Generate DIGEST scheme object, initialize it and add it to the local
            // auth cache
            DigestScheme digestAuth = new DigestScheme();
            // Suppose we already know the realm name
            digestAuth.overrideParamter("realm", "[email protected]");
            // Suppose we already know the expected nonce value
            digestAuth.overrideParamter("nonce", calculateNonce());
            authCache.put(target, digestAuth);

            // Add AuthCache to the execution context
            HttpClientContext localContext = HttpClientContext.create();
            localContext.setAuthCache(authCache);

            HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd");

            System.out.println("Executing request " + httpget.getRequestLine() + " to target " + target);
                CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
                try {
                    System.out.println("----------------------------------------");
                    System.out.println(response.getStatusLine());
                    System.out.println(EntityUtils.toString(response.getEntity()));
                } finally {
                    response.close();
                }
        } finally {
            httpclient.close();
        }
    }

    public static synchronized String calculateNonce() {

        Date d = new Date();
        SimpleDateFormat f = new SimpleDateFormat("yyyy:MM:dd:hh:mm:ss");
        String fmtDate = f.format(d);
        Random rand = new Random(100000);
        Integer randomInt = rand.nextInt();
        return org.apache.commons.codec.digest.DigestUtils.md5Hex(fmtDate + randomInt.toString());
    }
person David Thielen    schedule 28.12.2016