fix(测试跟踪): 缺陷接口调用支持http系统代理

--bug=1014343 --user=陈建星 【测试跟踪】github# 15226,与#7586同样的问题,在metersphere需要使用外网代理访问外网的环境里,在宿主机和docker里都设置了外网代理环境变量,且开通了对应外网访问权限,但在进行jira cloud集成的时候,仍然报连接超时错误 https://www.tapd.cn/55049933/s/1191158
This commit is contained in:
chenjianxing 2022-06-28 11:50:32 +08:00 committed by f2c-ci-robot[bot]
parent 3899c4be50
commit f2cb0393d5
2 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,44 @@
package io.metersphere.commons.utils;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.net.*;
import java.util.Arrays;
import java.util.List;
public class EnvProxySelector extends ProxySelector {
ProxySelector defaultProxySelector = ProxySelector.getDefault();
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
LogUtil.error("connectFailed : " + uri);
}
/**
* 获取环境变量http代理配置
*
* @param uri
* @return
*/
@Override
public List<Proxy> select(URI uri) {
String httpProxy = System.getenv("http_proxy");
String httpsProxy = System.getenv("https_proxy");
URI proxy = null;
try {
if (StringUtils.equalsIgnoreCase(uri.getScheme(), "https")
&& StringUtils.isNotBlank(httpsProxy)) {
proxy = new URI(httpsProxy);
} else if (StringUtils.isNotBlank(httpProxy)) {
proxy = new URI(httpProxy);
}
if (proxy != null) {
return Arrays.asList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHost(), proxy.getPort())));
}
} catch (URISyntaxException e) {
LogUtil.error(e);
}
return defaultProxySelector.select(uri);
}
}

View File

@ -4,11 +4,13 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.EncryptUtils; import io.metersphere.commons.utils.EncryptUtils;
import io.metersphere.commons.utils.EnvProxySelector;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
@ -31,7 +33,7 @@ public abstract class BaseClient {
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext); SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom() CloseableHttpClient httpClient = HttpClients.custom()
// 可以支持设置系统代理 // 可以支持设置系统代理
.useSystemProperties() .setRoutePlanner(new SystemDefaultRoutePlanner(new EnvProxySelector()))
.setSSLSocketFactory(csf) .setSSLSocketFactory(csf)
.build(); .build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();