Skip to content

Commit

Permalink
fix proxy auth
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Sep 13, 2022
1 parent be95f1a commit 9938f72
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
22 changes: 22 additions & 0 deletions aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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(proxy)) {
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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9938f72

Please sign in to comment.