From e29960c8d0143b92a1b9a9726d7d8172828be5c4 Mon Sep 17 00:00:00 2001 From: yndu13 Date: Thu, 18 Aug 2022 12:02:53 +0800 Subject: [PATCH] fix proxy auth --- .../main/java/com/aliyuncs/http/HttpUtil.java | 22 +++++++++++++ .../http/clients/ApacheHttpClient.java | 17 ++++++++++ .../java/com/aliyuncs/http/HttpUtilTest.java | 33 +++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/HttpUtil.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/HttpUtil.java index 83c430235b..d1fdefb6cb 100644 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/HttpUtil.java +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/HttpUtil.java @@ -17,6 +17,9 @@ import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.utils.StringUtils; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; public class HttpUtil { @@ -174,4 +177,23 @@ public static boolean needProxy(String targetHost, String clientNoProxyList, Str } return true; } + + public static void readCredentialsFromApacheProxy(CredentialsProvider credentialsProvider, String proxy) + throws ClientException { + try { + if (!StringUtils.isEmpty(proxy)) { + URL proxyUrl = new URL(proxy); + String userInfo = proxyUrl.getUserInfo(); + if (!StringUtils.isEmpty(userInfo)) { + final String[] userMessage = userInfo.split(":"); + credentialsProvider.setCredentials( + new AuthScope(proxyUrl.getHost(), + proxyUrl.getPort()), + new UsernamePasswordCredentials(userMessage[0], userMessage[1])); + } + } + } catch (IOException e) { + throw new ClientException("SDK.InvalidProxy", "proxy url is invalid"); + } + } } diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/ApacheHttpClient.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/ApacheHttpClient.java index 9c59f1d95d..62019142d3 100644 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/ApacheHttpClient.java +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/ApacheHttpClient.java @@ -7,6 +7,9 @@ import com.aliyuncs.utils.StringUtils; import org.apache.http.Header; import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.EntityBuilder; @@ -25,6 +28,7 @@ import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy; import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; @@ -189,6 +193,19 @@ protected void init(final HttpClientConfig config0) throws ClientException { CredentialsProvider credentialsProvider = this.clientConfig.getCredentialsProvider(); if (null != credentialsProvider) { builder.setDefaultCredentialsProvider(credentialsProvider); + } else { + BasicCredentialsProvider crePro = new BasicCredentialsProvider(); + if (!StringUtils.isEmpty(clientConfig.getHttpsProxy())) { + HttpUtil.readCredentialsFromApacheProxy(crePro, clientConfig.getHttpsProxy()); + } else if (!StringUtils.isEmpty(EnvironmentUtils.getHttpsProxy())) { + HttpUtil.readCredentialsFromApacheProxy(crePro, EnvironmentUtils.getHttpsProxy()); + } + if (!StringUtils.isEmpty(clientConfig.getHttpProxy())) { + HttpUtil.readCredentialsFromApacheProxy(crePro, clientConfig.getHttpProxy()); + } else if (!StringUtils.isEmpty(EnvironmentUtils.getHttpProxy())) { + HttpUtil.readCredentialsFromApacheProxy(crePro, EnvironmentUtils.getHttpProxy()); + } + builder.setDefaultCredentialsProvider(crePro); } // default request config RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout((int) config diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/http/HttpUtilTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/http/HttpUtilTest.java index 582d8bfc26..dd3b6d9538 100644 --- a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/http/HttpUtilTest.java +++ b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/http/HttpUtilTest.java @@ -3,10 +3,15 @@ import static org.mockito.Mockito.mock; import java.net.Proxy; +import java.net.URL; import java.util.HashMap; import java.util.Map; +import com.sun.org.apache.xalan.internal.lib.ExsltBase; import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.impl.client.BasicCredentialsProvider; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -216,4 +221,32 @@ public void testNeedProxyHasEnvProxyList() { boolean need = HttpUtil.needProxy("http://targethost.com", "", "http://www.aliyun.com,http://targethost.com"); Assert.assertFalse(need); } + + @Test + public void testReadCredentialsFromApacheProxy() throws Exception { + BasicCredentialsProvider crePro = new BasicCredentialsProvider(); + String proxy = "http://www.aliyun.com:80"; + URL proxyUrl = new URL(proxy); + try { + // proxy without auth + HttpUtil.readCredentialsFromApacheProxy(crePro, proxy); + Assert.assertNull(crePro.getCredentials(new AuthScope(proxyUrl.getHost(), + proxyUrl.getPort()))); + // proxy with auth + proxy = "http://user:passwd@www.aliyun.com"; + proxyUrl = new URL(proxy); + HttpUtil.readCredentialsFromApacheProxy(crePro, proxy); + Credentials credentials = crePro.getCredentials(new AuthScope(proxyUrl.getHost(), + proxyUrl.getPort())); + Assert.assertEquals("user", credentials.getUserPrincipal().getName()); + Assert.assertEquals("passwd", credentials.getPassword()); + // invalid proxy + proxy = "http0://www.aliyun.com"; + HttpUtil.readCredentialsFromApacheProxy(crePro, proxy); + Assert.fail(); + } catch (ClientException e) { + Assert.assertEquals("SDK.InvalidProxy : proxy url is invalid", e.getMessage()); + } + + } }