支持身份验证的 Java Proxy 客户端类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1540424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Proxy client class that supports authentication
提问by user121196
I'm looking for a java socks Proxy client class that supports authetication, any suggestions? The java.net.Proxy does not support authentication.
我正在寻找支持身份验证的 java socks 代理客户端类,有什么建议吗?java.net.Proxy 不支持身份验证。
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() 只允许一组凭证。
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));
采纳答案by Pascal Thivent
Java supports Socks proxy configuration via a registered java.net.Authenticatoror via preferences as documented in the Networking Properties:
Java 通过注册java.net.Authenticator或通过网络属性中记录的首选项支持 Socks 代理配置:
SOCKS protocol support settings
The SOCKS username and password are acquired in the following way. First, if the application has registered a
java.net.Authenticatordefault 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.
SOCKS 协议支持设置
SOCKS 用户名和密码的获取方式如下。首先,如果应用程序注册了一个
java.net.Authenticator默认实例,那么将使用设置为字符串“SOCKS5”的协议进行查询,并将提示设置为字符串“SOCKS authentication”。如果验证器没有返回用户名/密码或者如果没有注册验证器,则系统会检查用户首选项“java.net.socks.username”和“java.net.socks.password”。如果这些首选项不存在,则user.name检查系统属性“ ”的用户名。在这种情况下,不提供密码。socksProxyHost
socksProxyPort(default: 1080)
表示SOCKS代理服务器的名称和SOCKS协议层将使用的端口号。如果指定了 socksProxyHost,则所有 TCP 套接字都将使用 SOCKS 代理服务器来建立连接或接受连接。SOCKS 代理服务器可以是 SOCKS v4 或 v5 服务器,它必须允许未经身份验证的连接。
For client code examples, you can check this answeron Stack Overflow.
对于客户端代码示例,您可以在 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.
JDK 中 SOCKS 支持的副作用是您的整个 JVM 将通过相同的 SOCKS 代理。所以这可能对你不起作用。
The Authenticatoraffects all authentication in your JVM (HTTP auth, Proxy Auth). So again, this may not work for you.
这Authenticator会影响 JVM 中的所有身份验证(HTTP 身份验证、代理身份验证)。再说一次,这可能对您不起作用。
In your case, a possible solution would be to use HttpClientfrom HttpComponents(the successor of the legacy Commons HTTP Client 3.x). Check out the samplesand especially the Request via a proxyand Proxy authenticationexamples.
在你的情况下,一个可能的解决方案是使用的HttpClient从HttpComponents(传统的继承者共享HTTP客户端3.X)。查看示例,尤其是通过代理请求和代理身份验证示例。
回答by ZZ Coder
Use this HttpClient (Not Apache's),
使用这个 HttpClient(不是 Apache 的),
http://www.innovation.ch/java/HTTPClient/
http://www.innovation.ch/java/HTTPClient/
This is simply an URL handler so you can use the usual HTTPUrlConnection. It supports SOCKS and other proxy.
这只是一个 URL 处理程序,因此您可以使用通常的 HTTPUrlConnection。它支持SOCKS和其他代理。

