我正在尝试打开一个使用NTLM身份验证方案保护的URL的http连接。当我们使用Java 6时,这段代码已经正确工作了2年。我编写了一个小的Java程序来访问特定的URL,以使测试用例尽可能简单。
问题在于,我无法使程序在Linux上工作,并且在使用JDK 7版本时也是如此。Java尝试20次访问URL,然后我收到一个错误消息,告诉我服务器重定向太多次了。它在Linux和JDK 6上运行良好,在Windows 7上使用JDK 6或7也可以正常工作。
我检查并尝试了列在这里(以及其他很多)的解决方案:Getting "java.net.ProtocolException: Server redirected too many times" Error。它没有起作用。我还必须补充一点,当从浏览器访问URL时,我可以看到没有涉及到任何cookie。
以下是我尝试过的操作系统/ Java版本的详细信息:
成功:
当它失败时,我得到以下输出:
因此,由于服务器已注册到域中,它将其域作为NTLM协议的一部分发送回客户端。每次Java都会用变量“domainFromServer”替换我尝试强制的域,但由于用户存在于服务器上而不是服务器的域上,因此会失败。
我不确定该怎么处理这个问题。
问题在于,我无法使程序在Linux上工作,并且在使用JDK 7版本时也是如此。Java尝试20次访问URL,然后我收到一个错误消息,告诉我服务器重定向太多次了。它在Linux和JDK 6上运行良好,在Windows 7上使用JDK 6或7也可以正常工作。
我检查并尝试了列在这里(以及其他很多)的解决方案:Getting "java.net.ProtocolException: Server redirected too many times" Error。它没有起作用。我还必须补充一点,当从浏览器访问URL时,我可以看到没有涉及到任何cookie。
以下是我尝试过的操作系统/ Java版本的详细信息:
成功:
- Windows 7: Java(TM) SE Runtime Environment (版本 1.7.0_15-b03) (64 位)
- Windows 7: Java(TM) SE Runtime Environment (版本 1.7.0_10-b18) (64 位)
- Windows 7: Java(TM) SE Runtime Environment (版本 1.6.0_33-b04) (64 位)
- Redhat enterprise linux 6.4: Java(TM) SE Runtime Environment (版本 1.6.0_33-b04) (64 位)
失败:
- Redhat enterprise linux 6.4: Java(TM) SE Runtime Environment (版本 1.7.0-b147) (64 位)
- Redhat enterprise linux 6.4: Java(TM) SE Runtime Environment (版本 1.7.0_05-b06) (64 位)
- Redhat enterprise linux 6.4: Java(TM) SE Runtime Environment (版本 1.7.0_13-b20) (64 位)
- Redhat enterprise linux 6.4: Java(TM) SE Runtime Environment (版本 1.7.0_15-b03) (64 位)
Scheme:Negotiate
Scheme:ntlm
.... document content ....
Done
当它失败时,我得到以下输出:
Scheme:Negotiate
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
Scheme:ntlm
java.net.ProtocolException: Server redirected too many times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1635)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at TestWs.testWs(TestWs.java:67)
at TestWs.main(TestWs.java:20)
这是程序的源代码:
package com.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
public class TestWs {
public static void main(String[] args) throws Exception {
new TestWs().testWs();
}
public void testWs() {
try {
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
Authenticator.setDefault(new MyAuthenticator("username", "password"));
URL url = new URL("https://someurlprotectedbyntlmauthentication.com");
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while (true) {
String s = br.readLine();
if (s == null)
break;
System.out.println(s);
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class MyAuthenticator extends Authenticator {
private String httpUsername;
private String httpPassword;
public MyAuthenticator(String httpUsername, String httpPassword) {
this.httpUsername = httpUsername;
this.httpPassword = httpPassword;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("Scheme:" + getRequestingScheme());
return new PasswordAuthentication(httpUsername, httpPassword.toCharArray());
}
}
非常感谢您的帮助。
更新:
经过进一步调查,我发现如果使用域用户,身份验证可以正常工作,但如果使用本地用户,则无法正常工作。
JDK 7 中的此代码(类 com.sun.security.ntlm.Client)导致了我的麻烦:
public byte[] type3(byte[] type2, byte[] nonce) throws NTLMException {
if (type2 == null || (v != Version.NTLM && nonce == null)) {
throw new NullPointerException("type2 and nonce cannot be null");
}
debug("NTLM Client: Type 2 received\n");
debug(type2);
Reader r = new Reader(type2);
byte[] challenge = r.readBytes(24, 8);
int inputFlags = r.readInt(20);
boolean unicode = (inputFlags & 1) == 1;
String domainFromServer = r.readSecurityBuffer(12, unicode);
if (domainFromServer != null) {
domain = domainFromServer;
}
因此,由于服务器已注册到域中,它将其域作为NTLM协议的一部分发送回客户端。每次Java都会用变量“domainFromServer”替换我尝试强制的域,但由于用户存在于服务器上而不是服务器的域上,因此会失败。
我不确定该怎么处理这个问题。