Tuesday, August 4, 2009

Configure Tomcat to work behind a Proxy

Sometimes it's necessary to a Tomcat application to work behind a Proxy server. If this is the case, to get it work it's necessary to tell the virtual machine to "route" all the communications through this proxy server, in order to do that just use the following parameters when the virtual machine is started:

-Dhttp.proxyHost=proxyname -Dhttp.proxyPort=portnumber

And to manage exceptions to proxy connection use:

-Dhttp.noProxyHosts="localhost|127.0.0.1

And finally if the proxy server requires authentication, before any call that use the connection through the proxy call this in your Java code:

Authenticator.setDefault(new SimpleAuthenticator(username,password));

where SimpleAuthenticator is:

public class SimpleAuthenticator extends Authenticator {
private String username, password;
public SimpleAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
}

No comments:

Post a Comment