0

I am trying to connect azure using adal4j library for java.But i have to connect through the proxy.Following is the snippet of code

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

Also i have tried with

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

And all the time i am getting this following error

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

Note: This error is only if we have proxy within the office. From outside the office If I ran thisprogramme there is no issue.

Any Thought on this.

Thanks

user2052854
  • 89
  • 10
  • For access AAD thru Proxy in Java, you can review the thread http://stackoverflow.com/questions/32522773/adal-for-java-proxy/32558897. Meanwhile, please check the office networking with Proxy and be sure that it is available and valid. – Peter Pan Nov 09 '15 at 05:53

2 Answers2

2

There were the existed threads to answer the issue for java.net.ConnectException: Connection timed out in using Adal4j with Proxy. Please review ADAL for Java Proxy and Java proxy issues - Connection Timed Out and How do I make HttpURLConnection use a proxy?.

For more details, you can use the function setConnectTimeout of Class URLConnection (refer to http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html) to resolve it, see the picture and code below:

enter image description here

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

Meanwhile, according to my experience,if the proxy IP and port located on-promise network environment, you can be use it on local environment, but failed on Azure. From this perspective, I think you should confirm the proxy is valid on-promise and Azure firstly.

If the connection can connect and take a long time, it is useful to set the connection timeout property as my references above .

Community
  • 1
  • 1
Peter Pan
  • 23,476
  • 4
  • 25
  • 43
0

is your proxy https? if so, use jvm arguments https. not http:

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

ex. -Dhttps.proxyHost=myproxy.com -Dhttps.proxyPort=2021