Explorar el Código

扩展ByteArray根据URL初始化的方法,支持https协议

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi hace 3 años
padre
commit
e96dba6342

+ 21 - 2
tools-common/src/main/java/com/qmth/boot/tools/models/ByteArray.java

@@ -2,7 +2,9 @@ package com.qmth.boot.tools.models;
 
 import com.qmth.boot.tools.codec.CodecUtils;
 import com.qmth.boot.tools.io.IOUtils;
+import com.qmth.boot.tools.ssl.AllAcceptSSLContext;
 
+import javax.net.ssl.HttpsURLConnection;
 import java.io.*;
 import java.net.URL;
 import java.nio.charset.Charset;
@@ -34,8 +36,25 @@ public class ByteArray {
      * @param url
      * @return
      */
-    public static ByteArray fromUrl(String url) throws IOException {
-        return ByteArray.fromInputStream(new URL(url).openStream());
+    public static ByteArray fromUrl(String url) throws Exception {
+        URL urlObj = new URL(url);
+        InputStream ins = null;
+        if (urlObj.getProtocol().equalsIgnoreCase("http")) {
+            ins = new URL(url).openStream();
+        } else if (urlObj.getProtocol().equalsIgnoreCase("https")) {
+            HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
+            conn.setSSLSocketFactory(AllAcceptSSLContext.get().getSocketFactory());
+            conn.setHostnameVerifier((s, sslSession) -> true);
+            conn.setInstanceFollowRedirects(false);
+            ins = conn.getInputStream();
+        } else {
+            throw new RuntimeException("Invalid url protocol, should be http or https");
+        }
+        try {
+            return ByteArray.fromInputStream(ins);
+        } finally {
+            IOUtils.closeQuietly(ins);
+        }
     }
 
     /**

+ 17 - 0
tools-common/src/main/java/com/qmth/boot/tools/ssl/AllAcceptSSLContext.java

@@ -0,0 +1,17 @@
+package com.qmth.boot.tools.ssl;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+
+public class AllAcceptSSLContext {
+
+    private static SSLContext context;
+
+    public static SSLContext get() throws Exception {
+        if (context == null) {
+            context = SSLContext.getInstance("SSL", "SunJSSE");
+            context.init(null, new TrustManager[] { new AllAcceptTrustManager() }, new java.security.SecureRandom());
+        }
+        return context;
+    }
+}

+ 23 - 0
tools-common/src/main/java/com/qmth/boot/tools/ssl/AllAcceptTrustManager.java

@@ -0,0 +1,23 @@
+package com.qmth.boot.tools.ssl;
+
+import javax.net.ssl.X509TrustManager;
+import java.security.cert.X509Certificate;
+
+/**
+ * 全部通过的SSL配置
+ */
+public class AllAcceptTrustManager implements X509TrustManager {
+
+    @Override
+    public void checkClientTrusted(X509Certificate[] certificates, String authType) {
+    }
+
+    @Override
+    public void checkServerTrusted(X509Certificate[] ax509certificate, String s) {
+    }
+
+    @Override
+    public X509Certificate[] getAcceptedIssuers() {
+        return null;
+    }
+}

+ 16 - 0
tools-common/src/test/java/com/qmth/boot/test/tools/byteArray/ByteArrayTest.java

@@ -0,0 +1,16 @@
+package com.qmth.boot.test.tools.byteArray;
+
+import com.qmth.boot.tools.models.ByteArray;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ByteArrayTest {
+
+    @Test
+    public void test() throws Exception {
+        ByteArray array = ByteArray.fromUrl("https://www.qmth.com.cn");
+        Assert.assertNotNull(array);
+        Assert.assertTrue(array.value().length > 0);
+    }
+
+}