I'm looking for a java socks Proxy client class that supports authetication, any suggestions? The java.net.Proxy does not support authentication.
Edit: I can't seem to find a way that would attach authentication data to particular proxy host via socket. Authenticator.setDefault() allows only one set of credential.
Authenticator.setDefault(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
PasswordAuthentication p=new PasswordAuthentication("xxx", "xxx".toCharArray());
return p;
}
});
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("xxx.xx.xxx.xxx", xxx));
Socket sock = new Socket(proxy);
sock.connect(new InetSocketAddress(server,xx));
Java supports Socks proxy configuration via a registered java.net.Authenticator
or via preferences as documented in the Networking Properties:
SOCKS protocol support settings
The SOCKS username and password are acquired in the following way. First, if the application has registered a
java.net.Authenticator
default instance, then this will be queried with the protocol set to the string "SOCKS5", and the prompt set to to the string "SOCKS authentication". If the authenticator does not return a username/password or if no authenticator is registered then the system checks for the user preferences "java.net.socks.username
" and "java.net.socks.password
". If these preferences do not exist, then the system property "user.name
" is checked for a username. In this case, no password is supplied.socksProxyHost
socksProxyPort (default: 1080)
Indicates the name of the SOCKS proxy server and the port number that will be used by the SOCKS protocol layer. If socksProxyHost is specified then all TCP sockets will use the SOCKS proxy server to establish a connection or accept one. The SOCKS proxy server can either be a SOCKS v4 or v5 server and it has to allow for unauthenticated connections.
For client code examples, you can check this answer on Stack Overflow.
EDIT: Update of the answer as per comment
The side effect of the SOCKS support in JDK is that your whole JVM will go through the same SOCKS proxy. So this may not work for you.
The Authenticator
affects all authentication in your JVM (HTTP auth, Proxy Auth). So again, this may not work for you.
In your case, a possible solution would be to use HttpClient from HttpComponents (the successor of the legacy Commons HTTP Client 3.x). Check out the samples and especially the Request via a proxy and Proxy authentication examples.