Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authenticated HTTP proxy with Java

Tags:

java

http

proxy

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?

like image 429
Andre Pastore Avatar asked Oct 26 '09 18:10

Andre Pastore


People also ask

Does Java use Http_proxy?

Java provides proxy handlers for HTTP, HTTPS, FTP, and SOCKS protocols. A proxy can be defined for each handler as a hostname and port number: http. proxyHost – The hostname of the HTTP proxy server.


2 Answers

(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", ""); 
like image 81
Pascal Thivent Avatar answered Oct 04 '22 23:10

Pascal Thivent


You're almost there, you just have to append:

-Dhttp.proxyUser=someUserName -Dhttp.proxyPassword=somePassword 
like image 22
OscarRyz Avatar answered Oct 04 '22 23:10

OscarRyz