[英]Java URLConnection NTLM proxy authentication - linux
I'm trying to connect to an url by proxy with NTLM authentication. 我正在尝试使用NTLM身份验证通过代理连接到URL。
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: Everyting在Windows上工作正常,但它的原因是:
JDK-Based NTLM Authentication 基于JDK的NTLM身份验证
When Sun released the 1.4.2 version of the JDK, they slipped in support for native NTLM authentication on Windows.
当Sun发布JDK的1.4.2版本时,他们在Windows上支持本机NTLM身份验证。
But on configuration: 但在配置上:
Red Hat Enterprise Linux Server release 6.8
红帽企业Linux服务器版本6.8
java-1.8.0-openjdk-1.8.0.161-3.b14.el6_9.x86_64
Java的1.8.0-的OpenJDK,1.8.0.161-3.b14.el6_9.x86_64
or 要么
java-1.8.0_162 oracle
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: 发现这个错误描述: JDK-8151788清楚地说:
Resolved In Build: b128
已解决建立: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?
我有什么方法可以通过HttpURLConnection处理这个问题,还是应该找到别的东西? Any suggestions are appreciated!
任何建议表示赞赏!
@Edit @编辑
I added logging to getPasswordAuthentication()
and it seems like its never inside. 我添加了日志记录到
getPasswordAuthentication()
,它似乎永远不会在里面。 It led me to this topic and actually NullPointerException is no longer there. 它引导我进入这个主题,实际上NullPointerException已经不存在了。 Now I'm getting:
现在我得到:
java.io.IOException: Unable to tunnel through proxy.
java.io.IOException:无法通过代理进行隧道传输。 Proxy returns "HTTP/1.1 407 authenticationrequired"
代理返回“HTTP / 1.1 407 authenticationrequired”
The build b128 is a Java 9 build, and unluckily it seems it was not ported on Java 8 (I can't find any NTLM bug in any of the Java 8 bug fixes list such as http://www.oracle.com/technetwork/java/javase/2col/8u161-bugfixes-4021380.html ) Maybe could you try running your app with an old openjdk's ntlm implementation that is working well. 构建b128是一个Java 9构建,不幸的是它似乎没有在Java 8上移植(我在任何Java 8错误修复列表中找不到任何NTLM错误,例如http://www.oracle.com/ technetwork / java / javase / 2col / 8u161-bugfixes-4021380.html )也许您可以尝试使用运行良好的旧openjdk的ntlm实现来运行您的应用程序。 I have one with me but I'm not sure how to send you this jar file.
我有一个与我但我不知道如何发送给你这个jar文件。
I found little workaround for my problem. 我找到了解决问题的方法。
StringBuilder commandBuilder = new StringBuilder();
commandBuilder.append( "curl -s " )
.append( "--proxy $PXHOST:$PXPORT " )
.append( "--proxy-user $PXUSER:$PXPASS " )
.append( "--user $SNUSER:$SNPASS " )
.append( "--url \"" ).append( requestURL ).append( "\" " )
.append( "--request GET " )
.append( "--header \"Accept:application/json\" " )
.append( "--header \"Content-Type:application/json\"" );
ProcessBuilder processBuilder = new ProcessBuilder( "/bin/bash",
"-c", commandBuilder.toString() );
I don't find this solution any good, but its enough for me at this moment. 我觉得这个解决方案没有任何好处,但这对我来说已经足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.