【问题标题】:Play java application accessing a Web Service through an Authenticating Proxy播放通过身份验证代理访问 Web 服务的 java 应用程序
【发布时间】:2015-05-26 06:56:26
【问题描述】:

我正在开发一个 Play Java 应用程序,它需要使用 Play WS API (JavaWS) 通过身份验证代理(即代理需要用户名/密码)连接到另一个 REST 服务。

首先,我在启动 Play 应用程序时尝试使用下面给出的 JVM 选项。

-Dhttp.proxyHost=<proxy_server_hostname> -Dhttp.proxyPort=<proxy_server_port> -Dhttp.proxyUser=<username> -Dhttp.proxyPassword=<password>

以上没有完全发挥作用。应用程序能够毫无问题地连接到代理服务器,但代理服务器返回 PROXY_AUTH_REQUIRED 错误,这表明 -Dhttp.proxyUser 和 -Dhttp.proxyPassword JVM 选项不起作用。

我搜索并找到了以下两个链接,它们展示了如何在典型的 Java 应用程序中执行此操作。

http://memorynotfound.com/configure-http-proxy-settings-java/ http://rolandtapken.de/blog/2012-04/java-process-httpproxyuser-and-httpproxypassword

按照这两个链接的建议,我在 Global.java 中修改了 Play 应用程序的 onStart 方法,如下所示,

@Override
public void onStart(Application application) {
    //Proxy authentication begin
    System.setProperty("http.proxyHost", "<proxy_server_hostname>");
    System.setProperty("http.proxyPort", "<proxy_server_port>");
    System.setProperty("http.proxyUser", "<username>");
    System.setProperty("http.proxyPassword", "<password>");

    Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            if (getRequestorType() == RequestorType.PROXY) {
                String prot = getRequestingProtocol().toLowerCase();
                String host = System.getProperty(prot + ".proxyHost", "");
                String port = System.getProperty(prot + ".proxyPort", "");
                String user = System.getProperty(prot + ".proxyUser", "");
                String password = System.getProperty(prot + ".proxyPassword", "");

                if (getRequestingHost().equalsIgnoreCase(host)) {
                    if (Integer.parseInt(port) == getRequestingPort()) {
                        return new PasswordAuthentication(user, password.toCharArray());
                    }
                }
            }
            return null;
        }
    });
    //Proxy authentication end
}

通过上述修改,我启动了 Play 应用程序 * 没有指定前面提到的 JVM 选项(因为我现在在代码中给出了相同的选项)。但结果还是一样。代理服务器仍然向应用程序返回 PROXY_AUTH_REQUIRED 错误消息。应用程序再次通过上述代码修改连接到代理服务器,但 Java Authenticator 似乎没有将代理用户名和密码提交给代理服务器。

或者在 Play Java 应用程序中是否有其他方法可以做到这一点?

谢谢

【问题讨论】:

    标签: java playframework playframework-2.3


    【解决方案1】:

    网络身份验证:类身份验证器

    -   The class Authenticator represents an object that knows how to obtain authentication for a network connection. Usually, it will do this by prompting
        the user for information.
    - Can be used when credential need to be sent over network.
    

    Authenticator

    【讨论】:

    • 在您分享的链接中,您似乎还没有阅读完整的描述.. :|在我的第二种技术(即以编程方式连接代理)中,我已经完成了您共享的 Javadoc 链接的其余部分中提到的内容。这是我们应该以编程方式连接到身份验证代理的方式,但它在 Play Java 中不起作用。
    • 谢谢,我阅读并使用了 SOAP WS,即使在代理中它也非常适合我。就我而言,在设置系统属性时发现了一些问题,但已成功解决。
    • 您是否尝试过 Play Java 应用程序?
    • 不幸的是,这个答案对我的问题没有用。因此,我反对投票。
    • 这是您的电话,并检查过属性名称一次 bcz 与使用 SOAP lib 时遇到的相同。
    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2017-04-28
    相关资源
    最近更新 更多