简体   繁体   English

J2SE代理验证

[英]J2SE Proxy Authentication

We use 2 SIMILAR Microsoft ISA Proxy Server 2003 to connect to internet. 我们使用2 SIMILAR Microsoft ISA Proxy Server 2003连接到Internet。 Each Proxy has different Login style, as below : 每个代理具有不同的登录样式,如下所示:

Server-1 : nt-domain\\alan Server-2 : alan@love-u.com Server-1:nt-domain \\ alan Server-2:alan@love-u.com

Logon in IE, Firefox and my Phonecell via Wifi all are fine. 通过Wifi登录IE,Firefox和我的Phonecell都很好。 But, a problem appears when we run a java application J2SE Ver 4, 5 and 6, where it needs internet authentication. 但是,当我们运行java应用程序J2SE Ver 4,5和6时,会出现一个问题,它需要进行Internet身份验证。 Logon to Server-2 is OK, but FAIL for Server-2 (style : alan@domain.com). 登录到Server-2是正常的,但是对于Server-2,则为FAIL(样式:alan@domain.com)。

Note : Both proxy seen using Windows authentication, type : negotiate NTLM 注意:使用Windows身份验证看到的两个代理都键入:negotiate NTLM

Appreceate if you can help or for any suggest. 如果您可以提供帮助或任何建议,请接受。

Thank you, 谢谢,

Alan L 艾伦L.

The java documentation describes how to enable NTLM authentication in java. java文档描述了如何在java中启用NTLM身份验证。 If you have access to source, you can programmatically add system properties as described in "proxy" article, adding something as follows (see also discussion of axis2): 如果您有权访问源,则可以按照“代理”文章中的描述以编程方式添加系统属性,添加如下内容(另请参见axis2的讨论 ):

System.setProperty("http.auth.ntlm.domain", mydomain);

If you have no access to source, you can set properties on the command line that launches your java binary, adding something like: 如果您无权访问源,则可以在启动Java二进制文件的命令行上设置属性,添加如下内容:

java -DproxyHost=host  -DproxyPort=8080  -Dhttp.auth.ntlm.domain=mydomain  ...

There are some good libraries that can help you to get over this issue. 有一些很好的库可以帮助您克服这个问题。 Proxy Vole (Network proxy auto detection for Java) can will help you to use the same proxy settings as Internet Explorer to authenticate via the proxy. Proxy Vole (Java的网络代理自动检测)可以帮助您使用与Internet Explorer相同的代理设置通过代理进行身份验证。

To provide network connectivity out of the box for you Java application you can use the Proxy - Vole library. 要为Java应用程序提供开箱即用的网络连接,您可以使用Proxy-Vole库。 It provides some strategies for autodetecting the current proxy settings. 它提供了一些自动检测当前代理设置的策略。 There are many configureable strategies to choose from. 有许多可配置的策略可供选择。 At the moment Proxy - Vole supports the following proxy detection strategies. 目前,Proxy-Vole支持以下代理检测策略。

  • Read platform settings (Supports: Windows, KDE, Gnome) 读取平台设置(支持:Windows,KDE,Gnome)
  • Read browser setting (Supports: Firefox 3.x, Internet Explorer) 读取浏览器设置(支持:Firefox 3.x,Internet Explorer)
  • Read environment variables (often used variables on Linux / Unix server systems) 读取环境变量(Linux / Unix服务器系统上经常使用的变量)
  • Auto detection script by using WPAD/PAC (Only some features supported) 使用WPAD / PAC自动检测脚本(仅支持某些功能)

Are you running an application or an applet? 你在运行应用程序或applet吗? An applet can piggy back on the browser's proxy authentication mechanism. applet可以依赖浏览器的代理身份验证机制。

I would suggest using cURL to connect through your proxy first, and view the handshake which occurs. 我建议先使用cURL连接代理,然后查看发生的握手。 The proxy server will offer the client a list of authentication methods. 代理服务器将为客户端提供身份验证方法列表。

If the user and password are fixed, you can sometimes just lift the header out from your browser session, and use that in your code. 如果用户和密码已修复,您有时可以从浏览器会话中取出标头,并在代码中使用它。 For example, I can add this header to a request, and the proxy will see me as authenticated: 例如,我可以将此标头添加到请求中,代理会将我视为已通过身份验证:

Proxy-Authorization: Basic AbCdEfGhOjk==

Using apache-commons httpClient (version 3), I have the following code. 使用apache-commons httpClient(版本3),我有以下代码。 It isn't tested well (if it is at all), but I think it worked once.. :) This is in case you can modify the programs.. if they are some 3rd party packages, nothing you can do. 它没有经过良好的测试(如果它完全没有),但我认为它曾经工作过一次.. :)这是为了你可以修改程序..如果它们是一些第三方包,你无能为力。


String proxyHost = System.getProperty("https.proxyHost");
                int proxyPort = 0;
                try {
                    proxyPort = Integer.parseInt(System
                            .getProperty("https.proxyPort"));
                } catch (Exception ex) {
                    //
                }

            System.setProperty("java.net.useSystemProxies", "true");

            ProxySelector ps = ProxySelector.getDefault();
            List<Proxy> proxyList = ps.select(new URI(targetUrl));
            Proxy proxy = proxyList.get(0);
            if (proxy != null) {
                InetSocketAddress addr = ((InetSocketAddress) proxy
                        .address());
                if (addr != null) {
                    proxyHost = addr.getHostName();
                    proxyPort = addr.getPort();
                }
            }

            boolean useProxy = proxyHost != null && proxyHost.length() > 0;

            if (useProxy) {
                httpClient.getHostConfiguration().setProxy(proxyHost,
                        proxyPort);
            }

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

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