LINUX
2018.09.27 / 20:44

tomcat client forward proxy setting via squid proxy - ÅèĹ Ŭ¶óÀ̾ðÆ® ½ºÄûµå ÇÁ¶ô½Ã °æÀ¯ ȯ°æ¼³Á¤

hangawee
Ãßõ ¼ö 132

Configure HTTP/HTTPS Proxy Settings Java


https://memorynotfound.com/configure-http-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.

This property is introduced since JDK 1.5Note that this property is checked only once at startup.
-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

Setting 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;
    }
});

References