Skip to content

Commit e29960c

Browse files
committed
fix proxy auth
1 parent be95f1a commit e29960c

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

Diff for: aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/HttpUtil.java

+22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import com.aliyuncs.exceptions.ClientException;
1919
import com.aliyuncs.utils.StringUtils;
20+
import org.apache.http.auth.AuthScope;
21+
import org.apache.http.auth.UsernamePasswordCredentials;
22+
import org.apache.http.client.CredentialsProvider;
2023

2124
public class HttpUtil {
2225

@@ -174,4 +177,23 @@ public static boolean needProxy(String targetHost, String clientNoProxyList, Str
174177
}
175178
return true;
176179
}
180+
181+
public static void readCredentialsFromApacheProxy(CredentialsProvider credentialsProvider, String proxy)
182+
throws ClientException {
183+
try {
184+
if (!StringUtils.isEmpty(proxy)) {
185+
URL proxyUrl = new URL(proxy);
186+
String userInfo = proxyUrl.getUserInfo();
187+
if (!StringUtils.isEmpty(userInfo)) {
188+
final String[] userMessage = userInfo.split(":");
189+
credentialsProvider.setCredentials(
190+
new AuthScope(proxyUrl.getHost(),
191+
proxyUrl.getPort()),
192+
new UsernamePasswordCredentials(userMessage[0], userMessage[1]));
193+
}
194+
}
195+
} catch (IOException e) {
196+
throw new ClientException("SDK.InvalidProxy", "proxy url is invalid");
197+
}
198+
}
177199
}

Diff for: aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/ApacheHttpClient.java

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import com.aliyuncs.utils.StringUtils;
88
import org.apache.http.Header;
99
import org.apache.http.HttpHost;
10+
import org.apache.http.auth.AuthScope;
11+
import org.apache.http.auth.Credentials;
12+
import org.apache.http.auth.UsernamePasswordCredentials;
1013
import org.apache.http.client.CredentialsProvider;
1114
import org.apache.http.client.config.RequestConfig;
1215
import org.apache.http.client.entity.EntityBuilder;
@@ -25,6 +28,7 @@
2528
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
2629
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
2730
import org.apache.http.impl.client.HttpClientBuilder;
31+
import org.apache.http.impl.client.BasicCredentialsProvider;
2832
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
2933
import org.apache.http.protocol.HttpContext;
3034
import org.apache.http.util.EntityUtils;
@@ -189,6 +193,19 @@ protected void init(final HttpClientConfig config0) throws ClientException {
189193
CredentialsProvider credentialsProvider = this.clientConfig.getCredentialsProvider();
190194
if (null != credentialsProvider) {
191195
builder.setDefaultCredentialsProvider(credentialsProvider);
196+
} else {
197+
BasicCredentialsProvider crePro = new BasicCredentialsProvider();
198+
if (!StringUtils.isEmpty(clientConfig.getHttpsProxy())) {
199+
HttpUtil.readCredentialsFromApacheProxy(crePro, clientConfig.getHttpsProxy());
200+
} else if (!StringUtils.isEmpty(EnvironmentUtils.getHttpsProxy())) {
201+
HttpUtil.readCredentialsFromApacheProxy(crePro, EnvironmentUtils.getHttpsProxy());
202+
}
203+
if (!StringUtils.isEmpty(clientConfig.getHttpProxy())) {
204+
HttpUtil.readCredentialsFromApacheProxy(crePro, clientConfig.getHttpProxy());
205+
} else if (!StringUtils.isEmpty(EnvironmentUtils.getHttpProxy())) {
206+
HttpUtil.readCredentialsFromApacheProxy(crePro, EnvironmentUtils.getHttpProxy());
207+
}
208+
builder.setDefaultCredentialsProvider(crePro);
192209
}
193210
// default request config
194211
RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout((int) config

Diff for: aliyun-java-sdk-core/src/test/java/com/aliyuncs/http/HttpUtilTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
import static org.mockito.Mockito.mock;
44

55
import java.net.Proxy;
6+
import java.net.URL;
67
import java.util.HashMap;
78
import java.util.Map;
89

10+
import com.sun.org.apache.xalan.internal.lib.ExsltBase;
911
import org.apache.http.HttpHost;
12+
import org.apache.http.auth.AuthScope;
13+
import org.apache.http.auth.Credentials;
14+
import org.apache.http.impl.client.BasicCredentialsProvider;
1015
import org.junit.Assert;
1116
import org.junit.Rule;
1217
import org.junit.Test;
@@ -216,4 +221,32 @@ public void testNeedProxyHasEnvProxyList() {
216221
boolean need = HttpUtil.needProxy("http://targethost.com", "", "http://www.aliyun.com,http://targethost.com");
217222
Assert.assertFalse(need);
218223
}
224+
225+
@Test
226+
public void testReadCredentialsFromApacheProxy() throws Exception {
227+
BasicCredentialsProvider crePro = new BasicCredentialsProvider();
228+
String proxy = "http://www.aliyun.com:80";
229+
URL proxyUrl = new URL(proxy);
230+
try {
231+
// proxy without auth
232+
HttpUtil.readCredentialsFromApacheProxy(crePro, proxy);
233+
Assert.assertNull(crePro.getCredentials(new AuthScope(proxyUrl.getHost(),
234+
proxyUrl.getPort())));
235+
// proxy with auth
236+
proxy = "http://user:[email protected]";
237+
proxyUrl = new URL(proxy);
238+
HttpUtil.readCredentialsFromApacheProxy(crePro, proxy);
239+
Credentials credentials = crePro.getCredentials(new AuthScope(proxyUrl.getHost(),
240+
proxyUrl.getPort()));
241+
Assert.assertEquals("user", credentials.getUserPrincipal().getName());
242+
Assert.assertEquals("passwd", credentials.getPassword());
243+
// invalid proxy
244+
proxy = "http0://www.aliyun.com";
245+
HttpUtil.readCredentialsFromApacheProxy(crePro, proxy);
246+
Assert.fail();
247+
} catch (ClientException e) {
248+
Assert.assertEquals("SDK.InvalidProxy : proxy url is invalid", e.getMessage());
249+
}
250+
251+
}
219252
}

0 commit comments

Comments
 (0)