Skip to content

Commit dd18fc8

Browse files
authored
Merge pull request #134 from gnongsie/master
Proxy Authentication fix - Additional scenarios
2 parents 00d6e9d + 130665b commit dd18fc8

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

src/main/java/net/authorize/util/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public final class Constants {
66
public static final String HTTPS_USE_PROXY = "https.proxyUse";
77
public static final String HTTPS_PROXY_HOST = "https.proxyHost";
88
public static final String HTTPS_PROXY_PORT = "https.proxyPort";
9-
public static final String HTTPS_PROXY_USERNAME = "https.proxyUser";
9+
public static final String HTTPS_PROXY_USERNAME = "https.proxyUsername";
1010
public static final String HTTPS_PROXY_PASSWORD = "https.proxyPassword";
1111

1212
public static final String HTTP_USE_PROXY = "http.proxyUse";

src/main/java/net/authorize/util/HttpClient.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,37 @@ static CloseableHttpClient getHttpsClient() throws Exception {
344344

345345
CloseableHttpClient httpClient;
346346

347-
if ( !UseProxy && ( ProxyHost == null || proxyUsername == null || proxyPassword == null ) ) {
347+
if ( UseProxy && ProxyHost != null) {
348+
349+
HttpClientBuilder hcBuilder;
350+
if (proxyUsername != null && proxyPassword != null) {
351+
LogHelper.info(logger, "Setting up proxy to URL with Authentication: '%s://%s@%s:%d'", Constants.PROXY_PROTOCOL, proxyUsername, ProxyHost, ProxyPort);
352+
CredentialsProvider credsProvider = new BasicCredentialsProvider();
353+
AuthScope proxyScope = new AuthScope(ProxyHost, ProxyPort);
354+
Credentials proxyCreds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
355+
credsProvider.setCredentials(proxyScope, proxyCreds);
356+
hcBuilder = HttpClients.custom()
357+
.setSSLSocketFactory(sslSocketFactory)
358+
.setDefaultRequestConfig(requestConfig)
359+
.setRedirectStrategy(new LaxRedirectStrategy())
360+
.setDefaultCredentialsProvider(credsProvider);
361+
}
362+
else {
363+
LogHelper.info(logger, "Setting up proxy to URL: '%s://%s:%d'", Constants.PROXY_PROTOCOL, ProxyHost, ProxyPort);
364+
hcBuilder = HttpClients.custom()
365+
.setSSLSocketFactory(sslSocketFactory)
366+
.setDefaultRequestConfig(requestConfig)
367+
.setRedirectStrategy(new LaxRedirectStrategy());
368+
}
369+
370+
HttpHost httpProxy = new HttpHost(ProxyHost, ProxyPort, Constants.PROXY_PROTOCOL);
371+
hcBuilder.setProxy(httpProxy);
372+
373+
httpClient = hcBuilder.build();
374+
375+
proxySet = true;
376+
}
377+
else {
348378
LogHelper.warn(logger, "Defaulting to non-proxy environment");
349379

350380
httpClient = HttpClients.custom()
@@ -353,30 +383,6 @@ static CloseableHttpClient getHttpsClient() throws Exception {
353383
.setRedirectStrategy(new LaxRedirectStrategy())
354384
.build();
355385
}
356-
357-
else {
358-
359-
LogHelper.info(logger, "Setting up proxy to URL: '%s://%s:%d'", Constants.PROXY_PROTOCOL, ProxyHost, ProxyPort);
360-
CredentialsProvider credsProvider = new BasicCredentialsProvider();
361-
362-
AuthScope proxyScope = new AuthScope(ProxyHost, ProxyPort);
363-
Credentials proxyCreds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
364-
credsProvider.setCredentials(proxyScope, proxyCreds);
365-
366-
HttpClientBuilder hcBuilder = HttpClients.custom()
367-
.setSSLSocketFactory(sslSocketFactory)
368-
.setDefaultRequestConfig(requestConfig)
369-
.setRedirectStrategy(new LaxRedirectStrategy())
370-
.setDefaultCredentialsProvider(credsProvider);
371-
372-
HttpHost httpProxy = new HttpHost(ProxyHost, ProxyPort, Constants.PROXY_PROTOCOL);
373-
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(httpProxy);
374-
hcBuilder.setRoutePlanner(routePlanner);
375-
376-
httpClient = hcBuilder.build();
377-
378-
proxySet = true;
379-
}
380386

381387
return httpClient;
382388
} catch (Exception e) {

0 commit comments

Comments
 (0)