需要http 407代理身份验证:如何在java代码中处理

day*_*v89 13 java proxy proxy-server

System.setProperty("http.proxySet", "true");
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty("http.proxyHost", "192.168.1.103");
System.setProperty("http.proxyPort", "3128");
System.setProperty("http.proxyUser", "user123");
System.setProperty("http.proxyPassword", "passwD123");

url = new URL("http://www.google.co.in");
Run Code Online (Sandbox Code Playgroud)

每当我使用此代码时,IOException抛出HTTP响应代码407.HTTP 407表示需要代理身份验证.为什么在设置proxyUser和proxyPassword时会出现此问题. 在此输入图像描述
如果我输错密码会出现http 401,但它总是给我407,意味着我的代码不带用户名和密码.在上面的代码中,user123是用户名,passwD123是用于代理验证的密码.

day*_*v89 20

http://blog.vinodsingh.com/2008/05/proxy-authentication-in-java.html

我找到了解决方案,感谢Vinod Singh先生.

Java中的代理身份验证

通常的企业网络通过代理服务器提供互联网访问,有时它们也需要身份验证.应用程序可以打开与企业内部网外部服务器的连接.因此,必须以编程方式进行代理身份验证.幸运的是,Java提供了一种透明的机制来进行代理身份验证.

创建一个简单的类,如下所示 -

import java.net.Authenticator;

class ProxyAuthenticator extends Authenticator {

    private String user, password;

    public ProxyAuthenticator(String user, String password) {
        this.user = user;
        this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password.toCharArray());
    }
}
Run Code Online (Sandbox Code Playgroud)

并在代码打开URLConnection之前放置这些代码行 -

Authenticator.setDefault(new ProxyAuthenticator("user", "password"));
System.setProperty("http.proxyHost", "proxy host");
System.setProperty("http.proxyPort", "port");
Run Code Online (Sandbox Code Playgroud)

现在所有呼叫都将成功通过代理身份验证.


The*_*niK 7

@GauravDS你提到过:

http://blog.vinodsingh.com/2008/05/proxy-authentication-in-java.html 我找到了解决方案,感谢Vinod Singh先生.Java中的代理身份验证通常的企业网络通过代理服务器提供Internet访问,有时也需要身份验证.应用程序可以打开与企业内部网外部服务器的连接.因此,必须以编程方式进行代理身份验证.幸运的是,Java提供了一种透明的机制来进行代理身份验证.创建一个简单的类,如下所示.
.
.
并在代码打开URLConnection之前放置这些代码行 - Authenticator.setDefault(new ProxyAuthenticator("user", "password")); System.setProperty("http.proxyHost", "proxy host"); System.setProperty("http.proxyPort", "port"); 现在所有调用都将成功通过代理身份验证.

如果您要连接的网站也需要用户名/密码,那该怎么办?当外部站点查找经过身份验证的用户时,设置默认身份验证器(Authenticator.setDefault)将失败.

有什么意见吗?....有人吗?

编辑:1之前 使用此代码并收到错误(407)需要代理身份验证.我认为那是因为身份验证是由不同的主机请求的.当您为一个主机设置一个用户/通行证的默认验证器时,其他请求主机的验证将失败.我昨天对SimpleAuthenticator类进行了以下更改,现在它就像一个魅力.

   protected PasswordAuthentication getPasswordAuthentication()
   {
    String requestingHost = getRequestingHost();
    if (requestingHost == proxyHost){
        System.out.println("getPasswordAuthentication() request recieved from->" + requestingHost );
        return new PasswordAuthentication(proxyuser,proxypass.toCharArray());
    }
    else{
        System.out.println("getPasswordAuthentication() request recieved from->" + requestingHost );
        return new PasswordAuthentication(sharepointusername,sharepointpassword.toCharArray());
    }

   }
Run Code Online (Sandbox Code Playgroud)

更多信息:http://blog.ashwani.co.in/blog/2013-07-29/access-sharepoint-webservices-from-java-behind-proxy/


Dan*_*ell 5

Authenticator对于一般情况,使用an的答案是正确的。但是,Java 8u111和更高版本中HTTP 407的另一个原因是,如果您对代理使用BASIC身份验证。

在这种情况下,请添加以下系统属性:

-Djdk.http.auth.tunneling.disabledSchemes=
Run Code Online (Sandbox Code Playgroud)

我从以下网址发现了这一点:https : //confluence.atlassian.com/kb/basic-authentication-fails-for-outgoing-proxy-in-java-8u111-909643110.html