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

发布于 2024-08-07 19:08:35 字数 575 浏览 2 评论 0原文

我正在寻找一个支持身份验证的 javaocks 代理客户端类,有什么建议吗? 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));

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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

德意的啸 2024-08-14 19:08:35

Java 通过注册的 java.net.Authenticator 或通过 网络属性

SOCKS协议支持设置

SOCKS 用户名和密码是
通过以下方式获取。第一的,
如果应用程序已注册
java.net.Authenticator 默认
实例,那么这将被查询
将协议设置为字符串
“SOCKS5”,并将提示设置为
字符串“SOCKS 身份验证”。如果
验证器不返回
用户名/密码,或者如果没有
验证器已注册,然后
系统检查用户偏好
java.net.socks.username”和
java.net.socks.password”。如果这些
偏好不存在,那么
检查系统属性“user.name
获取用户名。在这种情况下,没有
已提供密码。

socksProxyHost
socksProxyPort(默认:1080)
指示 SOCKS 代理服务器的名称和 SOCKS 协议层将使用的端口号。如果指定了socksProxyHost,则所有TCP 套接字都将使用SOCKS 代理服务器来建立连接或接受连接。 SOCKS 代理服务器可以是 SOCKS v4 或 v5 服务器,并且必须允许未经身份验证的连接。

有关客户端代码示例,您可以检查

编辑:根据评论更新答案

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

Authenticator 影响 JVM 中的所有身份验证(HTTP 身份验证、代理身份验证)。再说一次,这可能不适合你。

在您的情况下,可能的解决方案是使用 HttpClient 来自 < a href="http://hc.apache.org/" rel="nofollow noreferrer">HttpComponents (遗留 Commons HTTP 客户端 3.x)。查看 示例,尤其是 通过 a 请求代理代理身份验证示例。

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.

樱&纷飞 2024-08-14 19:08:35

使用此 HttpClient(不是 Apache 的),

http://www.innovation.ch/java/HTTPClient/< /a>

这只是一个 URL 处理程序,因此您可以使用常用的 HTTPUrlConnection。它支持SOCKS和其他代理。

Use this HttpClient (Not Apache's),

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文