在接受NTLM和摘要式身份验证的端点上,Java URL NTLM身份验证失败

时间:2016-04-25 20:59:42

标签: java authentication ntlm

我的端点同时接受NTLM和摘要式身份验证。此代码似乎试图回退到Digest,而不是正确使用NTLM:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;

public class NT {

    static final String kuser = "[USERNAME]"; // your account name
    static final String kpass = "[PASSWORD]"; // retrieve password for your account 

    static class MyAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            // I haven't checked getRequestingScheme() here, since for NTLM
            // and Negotiate, the usrname and password are all the same.
            System.err.println("Feeding username and password for " + getRequestingScheme());
            return (new PasswordAuthentication(kuser, kpass.toCharArray()));
        }
    }

    public static void main(String[] args) throws Exception {
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        Authenticator.setDefault(new MyAuthenticator());
        URL url = new URL("http://sitethatneedsntlmauthentication");
        URLConnection connection = url.openConnection();
        InputStream ins = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
        String str;
        while((str = reader.readLine()) != null)
            System.out.println(str);
    }
}

输出:

Feeding username and password for Negotiate
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Feeding username and password for digest
Exception in thread "main" java.net.ProtocolException: Server redirected too man
y  times (20)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown S
ource)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown So
urce)
        at NT.main(nt.java:26)

服务器正在发回以下接受标头:

WWW-Authenticate: NTLM
WWW-Authenticate: Negotiate
WWW-Authenticate: Digest realm="site.company.com", ... [other metadata]

如果我针对只接受NTLM的URL尝试此代码,则此代码可以正常工作,因此我知道凭据正在运行。

0 个答案:

没有答案