从JBoss进行代理访问

从JBoss进行代理访问,jboss,proxy,Jboss,Proxy,我们有一个应用程序在JBoss上运行。在许多安装中,服务器运行在防火墙后面,防火墙拒绝服务器访问internet,除非通过代理。 现在,我的任务是找出在需要身份验证时如何使用此代理 将JBoss配置为使用代理是没有问题的,但是我看不到任何方法来指示用户名和密码 在非EJB应用程序上,我成功地使用了Authenticator.setDefault(新的ProxyAuthenticator(“test”、“test”))其中ProxyAuthenticator扩展了Authenticator。然而,

我们有一个应用程序在JBoss上运行。在许多安装中,服务器运行在防火墙后面,防火墙拒绝服务器访问internet,除非通过代理。 现在,我的任务是找出在需要身份验证时如何使用此代理

将JBoss配置为使用代理是没有问题的,但是我看不到任何方法来指示用户名和密码

在非EJB应用程序上,我成功地使用了
Authenticator.setDefault(新的ProxyAuthenticator(“test”、“test”))
其中ProxyAuthenticator扩展了Authenticator。然而,这在JBoss上不起作用


我在这种情况下遇到的一个子问题是,服务器和非EJB应用程序需要在不使用代理的情况下访问本地资源。

您可以通过以下属性阻止应用程序对某些主机使用代理:

-Dhttp.nonProxyHosts="*.foo.com|localhost"

至于针对代理进行身份验证,您可能会发现它非常有用。

最后,我实现了这一点。通过Richs post中的两个链接和一些尝试和错误,它现在可以按要求工作。 目前我只实现了基本的身份验证,将来还需要添加其他身份验证类型

一个很大的障碍是我开始用
-Dhttp.proxyHost和-Dhttp.proxyPort
配置JVM。这在某种程度上让JVM感到困惑,而不是帮助。通过该配置,从未调用过
ProxyAuthenticator.getPasswordAuthentication()
。因此,还需要设置默认代理选择器

该代码通过代理引导所有内容,也可以调用本地地址。很快我就需要解决这个问题:-)(有什么想法吗?)

这是我设置它所做的:

ProxySelector proxySelector;
if (proxySelector == null) {
    proxySelector = new MyProxySelector(ProxySelector.getDefault(), address, port);
}

ProxySelector.setDefault(proxySelector);
Authenticator.setDefault(ProxyAuthenticator.getInstance());
MyProxySelector:

import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyProxySelector extends ProxySelector {
    /**
     * Keep a reference on the default ProxySelector
     */
    private ProxySelector defaultProxySelector = null;
    private static ProxySelector proxySelector;

    /*
     * Inner class representing a Proxy and a few extra data
     */
    private class InnerProxy {
        Proxy proxy;
        SocketAddress addr;
         // How many times did we fail to reach this proxy?
        int failedCount = 0;

        InnerProxy(InetSocketAddress a) {
            addr = a;
            proxy = new Proxy(Proxy.Type.HTTP, a);
        }

        SocketAddress address() {
            return addr;
        }

        Proxy toProxy() {
            return proxy;
        }

        int failed() {
            return ++failedCount;
        }
    }

    /* A list of proxies, indexed by their address. */
    private HashMap<SocketAddress, InnerProxy> proxies = new HashMap<SocketAddress, InnerProxy>();

    public MyProxySelector(ProxySelector def, String address, Integer port) {
        // Save the previous default
        defaultProxySelector = def;

        // Populate the HashMap (List of proxies)
        InnerProxy i;
        if (address != null && port != null) {
            i = new InnerProxy(new InetSocketAddress(address, port));
            proxies.put(i.address(), i);
        }
    }

    /**
     * This is the method that the handlers will call.
     *
     * @param uri
     * @return a List of proxies.
     */
    public List<Proxy> select(URI uri) {
        if (uri == null) {
            throw new IllegalArgumentException("URI can't be null.");
        }

        // If it's a http (or https) URL, then we use our own
        // list.
        String protocol = uri.getScheme();
        if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {
            List<Proxy> proxyList = new ArrayList<Proxy>();
            for (InnerProxy p : proxies.values()) {
                proxyList.add(p.toProxy());
            }

            if (proxyList.size() == 0) {
                proxyList.add(Proxy.NO_PROXY);
            }
            return proxyList;
        }

         // Not HTTP or HTTPS (could be SOCKS or FTP)
         // defer to the default selector.
        if (defaultProxySelector != null) {
            return defaultProxySelector.select(uri);
        } else {
            List<Proxy> proxyList = new ArrayList<Proxy>();
            proxyList.add(Proxy.NO_PROXY);
            return proxyList;
        }
    }

    /**
     * Method called by the handlers when it failed to connect
     * to one of the proxies returned by select().
     *
     * @param uri
     * @param sa
     * @param ioe
     */
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        // Let's stick to the specs again.
        if (uri == null || sa == null || ioe == null) {
            throw new IllegalArgumentException("Arguments can't be null.");
        }

        // Let's lookup for the proxy
        InnerProxy p = proxies.get(sa);
        if (p != null) {
            // It's one of ours, if it failed more than 3 times
            // let's remove it from the list.
            if (p.failed() >= 3)
                proxies.remove(sa);
        } else {
            // Not one of ours, let's delegate to the default.
            if (defaultProxySelector != null)
                defaultProxySelector.connectFailed(uri, sa, ioe);
        }
    }
}

第一个链接假定您正在运行JBoss 5.1.0 GA和JBossWS 3.3.1。我们正在运行4.3.0 GA:-(第二个链接只使用Authenticator.setDefault,我已经尝试过了。你能解释一下这在什么意义上不起作用吗?我能想到的唯一其他解决方案是在关闭的代理上有一个打开的代理,并限制某些应用通过某种网络策略访问另一个代理。。。
import org.bouncycastle.crypto.RuntimeCryptoException;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

public class ProxyAuthenticator extends Authenticator {

    private String user;
    private String password;
    private static ProxyAuthenticator authenticator;

    public ProxyAuthenticator(String user, String password) {
        this.user = user;
        this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password.toCharArray());
    }

    public static Authenticator getInstance(String user, String password) {
        if (authenticator == null) {
            authenticator = new ProxyAuthenticator(user, password);
        }
        return authenticator;
    }
}