如何在java开发中在Azure(版本1.1.2)上设置代理?

时间:2017-09-27 07:55:50

标签: java azure authentication proxy azure-web-sites

我使用关注代码,但始终有错误:

    public static void connect(CloudCredentials credentials, final CloudProxy proxy) throws Exception {
    ApplicationTokenCredentials azCredential = new ApplicationTokenCredentials(credentials.getClientID(), 
            credentials.getTenantID(), credentials.getClientKey(), AzureEnvironment.AZURE);
    try {
        Configurable config = Azure.configure()
                .withConnectionTimeout(30, TimeUnit.SECONDS)
                .withReadTimeout(60, TimeUnit.SECONDS);
        if (proxy != null) {
            config.withProxy(new Proxy(Type.HTTP, new InetSocketAddress(proxy.getProxyHost(), proxy
                    .getProxyPort())));
            if (proxy.getProxyUsername() != null && proxy.getProxyUsername().trim().length() > 0) {
                Authenticator.setDefault(new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(proxy.getProxyUsername(), proxy.getProxyPassword()
                                .toCharArray());
                    }
                });
                config.withProxyAuthenticator(new JavaNetAuthenticator());
            }
        }
        Authenticated authenticate = config.authenticate(azCredential);
        authenticate.withSubscription(credentials.getSubscription()).getCurrentSubscription().displayName();
        System.out.println(authenticate);
    } catch (Exception e) {
        System.out.println(e);
    }
}
[pool-1-thread-1] ERROR com.microsoft.aad.adal4j.AuthenticationContext - [Correlation ID: ] Request to acquire token failed.
java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"
    at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2124)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1316)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1291)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
    at com.microsoft.aad.adal4j.AdalOAuthRequest.configureHeaderAndExecuteOAuthCall(AdalOAuthRequest.java:140)
    at com.microsoft.aad.adal4j.AdalOAuthRequest.send(AdalOAuthRequest.java:83)
    at com.microsoft.aad.adal4j.AdalTokenRequest.executeOAuthRequestAndProcessResponse(AdalTokenRequest.java:80)
    at com.microsoft.aad.adal4j.AuthenticationContext.acquireTokenCommon(AuthenticationContext.java:818)
    at com.microsoft.aad.adal4j.AuthenticationContext.access$100(AuthenticationContext.java:66)
    at com.microsoft.aad.adal4j.AuthenticationContext$1.call(AuthenticationContext.java:174)
    at com.microsoft.aad.adal4j.AuthenticationContext$1.call(AuthenticationContext.java:163)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
java.lang.RuntimeException: java.io.IOException: java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"

但代理我相信没关系。我有另一个测试代码。它运作良好:

    String a = "111";
    System.out.print(a.toCharArray());

    Authenticator.setDefault(new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("test", "111".toCharArray());
        }
    });
    URLConnection connection;
    try {
        connection = new URL(
                "http://devio.us/~ricky/stackoverflow-protected/nothingtosee.html")
                .openConnection(new Proxy(Type.HTTP, new InetSocketAddress("192.168.1.59", 808)));

        System.out.println(new BufferedReader(new InputStreamReader(
                connection.getInputStream(), "UTF-8")).readLine());

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

我不想使用System.setProperty设置代理,因为所有java连接都将通过代理。 在Azure文档中,我找不到任何代理信息。 谁能帮我?非常感谢你。

1 个答案:

答案 0 :(得分:1)

  

现在,代理在设置隧道时需要基本身份验证   对于HTTPS,默认情况下将不再成功。如果需要,这个   可以通过从中删除Basic来重新激活身份验证方案   jdk.http.auth.tunneling.disabledSchemes网络属性,或者   将同名的系统属性设置为"" (空)上   命令行。

您可以从Disable Basic authentication for HTTPS tunneling找到它。

因此,您可以在net.properties中找到jdk/jre/lib directory个文件。 (对我来说,它是C:\Program Files\Java\jdk1.8.0_144\jre\lib)。

然后更改变量

jdk.http.auth.tunneling.disabledSchemes 
jdk.http.auth.proxying.disabledSchemes

清空如:

jdk.http.auth.tunneling.disabledSchemes=
jdk.http.auth.proxying.disabledSchemes=

请重试您的代码。如有任何疑虑,请随时让我知道。