Authenticated HTTP proxy with Java

http, java, proxy

Solution

(EDIT: As pointed out by the OP, the using a `java.net.Authenticator` is required too. I'm updating my answer accordingly for the sake of correctness.)

(EDIT#2: As pointed out in another answer, in JDK 8 it's required to remove `basic` auth scheme from `jdk.http.auth.tunneling.disabledSchemes` property)

For authentication, use `java.net.Authenticator` to set proxy's configuration and set the system properties `http.proxyUser` and `http.proxyPassword`.

final String authUser = "user";
final String authPassword = "password";
Authenticator.setDefault(
  new Authenticator() {
    @Override
    public PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(authUser, authPassword.toCharArray());
    }
  }
);

System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);

System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

Problem

How can I configure the username and password to authenticate a http proxy server using Java? I just found the following configuration parameters: ``` http.proxyHost=<proxyAddress> http.proxyPort=<proxyPort> https.proxyHost=<proxyAddress> https.proxyPort=<proxyPort> ``` But, my proxy server requires authentication. How can I configure my app to use the proxy server?

Original source