加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

支持身份验证的Java代理客户端类

发布时间:2020-12-15 01:11:06 所属栏目:Java 来源:网络整理
导读:我正在寻找支持认证的java socks Proxy客户端类,有什么建议吗? java.net.Proxy不支持身份验证. 编辑: 我似乎找不到通过套接字将身份验证数据附加到特定代理主机的方法. Authenticator.setDefault()仅允许一组凭据. Authenticator.setDefault(new Authentic

我正在寻找支持认证的java socks Proxy客户端类,有什么建议吗? java.net.Proxy不支持身份验证.

编辑:
我似乎找不到通过套接字将身份验证数据附加到特定代理主机的方法. 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));
最佳答案
Java通过已注册的java.net.Authenticator或通过Networking Properties中记录的首选项支持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.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.

对于客户端代码示例,您可以在Stack Overflow上查看this answer.

编辑:根据评论更新答案

JDK中SOCKS支持的副作用是您的整个JVM将通过相同的SOCKS代理.所以这可能不适合你.

Authenticator会影响JVM中的所有身份验证(HTTP身份验证,代理身份验证).所以,这可能不适合你.

在您的情况下,可能的解决方案是使用HttpComponents HttpClient(传统Commons HTTP Client 3.x的后继者).查看samples,尤其是Request via a proxy和Proxy authentication示例.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读