Commit bb94831177ca7c7c46ba232219189f56307ab7f3
1 parent
105ad255
修复访问assist配置https访问失败的问题
Showing
2 changed files
with
59 additions
and
4 deletions
src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java
| @@ -2,9 +2,8 @@ package com.genersoft.iot.vmp.media.zlm; | @@ -2,9 +2,8 @@ package com.genersoft.iot.vmp.media.zlm; | ||
| 2 | 2 | ||
| 3 | import com.alibaba.fastjson2.JSON; | 3 | import com.alibaba.fastjson2.JSON; |
| 4 | import com.alibaba.fastjson2.JSONObject; | 4 | import com.alibaba.fastjson2.JSONObject; |
| 5 | -import com.genersoft.iot.vmp.conf.exception.ControllerException; | ||
| 6 | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; | 5 | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; |
| 7 | -import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; | 6 | +import com.genersoft.iot.vmp.utils.SSLSocketClientUtil; |
| 8 | import okhttp3.*; | 7 | import okhttp3.*; |
| 9 | import okhttp3.logging.HttpLoggingInterceptor; | 8 | import okhttp3.logging.HttpLoggingInterceptor; |
| 10 | import org.jetbrains.annotations.NotNull; | 9 | import org.jetbrains.annotations.NotNull; |
| @@ -13,11 +12,10 @@ import org.slf4j.LoggerFactory; | @@ -13,11 +12,10 @@ import org.slf4j.LoggerFactory; | ||
| 13 | import org.springframework.stereotype.Component; | 12 | import org.springframework.stereotype.Component; |
| 14 | import org.springframework.util.ObjectUtils; | 13 | import org.springframework.util.ObjectUtils; |
| 15 | 14 | ||
| 15 | +import javax.net.ssl.X509TrustManager; | ||
| 16 | import java.io.IOException; | 16 | import java.io.IOException; |
| 17 | import java.net.ConnectException; | 17 | import java.net.ConnectException; |
| 18 | -import java.net.MalformedURLException; | ||
| 19 | import java.net.SocketTimeoutException; | 18 | import java.net.SocketTimeoutException; |
| 20 | -import java.net.URL; | ||
| 21 | import java.util.HashMap; | 19 | import java.util.HashMap; |
| 22 | import java.util.List; | 20 | import java.util.List; |
| 23 | import java.util.Map; | 21 | import java.util.Map; |
| @@ -61,6 +59,10 @@ public class AssistRESTfulUtils { | @@ -61,6 +59,10 @@ public class AssistRESTfulUtils { | ||
| 61 | // OkHttp進行添加攔截器loggingInterceptor | 59 | // OkHttp進行添加攔截器loggingInterceptor |
| 62 | httpClientBuilder.addInterceptor(logging); | 60 | httpClientBuilder.addInterceptor(logging); |
| 63 | } | 61 | } |
| 62 | + X509TrustManager manager = SSLSocketClientUtil.getX509TrustManager(); | ||
| 63 | + // 设置ssl | ||
| 64 | + httpClientBuilder.sslSocketFactory(SSLSocketClientUtil.getSocketFactory(manager), manager); | ||
| 65 | + httpClientBuilder.hostnameVerifier(SSLSocketClientUtil.getHostnameVerifier());//忽略校验 | ||
| 64 | client = httpClientBuilder.build(); | 66 | client = httpClientBuilder.build(); |
| 65 | } | 67 | } |
| 66 | return client; | 68 | return client; |
src/main/java/com/genersoft/iot/vmp/utils/SSLSocketClientUtil.java
0 → 100644
| 1 | +package com.genersoft.iot.vmp.utils; | ||
| 2 | + | ||
| 3 | +import javax.net.ssl.*; | ||
| 4 | +import java.security.KeyManagementException; | ||
| 5 | +import java.security.NoSuchAlgorithmException; | ||
| 6 | +import java.security.SecureRandom; | ||
| 7 | +import java.security.cert.CertificateException; | ||
| 8 | +import java.security.cert.X509Certificate; | ||
| 9 | + | ||
| 10 | +public class SSLSocketClientUtil { | ||
| 11 | + public static SSLSocketFactory getSocketFactory(TrustManager manager) { | ||
| 12 | + SSLSocketFactory socketFactory = null; | ||
| 13 | + try { | ||
| 14 | + SSLContext sslContext = SSLContext.getInstance("SSL"); | ||
| 15 | + sslContext.init(null, new TrustManager[]{manager}, new SecureRandom()); | ||
| 16 | + socketFactory = sslContext.getSocketFactory(); | ||
| 17 | + } catch (NoSuchAlgorithmException e) { | ||
| 18 | + e.printStackTrace(); | ||
| 19 | + } catch (KeyManagementException e) { | ||
| 20 | + e.printStackTrace(); | ||
| 21 | + } | ||
| 22 | + return socketFactory; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + public static X509TrustManager getX509TrustManager() { | ||
| 26 | + return new X509TrustManager() { | ||
| 27 | + @Override | ||
| 28 | + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { | ||
| 29 | + | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + @Override | ||
| 33 | + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { | ||
| 34 | + | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + @Override | ||
| 38 | + public X509Certificate[] getAcceptedIssuers() { | ||
| 39 | + return new X509Certificate[0]; | ||
| 40 | + } | ||
| 41 | + }; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public static HostnameVerifier getHostnameVerifier() { | ||
| 45 | + HostnameVerifier hostnameVerifier = new HostnameVerifier() { | ||
| 46 | + @Override | ||
| 47 | + public boolean verify(String s, SSLSession sslSession) { | ||
| 48 | + return true; | ||
| 49 | + } | ||
| 50 | + }; | ||
| 51 | + return hostnameVerifier; | ||
| 52 | + } | ||
| 53 | +} |