Configure HTTP/HTTPS Proxy Settings Java
For local networks inside an organisation it is common to access the public internet through a HTTP Proxy. This tutorial will show you how you can globally set the HTTP Proxy Settings in Java.
Use system Proxy Settings
If you have a proxy configured on your local system. You can try to set the java.net.useSystemProxies
(default is false) this property will try to apply the system properties.
-Djava.net.useSystemProxies=true
System.setProperty("java.net.useSystemProxies", "true");
Using Command Line JVM Settings
You can configure your server to use a HTTP Proxy server with JVM command line arguments. You can pass the following arguments either when you use a http or https proxy server.
// HTTP
-Dhttp.proxyHost=http://proxy.memorynotfound.com
-Dhttp.proxyPort=80
// HTTPS
-Dhttps.proxyHost=https://proxy.memorynotfound.com
-Dhttps.proxyPort=443
Passing arguments to Tomcat
Catalina.properties
Append following properties to the catalina.properties file in Tomcat: ${CATALINA_HOME}/conf/catalina.properties
.
http.proxyHost=http://proxy.memorynotfound.com
http.proxyPort=80
Catalina.bat
Append following properties to the startup file in Tomcat:
${CATALINA_HOME}/bin/catalina.bat
for windows${CATALINA_HOME}/bin/catalina.sh
for linux/mac
JAVA_OPTS="-Dhttp.proxyHost=http://proxy.memorynotfound.com -Dhttp.proxyPort=80"
Programatically set HTTP Proxy Settings
If you want you can also add proxy settings programmatically use the same properties but this time set them using System.setProperty();
.
// HTTP
System.setProperty("http.proxyHost", "http://proxy.memorynotfound.com");
System.setProperty("http.proxyPort", "80");
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1");
// HTTPS
System.setProperty("https.proxyHost", "https://proxy.memorynotfound.com");
System.setProperty("https.proxyPort", "443");
Authenticating Proxy
http.proxyUser
and http.proxyPassword
will not automatically authenticate via a proxy.Taking the above into account. Here is how you can Authenticate via a proxy. This initialisation code is typically executed at application startup. So make sure you register this authenticator before you make any HTTP Requests that require Proxy Authentication.
// settings proxy credentials
System.setProperty("http.proxyUser", "proxyUser");
System.setProperty("http.proxyPassword", "secret");
// Java ignores http.proxyUser. Here come's the workaround.
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", "80");
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;
}
});
The problem with this approach is it is not thread safe as you’re changing system properties.
System.setProperty(“java.net.useSystemProxies”, “true”); just doesn’t work as I would expect. It does not use the windows system proxy (from internet settings).
It must be said that we use a .pac and not a manually set proxy.
Guess Java just can’t.
I think the property values for http[s].proxyHost are incorrect, you should not include the scheme prefix, e.g.:
-Dhttp.proxyHost=proxy.memorynotfound.com
-Dhttps.proxyHost=proxy.memorynotfound.com