diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/support/HttpClientSupport.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/support/HttpClientSupport.java index 8c9f7784..47eddbf3 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/support/HttpClientSupport.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/support/HttpClientSupport.java @@ -15,12 +15,15 @@ */ package org.springframework.cloud.config.server.support; +import java.net.ProxySelector; import java.security.GeneralSecurityException; import org.apache.http.client.config.RequestConfig; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.client.SystemDefaultCredentialsProvider; +import org.apache.http.impl.conn.SystemDefaultRoutePlanner; import org.apache.http.ssl.SSLContextBuilder; import org.springframework.cloud.config.server.proxy.ProxyHostCredentialsProvider; @@ -49,6 +52,9 @@ public class HttpClientSupport { httpClientBuilder.setRoutePlanner(new SchemeBasedRoutePlanner(httpsProxy, httpProxy)); httpClientBuilder.setDefaultCredentialsProvider(new ProxyHostCredentialsProvider(httpProxy, httpsProxy)); + } else { + httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())); + httpClientBuilder.setDefaultCredentialsProvider(new SystemDefaultCredentialsProvider()); } int timeout = environmentProperties.getTimeout() * 1000; diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/ConfigurableHttpConnectionFactoryIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/ConfigurableHttpConnectionFactoryIntegrationTests.java index fbb62160..aa9dbdff 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/ConfigurableHttpConnectionFactoryIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/ConfigurableHttpConnectionFactoryIntegrationTests.java @@ -16,10 +16,16 @@ package org.springframework.cloud.config.server.environment; import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.ProxySelector; +import java.net.SocketAddress; +import java.net.URI; import java.net.URL; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.apache.http.client.HttpClient; @@ -200,6 +206,39 @@ public class ConfigurableHttpConnectionFactoryIntegrationTests { makeRequest(httpClient, "https://somehost"); } + @Test + public void httpProxy_fromSystemProperty() throws Exception { + ProxySelector defaultProxySelector = ProxySelector.getDefault(); + try { + ProxySelector.setDefault(new ProxySelector() { + @Override + public List select(URI uri) { + InetSocketAddress address = new InetSocketAddress(HTTP_PROXY.getHost(), HTTP_PROXY.getPort()); + Proxy proxy = new Proxy(Proxy.Type.HTTP, address); + return Collections.singletonList(proxy); + } + + @Override + public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { + + } + }); + String repoUrl = "https://myrepo/repo.git"; + new SpringApplicationBuilder(TestConfiguration.class) + .web(WebApplicationType.NONE) + .properties(new String[] {"spring.cloud.config.server.git.uri=" + repoUrl}) + .run(); + HttpClient httpClient = getHttpClientForUrl(repoUrl); + expectedException.expectCause(allOf( + instanceOf(UnknownHostException.class), + hasProperty("message", containsString(HTTP_PROXY.getHost())))); + + makeRequest(httpClient, "http://somehost"); + } finally { + ProxySelector.setDefault(defaultProxySelector); + } + } + private String[] gitProperties(String repoUrl, ProxyHostProperties httpProxy, ProxyHostProperties httpsProxy) { List result = new ArrayList<>(); result.add("spring.cloud.config.server.git.uri=" + repoUrl); @@ -237,7 +276,8 @@ public class ConfigurableHttpConnectionFactoryIntegrationTests { private HttpClient getHttpClientForUrl(String repoUrl) throws IOException { HttpConnectionFactory connectionFactory = HttpTransport.getConnectionFactory(); - HttpConnection httpConnection = connectionFactory.create(new URL(repoUrl)); + URL url = new URL(repoUrl); + HttpConnection httpConnection = connectionFactory.create(url); assertThat(httpConnection).isInstanceOf(HttpClientConnection.class); return (HttpClient) ReflectionTestUtils.getField(httpConnection, "client"); }