refactor: 增加一个restTemplateIgnoreSSL

This commit is contained in:
Captain.B 2021-03-09 10:37:37 +08:00
parent 3c970efd2d
commit 9ba7639437
2 changed files with 38 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.EncryptUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.controller.request.IntegrationRequest;
import io.metersphere.service.IntegrationService;
@ -15,10 +16,21 @@ import io.metersphere.service.ProjectService;
import io.metersphere.track.request.testcase.IssuesRequest;
import io.metersphere.track.service.TestCaseService;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.security.cert.X509Certificate;
public abstract class AbstractIssuePlatform implements IssuesPlatform {
private static RestTemplate restTemplate;
protected IntegrationService integrationService;
protected TestCaseIssuesMapper testCaseIssuesMapper;
protected ProjectService projectService;
@ -26,8 +38,29 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform {
protected IssuesMapper issuesMapper;
protected ExtIssuesMapper extIssuesMapper;
protected RestTemplate restTemplateIgnoreSSL;
protected String testCaseId;
static {
try {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
restTemplate = new RestTemplate(requestFactory);
} catch (Exception e) {
LogUtil.error(e);
}
}
public AbstractIssuePlatform(IssuesRequest issuesRequest) {
this.integrationService = CommonBeanFactory.getBean(IntegrationService.class);
this.testCaseIssuesMapper = CommonBeanFactory.getBean(TestCaseIssuesMapper.class);
@ -36,6 +69,8 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform {
this.issuesMapper = CommonBeanFactory.getBean(IssuesMapper.class);
this.extIssuesMapper = CommonBeanFactory.getBean(ExtIssuesMapper.class);
this.testCaseId = issuesRequest.getTestCaseId();
//
this.restTemplateIgnoreSSL = restTemplate;
}
protected String getPlatformConfig(String platform) {
@ -64,6 +99,7 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform {
/**
* 获取平台与项目相关的属性
*
* @return 其他平台和本地项目绑定的属性值
*/
abstract String getProjectId();

View File

@ -186,8 +186,8 @@ public class JiraPlatform extends AbstractIssuePlatform {
String url = object.getString("url");
HttpHeaders headers = auth(account, password);
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
restTemplate.exchange(url + "rest/api/2/issue/createmeta", HttpMethod.GET, requestEntity, String.class);
// 忽略ssl
restTemplateIgnoreSSL.exchange(url + "rest/api/2/issue/createmeta", HttpMethod.GET, requestEntity, String.class);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
MSException.throwException("验证失败!");