When Sun released the 1.4.2 version of the JDK, they slipped in support for native NTLM authentication on Windows. This is done by retrieving the logged-on user’s username and password from the OS whenever an NTLM challenge is received. The only information that needs to be supplied is the NT domain. Here is an example that authenticates through an MS proxy, using my currently active credentials.
package uk.co.researchkitchen.ntlm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class TestJDKNTLM {
private void execute() throws IOException {
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "proxy1.researchkitchen.co.uk");
System.setProperty("http.proxyPort", "8080");
System.setProperty("http.auth.ntlm.domain", "MYDOMAIN");
URL url = new URL("http://www.yahoo.com");
URLConnection conn = url.openConnection();
conn.setAllowUserInteraction(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
int read = 0;
String body = null;
do {
body = reader.readLine();
System.out.println(body);
} while (body != null);
}
public static void main(String[] args) throws IOException {
new TestJDKNTLM().execute();
}
} |