繁体   English   中英

Java URLConnection NTLM代理身份验证 - linux

[英]Java URLConnection NTLM proxy authentication - linux

我正在尝试使用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的NTLM身份验证

当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处理这个问题,还是应该找到别的东西? 任何建议表示赞赏!

@编辑

我添加了日志记录到getPasswordAuthentication() ,它似乎永远不会在里面。 它引导我进入这个主题,实际上NullPointerException已经不存在了。 现在我得到:

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

构建b128是一个Java 9构建,不幸的是它似乎没有在Java 8上移植(我在任何Java 8错误修复列表中找不到任何NTLM错误,例如http://www.oracle.com/ technetwork / java / javase / 2col / 8u161-bugfixes-4021380.html )也许您可以尝试使用运行良好的旧openjdk的ntlm实现来运行您的应用程序。 我有一个与我但我不知道如何发送给你这个jar文件。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM