首页 文章

Java URLConnection NTLM代理身份验证 - linux

提问于
浏览
3

我正在尝试使用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在Windows上工作正常,但它的原因是:

JDK-Based NTLM Authentication

当Sun发布JDK的1.4.2版本时,他们在Windows上支持本机NTLM身份验证 .

但在配置上:

红帽企业Linux服务器版本6.8 java-1.8.0-openjdk-1.8.0.161-3.b14.el6_9.x86_64

要么

java-1.8.0_162 oracle

我得到这个错误:

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)

发现这个描述的错误:JDK-8151788清楚地说:

已解决构建:b128

我错过了一些非常简单的事情吗?我有什么方法可以通过HttpURLConnection处理这个问题,还是应该找到别的东西?任何建议表示赞赏!

@Edit

我添加了日志到 getPasswordAuthentication() ,它似乎永远不会在里面 . 它引导我到this主题,实际上NullPointerException不再存在 . 现在我得到:

java.io.IOException:无法通过代理进行隧道传输 . 代理返回“HTTP / 1.1 407 authenticationrequired”

2 回答

  • 0

    构建b128是一个Java 9构建,不幸的是它似乎没有在Java 8上移植(我在任何Java 8错误修复列表中找不到任何NTLM错误,例如http://www.oracle.com/technetwork/java/javase/2col/8u161-bugfixes-4021380.html)也许你可以尝试运行你的应用程序一个旧的openjdk 's ntlm implementation that is working well. I have one with me but I'我不知道如何发送这个jar文件 .

  • 1

    我找到了解决问题的方法 .

    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() );
    

    我觉得这个解决方案没有任何好处,但这对我来说已经足够了 .

相关问题