【问题标题】:ADAL4j java proxy issueADAL4j java代理问题
【发布时间】:2021-11-13 00:47:25
【问题描述】:

我正在尝试使用 java 的 adal4j 库连接 azure。但我必须通过代理连接。以下是代码的 sn-p

String url = "https://login.microsoftonline.com/tenant_id/oauth2/authorize";
            authContext = new AuthenticationContext(url,false,
                                                    service);
   Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyhostname", 443));
   authContext.setProxy(proxy);
   ClientCredential clientCred = new ClientCredential(XXXX, xxxx);
   Future<AuthenticationResult>  future = authContext.acquireToken(                                                   
                                                                clientCred,
                                                                null);
   authResult = future.get();

我也试过

            System.setProperty("http.proxyPort", "80");
            System.setProperty("http.proxyUser", "xxxx");
            System.setProperty("http.proxyPassword", "xxxx");
            System.setProperty("http.proxyHost", "xxxxxxx");

而且我一直收到以下错误

the error is.....java.net.ConnectException: Connection timed out: connect
java.util.concurrent.ExecutionException: java.net.ConnectException: Connection timed out: connect
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:188)
    at com.toyota.eap.auth.Test.main(Test.java:76)
Caused by: java.net.ConnectException: Connection timed out: connect

注意:仅当我们在办公室内有代理时才会出现此错误。如果我在办公室外运行此程序,则没有问题。

对此有任何想法。

谢谢

【问题讨论】:

标签: java azure proxy adal


【解决方案1】:

在使用带有代理的 Adal4j 时,已有线程可以回答 java.net.ConnectException: Connection timed out 的问题。请查看ADAL for Java ProxyJava proxy issues - Connection Timed OutHow do I make HttpURLConnection use a proxy?

更多细节可以使用URLConnection类的函数setConnectTimeout(参考http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html)来解决,见下图和代码:

String url = "<url_link for http or https>";
int timeout = 30*1000; // 30 seconds
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("<proxy_host>", <proxy_port>));
// if need to auth for proxy
Authenticator authenticator = new Authenticator() {
    public PasswordAuthentication getPasswordAuthentication() {
        return (new PasswordAuthentication("<user>",
            "<password>".toCharArray()));
    }
};
Authenticator.setDefault(authenticator);
// open connection using proxy directly for this connection
// if not, setting in the JVM startup argus  or using System.setProperty for app global scope
HttpURLConnection conn = new URL(url).openConnection(proxy); // Also HttpsURLConnection
conn.setConnectTimeout(timeout);    // set Timeout

同时,根据我的经验,如果代理 IP 和端口位于承诺的网络环境中,您可以在本地环境中使用它,但在 Azure 上失败。从这个角度来看,我认为您应该首先确认代理是有效的 on-promise 和 Azure。

如果连接可以连接并且需要很长时间,那么将连接超时属性设置为我上面的参考是很有用的。

【讨论】:

    【解决方案2】:

    你的代理是 https 吗?如果是这样,请使用 jvm 参数 https。不是http:

    HTTPS -Dhttps.proxyHost=proxy-name-without-https.com -Dhttps.proxyPort=proxy-port

    例如。 -Dhttps.proxyHost=myproxy.com -Dhttps.proxyPort=2021

    【讨论】: