Pārlūkot izejas kodu

fix: 增加了跳过SSL证书步骤

DingXiaoYu 2 gadi atpakaļ
vecāks
revīzija
d706abb8a9

+ 37 - 1
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/serviceImpl/HttpServiceImpl.java

@@ -22,6 +22,8 @@ import org.apache.http.impl.client.HttpClients;
 import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.HTTP;
 import org.apache.http.protocol.HttpContext;
+import org.apache.http.ssl.SSLContexts;
+import org.apache.http.ssl.TrustStrategy;
 import org.apache.http.util.EntityUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -30,15 +32,28 @@ import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
 import org.springframework.web.multipart.MultipartFile;
 
+import javax.net.ssl.*;
 import javax.servlet.http.HttpServletRequest;
 import java.nio.charset.StandardCharsets;
 import java.io.File;
 import java.io.IOException;
+import java.security.KeyStoreException;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.ssl.SSLContexts;
+import javax.net.ssl.*;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+
 /**
  * @author wind
  */
@@ -46,6 +61,27 @@ import java.util.Optional;
 public class HttpServiceImpl implements HttpService {
     private final static Logger log = LoggerFactory.getLogger(HttpService.class);
 
+    public static CloseableHttpClient createUnsafeHttpClient() {
+        try {
+            SSLContext sslContext = SSLContexts.custom()
+                    .loadTrustMaterial(new TrustStrategy() {
+                        @Override
+                        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
+                            return true; // 忽略证书验证
+                        }
+                    })
+                    .build();
+            SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
+            CloseableHttpClient httpClient = HttpClients.custom()
+                    .setSSLSocketFactory(sslConnectionFactory)
+                    .build();
+            return httpClient;
+        } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
     @Override
     public String sendPost(String uri, JSONObject requestBody, List<NameValuePair> params, String token) throws Exception {
         String completedUri;
@@ -60,7 +96,7 @@ public class HttpServiceImpl implements HttpService {
         httpPost.setHeader("Content-Type", "application/json;chartset=UTF-8");
         httpPost.setEntity(new StringEntity(JSON.toJSONString(requestBody), StandardCharsets.UTF_8));
         httpPost.setConfig(RequestConfig.DEFAULT);
-        CloseableHttpClient client = HttpClients.createDefault();
+        CloseableHttpClient client = HttpServiceImpl.createUnsafeHttpClient();
         CloseableHttpResponse response = client.execute(httpPost);
         HttpEntity entity = response.getEntity();
         String result = EntityUtils.toString(entity);