首页 > 其他分享 >openfeign,webClient, restTemplate 忽略 ssl 证书

openfeign,webClient, restTemplate 忽略 ssl 证书

时间:2024-03-13 16:45:33浏览次数:29  
标签:return openfeign RestTemplate restTemplate ssl connection new public

0 springboot 版本


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

1 openfeign


@Configuration
public class FeignIgnoreSSLConfig {
    @Bean
    public Client feignClient() {
        return new Client.Default(getSSLSocketFactory(), new NoopHostnameVerifier());
    }

    private SSLSocketFactory getSSLSocketFactory() {
        try {
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
            return sslContext.getSocketFactory();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

2 webClient


@Configuration
public class WebClientConfig {
    @Bean(name = "webClient")
    public WebClient getWebClient() {
        return WebClient.create();
    }

    @Bean(name = "ignoreSSLWebClient")
    public WebClient getIgnoreSSLWebClient() throws SSLException {
        SslContext sslContext = SslContextBuilder.forClient()
            .trustManager(InsecureTrustManagerFactory.INSTANCE)
            .build();

        HttpClient httpClient = HttpClient.create().secure(contextSpec -> contextSpec.sslContext(sslContext));

        return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
    }
}

3 restTemplate


@Configuration
public class RestTemplateConfig {
    /**
     * RestTemplate注入
     */
    @Bean("restTemplate")
    @LoadBalanced
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
        return restTemplate;
    }

    @Bean("ignoreHttpsRestTemplate")
    public RestTemplate ignoreHttpsRestTemplate() {
        RestTemplate restTemplate = new RestTemplate(new SSLFactory());
        // 支持中文编码
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }


@Slf4j
public class SSLFactory extends SimpleClientHttpRequestFactory {
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
        if (connection instanceof HttpsURLConnection) {
            prepareHttpsConnection((HttpsURLConnection) connection);
        }
        super.prepareConnection(connection, httpMethod);
    }

    private void prepareHttpsConnection(HttpsURLConnection connection) {
        connection.setHostnameVerifier(new SkipHostnameVerifier());
        try {
            connection.setSSLSocketFactory(createSslSocketFactory());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }

    @SneakyThrows
    private SSLSocketFactory createSslSocketFactory() {
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] {new SkipX509TrustManager()}, new SecureRandom());
        return context.getSocketFactory();
    }

    private static class SkipHostnameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }

    }

    private static class SkipX509TrustManager implements X509TrustManager {
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }
    }
}

标签:return,openfeign,RestTemplate,restTemplate,ssl,connection,new,public
From: https://www.cnblogs.com/linzm14/p/18070979

相关文章

  • Practical Learned Lossless JPEG Recompression with Multi-Level Cross-Channel Ent
    目录简介模型DCTCoefficientsRearrangement将系数重排Cross-ColorEntropyModelMatrixContextModelMulti-LevelCross-ChannelEntropyModel创新点实验设置训练数据集:测试数据集:训练细节:结果简介JPEG是一种非常流行的压缩方法,然而最近关于图像压缩的研究主要集中在未压......
  • 别让黑客窥探你的邮件,SSL安全加密,私密从此有保障
    在数字化的世界中,电子邮件已成为人们日常生活和工作中不可或缺的通讯方式。随着互联网的发展,邮件传输安全逐渐成为了大众关注的焦点之一。SSL(SecureSocketsLayer)协议作为一种保护网络通信安全的加密协议,其在邮件传输中的应用显得尤为重要。SSL协议最初是由网景公司(Netscape)研发......
  • httpsok-v1.8.1 一分钟搞定SSL证书自动续期
    ......
  • xsslabs靶场
    level1payload:<scirpt>alert(123);</script>从php代码可以看出level1没有对输入的数据进行防御!level2输入level1的payload可以看到输入到input标签中的value值中,所以用'"用来闭合value,用>来闭合input标签payload:'"><script>alert(123)</script>从php代码可以看到l......
  • Jetty的ssl模块
    启用ssl模块,执行如下命令:java-jar$JETTY_HOME/start.jar--add-modules=ssl命令的输出,如下:INFO:sslinitializedin${jetty.base}/start.d/ssl.iniINFO:Basedirectorywasmodified查看ssl模块的配置文件,执行如下命令:cat$JETTY_BASE/start.d/ssl.i......
  • nginx国密ssl测试
    文章目录文件准备编译部署nginx申请国密数字证书配置证书并测试文件准备下载文件并上传到服务器,这里使用centos7.8本文涉及的程序文件已打包可以直接下载。点击下载下载国密版opensslhttps://www.gmssl.cn/gmssl/index.jsp下载稳定版nginxhttp://nginx.org/en/download.html......
  • spring-restTemplate-网络请求
    1,引言  现如今的IT项目,由服务端向外发起网络请求的场景,基本上处处可见!传统情况下,在服务端代码里访问http服务时,一般会使用JDK的HttpURLConnection或者Apache的HttpClient,不过这种方法使用起来太过繁琐,而且api使用起来非常的复杂,还得操心资源回收。  RestTempl......
  • [转]mkcert 使用指南:如何快速创建自签名 SSL 证书
    原文地址:mkcert使用指南:如何快速创建自签名SSL证书-知乎我们平时在本地开发时,有时会要求通过HTTPS请求来访问。一个通用的做法是用OpenSSL生成自签证书,然后对Web服务进行配置。但OpenSSL的命令比较繁琐,参数也比较复杂,用起来不够方便。于是我找到了一个替代方案:mkce......
  • RestTemplate+OkHttp3整合(一)
    代码(HTTP、HTTPS)一、POM<!-okhttp-><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>3.10.0</version></dependency><!-非必要,这里解析流数据时用了一下->&l......
  • 记一次openfeign反序列化异常复盘
    前言之前业务部门有2个通用响应类,一个是负责和前端交互的响应类AjaxResult,一个是负责和后端RPC接口交互的响应类RpcResult。一开始这两个响应类的值字段都一样,形如下 privateBooleansuccess; privateStringmessage; privateIntegercode; privateTdata;因为前端和......