Explain Codes LogoExplain Codes Logo

How do I set the proxy to be used by the JVM

java
proxy-engineering
jvm-configuration
networking
Alex KataevbyAlex Kataev·Sep 29, 2024
TLDR

Employ JVM proxies via -Dhttp.proxyHost, -Dhttp.proxyPort, -Dhttps.proxyHost, and -Dhttps.proxyPort system properties.

Example:

java -Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=8080 -Dhttps.proxyHost=proxy.example.com -Dhttps.proxyPort=443 MyApp

You can also tweak these settings programmatically using System.setProperty method:

System.setProperty("http.proxyHost", "proxy.example.com"); System.setProperty("http.proxyPort", "8080"); System.setProperty("https.proxyHost", "proxy.example.com"); System.setProperty("https.proxyPort", "443");

Practical Guide to JVM Proxy Configuration

Managing Proxy Exceptions and System-Wide Settings

For excluding certain hosts from using a proxy, utilize the -Dhttp.nonProxyHosts JVM flag:

java -Dhttp.nonProxyHosts="localhost|192.168.*.*" MyApp // Keep it secret, keep it safe (from the proxy)!

In cases where multiple applications are relying on one configuration, enforce system-wide proxy settings. Set the java.net.useSystemProxies property true.

Tailoring Proxy Settings for Specific Servers

When working with servers such as JBoss or WebLogic, incorporate proxy configurations into the respective startup scripts.

export JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=3128" // Your application now has VIP access to internet!

Handling Proxy Authentication & Ensuring Secure Credentials

Set up proxy authentication using java.net.Authenticator and custom ProxyAuth classes. Prefer Base64 encoding for handling HTTP proxy user credentials, ensuring that your credentials are not compromised.

String secretSquirrel = new String(Base64.getEncoder().encode((username + ":" + password).getBytes())); // Da Vinci couldn't have encrypted it better!

Attach your specially encrypted credentials to your request headers for foolproof authentication.

Recognizing Different Proxy Types

Remember to differentiate between HTTP and SOCKS proxies - they each have their own unique system properties:

-DsocksProxyHost=socks.example.com -DsocksProxyPort=1080 // It's socks or nothing!

To disable proxy, set the corresponding host and port properties to blank values. Lastly, ensure that your application's networking configuration is compatible with these proxy settings to avoid connectivity problems.

Dive Deep Into JVM Proxy Configurations

Setting Proxy Options Programmatically

Apart from JVM flags, there is an option of programmatic configuration which offers dynamic proxy settings:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080)); URLConnection connection = new URL("http://example.com").openConnection(proxy); // Easy as it sounds!

Verifying Access to Internet Resources

Using clever programming, ensure that your application can reach internet resources behind the proxy by implementing connectivity checks and alert or fallback actions for failures.

Ensuring All Traffic Routed Through the Proxy

If your app requires all internet traffic to be routed through a proxy, confirm that all HTTP clients use your JVM's proxy settings.