使用 Java 进行身份验证的 HTTP 代理 [英] Authenticated HTTP proxy with Java
问题描述
如何配置用户名和密码以使用 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.net.Authenticator
也是必需的.我正在更新为了正确起见,我的回答相应地.)
( As pointed out by the OP, the using a java.net.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.net.Authenticator
设置代理的配置并设置系统属性http.proxyUser
和http.proxyPassword
.
For authentication, use java.net.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 代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!