From 0ec87388b0b05d2019638e37e76240c8dd540c1a Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Tue, 17 Oct 2023 16:26:45 +0800 Subject: [PATCH] =?UTF-8?q?feign=20=E5=BF=BD=E7=95=A5=20ssl=20=E8=AF=81?= =?UTF-8?q?=E4=B9=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../media/feign/IgnoreHttpsSSLClient.java | 41 +++++++++++++++++++ .../feign/IgnoreSSLFeignClientConfig.java | 15 +++++++ .../media/proxy/ZlmMediaHttpClient.java | 3 +- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/feign/IgnoreHttpsSSLClient.java create mode 100644 zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/feign/IgnoreSSLFeignClientConfig.java diff --git a/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/feign/IgnoreHttpsSSLClient.java b/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/feign/IgnoreHttpsSSLClient.java new file mode 100644 index 0000000..07ddac0 --- /dev/null +++ b/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/feign/IgnoreHttpsSSLClient.java @@ -0,0 +1,41 @@ +package cn.skcks.docking.gb28181.media.feign; + +import feign.Client; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +@Configuration +public class IgnoreHttpsSSLClient { + @Bean + @ConditionalOnMissingBean + public Client feignClient() { + try { + SSLContext ctx = SSLContext.getInstance("SSL"); + X509TrustManager tm = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain,String authType) throws CertificateException { + } + + @Override + public void checkServerTrusted(X509Certificate[] chain,String authType) throws CertificateException { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + }; + ctx.init(null, new TrustManager[]{tm}, null); + return new Client.Default(ctx.getSocketFactory(), (hostname, session) -> true); + } catch (Exception e) { + return null; + } + } +} \ No newline at end of file diff --git a/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/feign/IgnoreSSLFeignClientConfig.java b/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/feign/IgnoreSSLFeignClientConfig.java new file mode 100644 index 0000000..17f12c1 --- /dev/null +++ b/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/feign/IgnoreSSLFeignClientConfig.java @@ -0,0 +1,15 @@ +package cn.skcks.docking.gb28181.media.feign; + +import feign.Client; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.context.annotation.Bean; + +public class IgnoreSSLFeignClientConfig { + + // 加载自定义Client + @Bean + @ConditionalOnBean(IgnoreHttpsSSLClient.class) + public Client generateClient(IgnoreHttpsSSLClient ignoreHttpsSSLClient) { + return ignoreHttpsSSLClient.feignClient(); + } +} \ No newline at end of file diff --git a/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/proxy/ZlmMediaHttpClient.java b/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/proxy/ZlmMediaHttpClient.java index 4ee1e44..0ff458d 100644 --- a/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/proxy/ZlmMediaHttpClient.java +++ b/zlmediakit-service/src/main/java/cn/skcks/docking/gb28181/media/proxy/ZlmMediaHttpClient.java @@ -8,6 +8,7 @@ import cn.skcks.docking.gb28181.media.dto.response.ZlmResponse; import cn.skcks.docking.gb28181.media.dto.rtp.*; import cn.skcks.docking.gb28181.media.dto.snap.Snap; import cn.skcks.docking.gb28181.media.dto.version.VersionResp; +import cn.skcks.docking.gb28181.media.feign.IgnoreSSLFeignClientConfig; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -17,7 +18,7 @@ import org.springframework.web.bind.annotation.RequestParam; import java.util.List; -@FeignClient(name="zlmMediaServerProxy", url = "${media.url}") +@FeignClient(name="zlmMediaServerProxy", url = "${media.url}", configuration = IgnoreSSLFeignClientConfig.class) public interface ZlmMediaHttpClient { @GetMapping("/index/api/getApiList") ZlmResponse> getApiList(@RequestParam String secret);