使用 Java 进行身份验证的 HTTP 代理

编程入门 行业动态 更新时间:2024-10-22 19:33:00
本文介绍了使用 Java 进行身份验证的 HTTP 代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何配置用户名和密码以使用 Java 对 http 代理服务器进行身份验证?

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?

推荐答案

(正如 OP 所指出的,使用 java.Authenticator 也是必需的.我正在更新为了正确起见,我的回答相应地.)

( As pointed out by the OP, the using a java.Authenticator is required too. I'm updating my answer accordingly for the sake of correctness.)

(编辑#2:正如另一个答案中所指出的,在 JDK 8 中需要删除 basic 来自 jdk.http.auth.tunneling.disabledSchemes 属性的身份验证方案)

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

对于认证,使用java.Authenticator设置代理的配置并设置系统属性http.proxyUser和http.proxyPassword.

For authentication, use java.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", "");

更多推荐

使用 Java 进行身份验证的 HTTP 代理

本文发布于:2023-11-03 16:20:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   Java   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!