From 36eec7e73b661338239797f9568476f2a38ac80d Mon Sep 17 00:00:00 2001 From: Hoshi <1196756653@qq.com> Date: Wed, 6 Nov 2024 17:39:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8C=96=E8=BF=90=E7=BB=B4?= =?UTF-8?q?=EF=BC=8C=E4=B8=BB=E6=9C=BA=E8=B5=84=E6=BA=90=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/internal/CmpInternetService.java | 19 +++++++++++--- .../internal/IgnoreVerifyHttpClient.java | 26 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 bocloud.cop.service/src/main/java/com/bocloud/cop/service/internal/IgnoreVerifyHttpClient.java diff --git a/bocloud.cop.service/src/main/java/com/bocloud/cop/service/internal/CmpInternetService.java b/bocloud.cop.service/src/main/java/com/bocloud/cop/service/internal/CmpInternetService.java index 552a462..2b81093 100644 --- a/bocloud.cop.service/src/main/java/com/bocloud/cop/service/internal/CmpInternetService.java +++ b/bocloud.cop.service/src/main/java/com/bocloud/cop/service/internal/CmpInternetService.java @@ -2,13 +2,20 @@ package com.bocloud.cop.service.internal; import com.alibaba.fastjson.JSON; import com.megatron.common.encrypt.AESEncryptor; +import com.megatron.common.http.ConnectionManagerHolder; import com.megatron.common.http.HttpClient; import com.megatron.common.model.GeneralResult; import com.megatron.common.model.Param; import com.megatron.common.model.Sign; import lombok.extern.slf4j.Slf4j; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -17,6 +24,9 @@ import org.springframework.util.Assert; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; import java.util.Collections; import java.util.List; @@ -42,7 +52,7 @@ public class CmpInternetService { "?page=1" + "&rows="+Integer.MAX_VALUE + "¶ms="+ URLEncoder.encode(JSON.toJSONString(httpParams), StandardCharsets.UTF_8); - HttpClient httpClient = new HttpClient(); + HttpClient httpClient = new IgnoreVerifyHttpClient(); HttpGet httpGet = new HttpGet(httpUrl); httpGet.addHeader("bsm-token", this.getToken()); CloseableHttpResponse httpResponse = httpClient.getHttpClient().execute(httpGet); @@ -51,6 +61,7 @@ public class CmpInternetService { Assert.isTrue(httpCode == 200, httpBody); return JSON.parseObject(httpBody, GeneralResult.class); } catch (IOException e) { + log.error(e.getMessage(), e); return new GeneralResult<>(false, e.getMessage()); } } @@ -64,7 +75,7 @@ public class CmpInternetService { "?page=1" + "&rows="+Integer.MAX_VALUE + "¶ms="+ URLEncoder.encode(JSON.toJSONString(httpParams), StandardCharsets.UTF_8); - HttpClient httpClient = new HttpClient(); + HttpClient httpClient = new IgnoreVerifyHttpClient(); HttpGet httpGet = new HttpGet(httpUrl); httpGet.addHeader("bsm-token", this.getToken()); CloseableHttpResponse httpResponse = httpClient.getHttpClient().execute(httpGet); @@ -73,6 +84,7 @@ public class CmpInternetService { Assert.isTrue(httpCode == 200, httpBody); return JSON.parseObject(httpBody, GeneralResult.class); } catch (IOException e) { + log.error(e.getMessage(), e); return new GeneralResult<>(false, e.getMessage()); } } @@ -86,7 +98,7 @@ public class CmpInternetService { "?page=1" + "&rows="+Integer.MAX_VALUE + "¶ms="+ URLEncoder.encode(JSON.toJSONString(httpParams), StandardCharsets.UTF_8); - HttpClient httpClient = new HttpClient(); + HttpClient httpClient = new IgnoreVerifyHttpClient(); HttpGet httpGet = new HttpGet(httpUrl); httpGet.addHeader("bsm-token", this.getToken()); CloseableHttpResponse httpResponse = httpClient.getHttpClient().execute(httpGet); @@ -95,6 +107,7 @@ public class CmpInternetService { Assert.isTrue(httpCode == 200, httpBody); return JSON.parseObject(httpBody, GeneralResult.class); } catch (IOException e) { + log.error(e.getMessage(), e); return new GeneralResult<>(false, e.getMessage()); } } diff --git a/bocloud.cop.service/src/main/java/com/bocloud/cop/service/internal/IgnoreVerifyHttpClient.java b/bocloud.cop.service/src/main/java/com/bocloud/cop/service/internal/IgnoreVerifyHttpClient.java new file mode 100644 index 0000000..601a585 --- /dev/null +++ b/bocloud.cop.service/src/main/java/com/bocloud/cop/service/internal/IgnoreVerifyHttpClient.java @@ -0,0 +1,26 @@ +package com.bocloud.cop.service.internal; + +import com.megatron.common.http.ConnectionManagerHolder; +import com.megatron.common.http.HttpClient; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.ssl.SSLContextBuilder; + +public class IgnoreVerifyHttpClient extends HttpClient { + + @Override + protected void init() { + try { + this.config = RequestConfig.custom().setSocketTimeout(getTimeout()).setConnectTimeout(getTimeout()).setProxy(this.proxy).setCookieSpec("standard-strict").build(); + this.httpClient = HttpClientBuilder.create() + .setSSLContext(new SSLContextBuilder().loadTrustMaterial((chain, authType) -> true).build()) + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .setConnectionManager(ConnectionManagerHolder.getConnectionManager()) + .setConnectionManagerShared(true) + .setDefaultRequestConfig(this.config).build(); + } catch (Exception e) { + throw new RuntimeException("IgnoreVerifyHttpClient init error", e); + } + } +}