http 请求修改

This commit is contained in:
even 2025-05-21 20:30:35 +08:00
parent eff37eb9bf
commit fd94a43f9c
3 changed files with 33 additions and 20 deletions

View File

@ -58,6 +58,11 @@
<artifactId>winrm4j</artifactId> <artifactId>winrm4j</artifactId>
<version>0.12.0</version> <!-- 最新稳定版 --> <version>0.12.0</version> <!-- 最新稳定版 -->
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version> <!-- 建议使用最新稳定版本 -->
</dependency>
</dependencies> </dependencies>

View File

@ -1,10 +1,13 @@
package cd.casic.ci.process.engine.worker; package cd.casic.ci.process.engine.worker;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.http.impl.client.HttpClients; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.http.ssl.SSLContextBuilder; import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.core5.ssl.TrustStrategy;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -12,6 +15,7 @@ import javax.net.ssl.SSLContext;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.KeyStoreException; import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
/** /**
* @author HopeLi * @author HopeLi
@ -21,23 +25,27 @@ import java.security.NoSuchAlgorithmException;
* @Description: * @Description:
*/ */
public abstract class HttpWorker extends BaseWorker{ public abstract class HttpWorker extends BaseWorker{
public static RestTemplate getRestTemplateWithoutSANCheck() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException { public static RestTemplate getRestTemplateWithoutSANCheck() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, KeyStoreException {
// 创建一个信任所有证书的 SSL 上下文 // 创建信任所有证书的SSL上下文
SSLContext sslContext = new SSLContextBuilder() SSLContext sslContext = SSLContextBuilder.create()
.loadTrustMaterial(null, new TrustSelfSignedStrategy()) .loadTrustMaterial(null, (TrustStrategy) (chain, authType) -> true)
.build(); .build();
// 创建一个不验证主机名的主机名验证器 // 配置连接工厂
CloseableHttpClient httpClient = HttpClients.custom() CloseableHttpClient httpClient = HttpClients.custom()
.setSSLContext(sslContext) .setConnectionManager(
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
.setSslContext(sslContext)
.setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build())
.build())
.build(); .build();
// 创建一个自定义的请求工厂 // 创建RestTemplate
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); HttpComponentsClientHttpRequestFactory factory =
requestFactory.setHttpClient(httpClient); new HttpComponentsClientHttpRequestFactory(httpClient);
return new RestTemplate(factory);
return new RestTemplate(requestFactory);
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -41,7 +41,7 @@ import java.util.*;
*/ */
@Slf4j @Slf4j
@Plugin(taskType = "ScaSbom") @Plugin(taskType = "ScaSbom")
public class ScaSbomWorker extends BaseWorker{ public class ScaSbomWorker extends HttpWorker{
private static final int POLLING_INTERVAL = 5000; // 轮询间隔单位毫秒 private static final int POLLING_INTERVAL = 5000; // 轮询间隔单位毫秒
private static final int MAX_POLLING_TIMES = 100; // 最大退出次数 private static final int MAX_POLLING_TIMES = 100; // 最大退出次数
@ -78,7 +78,7 @@ public class ScaSbomWorker extends BaseWorker{
PipPipeline pipeline = pipelineService.getById(pipelineId); PipPipeline pipeline = pipelineService.getById(pipelineId);
//根据目标id查询目标信息 //根据目标id查询目标信息
if (StringUtils.isEmpty(pipeline.getTargetVersionId())){ if (StringUtils.isEmpty(pipeline.getTargetVersionId())){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标文件不存在") throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标文件不存在");
} }
TargetVersion targetVersion = targetVersionService.getById(pipeline.getTargetVersionId()); TargetVersion targetVersion = targetVersionService.getById(pipeline.getTargetVersionId());
filePath = targetVersion.getFilePath(); filePath = targetVersion.getFilePath();
@ -95,7 +95,7 @@ public class ScaSbomWorker extends BaseWorker{
handleUpload(workerParam, contextDef, pipTask.getTaskProperties(), file); handleUpload(workerParam, contextDef, pipTask.getTaskProperties(), file);
}catch (Exception e){ }catch (Exception e){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"SCA-SBOM节点执行失败") throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"SCA-SBOM节点执行失败");
} }
} }