From 694105aae1ec1e8e88b31cd0d1674dd7c8fb4738 Mon Sep 17 00:00:00 2001 From: Luke Wage Date: Fri, 22 Apr 2016 10:01:42 -0400 Subject: [PATCH] Add support for UseDefaultCredentials option Allow Integrated Windows Authentication to be used when authenticating to the Exchange server. --- pom.xml | 7 +++++++ .../data/core/ExchangeServiceBase.java | 18 +++++++++++++++--- .../core/request/HttpClientWebRequest.java | 5 +++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index e303359bc..375bccc65 100644 --- a/pom.xml +++ b/pom.xml @@ -97,6 +97,7 @@ 0.7.5.201505241946 4.4.1 + 4.4.1 4.4.1 1.2 2.8 @@ -219,6 +220,12 @@ ${httpclient.version} + + org.apache.httpcomponents + httpclient-win + ${httpclient-win.version} + + org.apache.httpcomponents httpcore diff --git a/src/main/java/microsoft/exchange/webservices/data/core/ExchangeServiceBase.java b/src/main/java/microsoft/exchange/webservices/data/core/ExchangeServiceBase.java index 9e383c582..162248b96 100644 --- a/src/main/java/microsoft/exchange/webservices/data/core/ExchangeServiceBase.java +++ b/src/main/java/microsoft/exchange/webservices/data/core/ExchangeServiceBase.java @@ -69,7 +69,9 @@ import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.client.WinHttpClients; import org.apache.http.impl.conn.BasicHttpClientConnectionManager; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; @@ -171,7 +173,7 @@ public abstract class ExchangeServiceBase implements Closeable { * every other constructor. */ protected ExchangeServiceBase() { - setUseDefaultCredentials(true); + setUseDefaultCredentials(false); initializeHttpClient(); initializeHttpContext(); } @@ -200,7 +202,7 @@ private void initializeHttpClient() { HttpClientConnectionManager httpConnectionManager = new BasicHttpClientConnectionManager(registry); AuthenticationStrategy authStrategy = new CookieProcessingTargetAuthenticationStrategy(); - httpClient = HttpClients.custom() + httpClient = constructHttpClientBuilder() .setConnectionManager(httpConnectionManager) .setTargetAuthenticationStrategy(authStrategy) .build(); @@ -213,11 +215,21 @@ private void initializeHttpPoolingClient() { httpConnectionManager.setDefaultMaxPerRoute(maximumPoolingConnections); AuthenticationStrategy authStrategy = new CookieProcessingTargetAuthenticationStrategy(); - httpPoolingClient = HttpClients.custom() + httpPoolingClient = constructHttpClientBuilder() .setConnectionManager(httpConnectionManager) .setTargetAuthenticationStrategy(authStrategy) .build(); } + + private HttpClientBuilder constructHttpClientBuilder(){ + if(useDefaultCredentials) { + if(System.getProperty("os.name").toLowerCase().contains("windows")) { + return WinHttpClients.custom(); + } + } + + return HttpClients.custom(); + } /** * Sets the maximum number of connections for the pooling connection manager which is used for diff --git a/src/main/java/microsoft/exchange/webservices/data/core/request/HttpClientWebRequest.java b/src/main/java/microsoft/exchange/webservices/data/core/request/HttpClientWebRequest.java index 8cd6ccb8f..69dbdab71 100644 --- a/src/main/java/microsoft/exchange/webservices/data/core/request/HttpClientWebRequest.java +++ b/src/main/java/microsoft/exchange/webservices/data/core/request/HttpClientWebRequest.java @@ -35,6 +35,7 @@ import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.impl.auth.win.WindowsCredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; @@ -152,6 +153,10 @@ public void prepareConnection() { credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY), webServiceCredentials); } + if(isUseDefaultCredentials() && System.getProperty("os.name").toLowerCase().contains("windows")) { + credentialsProvider = new WindowsCredentialsProvider(credentialsProvider); + } + httpContext.setCredentialsProvider(credentialsProvider); httpPost.setConfig(requestConfigBuilder.build());