I'm trying to connect to an url by proxy with NTLM authentication.
proxy = new Proxy( Proxy.Type.HTTP, new InetSocketAddress( host, 80 ) );
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
logger.info("Getting password authentication...");
return new PasswordAuthentication(
"DOMAIN\\user",
"password".toCharArray() ) ;
}
});
connection = (HttpURLConnection) url.openConnection( proxy );
String credentials = username + ":" + password;
String basicAuth = "Basic " + Base64.encode( credentials.getBytes() );
connection.setRequestProperty( "Authorization", basicAuth );
connection.setRequestMethod( "GET" );
//username/password above != user/pass below
String proxyAuth = Base64.encode( ("user:pass").getBytes() );
proxyAuth = "Basic " + proxyAuth;
connection.setRequestProperty( "Proxy-Connection", "Keep-Alive" );
connection.setRequestProperty( "Proxy-Authorization", proxyAuth );
connection.connect();
Everyting works fine on Windows, but its because of:
When Sun released the 1.4.2 version of the JDK, they slipped in support for native NTLM authentication on Windows.
But on configuration:
Red Hat Enterprise Linux Server release 6.8
java-1.8.0-openjdk-1.8.0.161-3.b14.el6_9.x86_64
or
java-1.8.0_162 oracle
Im getting this error:
java.lang.NullPointerException
at com.sun.security.ntlm.Client.type3(Client.java:161)
at sun.net.www.protocol.http.ntlm.NTLMAuthentication.buildType3Msg(NTLMAuthentication.java:250)
at sun.net.www.protocol.http.ntlm.NTLMAuthentication.setHeaders(NTLMAuthentication.java:225)
at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2114)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:162)
Found this bug described: JDK-8151788 where clearly says:
Resolved In Build: b128
Am I missing something very simple about that? Is there any way I can deal with that problem by HttpURLConnection or should I find something else? Any suggestions are appreciated!
@Edit
I added logging to getPasswordAuthentication() and it seems like its never inside.
It led me to this topic and actually NullPointerException is no longer there.
Now I'm getting:
java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 authenticationrequired"