我们在JBoss上运行了一个应用程序。在许多安装中,服务器在防火墙后运行,拒绝通过代理访问互联网。 现在我的任务是找出在需要身份验证时如何使用此代理。
配置JBoss使用代理对-Dhttp.proxyHost=proxy_host -Dhttp.proxyPort=proxy_port
没有问题,但我认为无法指出用户名和密码。
在非EJB应用程序上,我使用Authenticator.setDefault(new ProxyAuthenticator("test", "test"))
成功,其中ProxyAuthenticator正在扩展Authenticator。但是,这对JBoss无效。
我遇到的一个子问题是服务器和非EJB应用程序需要访问本地资源而不使用代理。
答案 0 :(得分:1)
答案 1 :(得分:1)
最后,我得到了这个工作。有了Richs帖子中的两个链接以及一些反复试验,它现在可以正常运行。 目前我只实现了基本身份验证,将来我将不得不添加其他身份验证类型。
一个很大的障碍是我开始使用-Dhttp.proxyHost and -Dhttp.proxyPort
配置JVM。这在某种程度上混淆了JVM而不是它的帮助。使用该配置,ProxyAuthenticator.getPasswordAuthentication()
从未被调用过。因此,还必须设置默认的ProxySelector。
代码通过代理引导一切 - 也调用本地地址。很快我就需要解决这个问题:-)(任何想法?)
这就是我设置它的方法:
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);
}
}
}
ProxyAuthenticator:
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;
}
}