diff --git a/source/java/org/rsna/util/HttpUtil.java b/source/java/org/rsna/util/HttpUtil.java index ebd5af2..baab7ec 100644 --- a/source/java/org/rsna/util/HttpUtil.java +++ b/source/java/org/rsna/util/HttpUtil.java @@ -8,9 +8,11 @@ package org.rsna.util; import java.io.*; +import java.net.Authenticator; import java.net.CookieHandler; import java.net.CookieManager; import java.net.HttpURLConnection; +import java.net.PasswordAuthentication; import java.net.URL; import java.security.SecureRandom; import java.util.Properties; @@ -56,6 +58,7 @@ public static HttpURLConnection getConnection(String urlString) throws Exception */ public static HttpURLConnection getConnection(URL url) throws Exception { + initializeProxyAuthenticator(); String protocol = url.getProtocol().toLowerCase(); if (!protocol.startsWith("https") && !protocol.startsWith("http")) { throw new Exception("Unsupported protocol ("+protocol+")"); @@ -115,4 +118,33 @@ public static CookieManager initializeCookieManager() { CookieHandler.setDefault(cookieManager); return cookieManager; } + + /** + * Initialize a default Authenticator for proxy server basic authentication. + */ + private static void initializeProxyAuthenticator() { + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + if (getRequestorType() == RequestorType.PROXY) { + String prot = getRequestingProtocol().toLowerCase(); + String host = System.getProperty(prot + ".proxyHost"); + String port = System.getProperty(prot + ".proxyPort"); + String user = System.getProperty(prot + ".proxyUser"); + String password = System.getProperty(prot + ".proxyPassword"); + if (user != null && password != null) { + if (getRequestingHost().equalsIgnoreCase(host)) { + if (Integer.parseInt(port) == getRequestingPort()) { + // Host and port seems to match configured proxy details + return new PasswordAuthentication(user, password.toCharArray()); + } + } + } + } + return null; + } + }); + // Need to remove basic auth from disabled schemes in Java 8 + System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); + } }