zoukankan      html  css  js  c++  java
  • SpringBoot用restTemplate调用https接口报 sun.security.validator.ValidatorException: PKIX path 。。。

    修改自:http://blog.joylau.cn/2020/10/19/SpringBoot-RestTemplate-SSL/

    解决办法是写一个@Configuration配置类,里面添加代码:

    import org.apache.http.conn.ssl.NoopHostnameVerifier;
        import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
        import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
        import org.apache.http.impl.client.CloseableHttpClient;
        import org.apache.http.impl.client.HttpClients;
        import org.apache.http.ssl.SSLContextBuilder;
    
    
        @Bean
        public RestTemplate restTemplate(){
            return new RestTemplateBuilder.build();
        }
    
        /**
         * HTTPS RestTemplate
         */
        @Bean
        public RestTemplate httpsRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
            SSLContextBuilder builder = new SSLContextBuilder();
            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
    
            CloseableHttpClient httpClient
                    = HttpClients.custom()
                    .setSSLHostnameVerifier(new NoopHostnameVerifier())
                    .setSSLSocketFactory(sslConnectionSocketFactory)
                    .build();
            HttpComponentsClientHttpRequestFactory requestFactory
                    = new HttpComponentsClientHttpRequestFactory();
            requestFactory.setHttpClient(httpClient);
            requestFactory.setConnectTimeout((int)Duration.ofSeconds(5).toMillis());
            return new RestTemplate(requestFactory);
        }

    然后用的地方由原来的:

    @Resource
    private RestTemplate restTemplate;

    改成:

    @Resource
    private RestTemplate httpsRestTemplate;

    即可;

  • 相关阅读:
    一些链接
    svn
    the source attachment does not contain the source for the file StrutsPrepareAndExecuteFilter.class
    hibernate学习
    hibernate
    mysql front查看所有数据库
    SSD: Single Shot MultiBox Detector 论文解读,附代码
    YOLO算法-论文笔记
    Deep Learning菜鸡篇,我的第一个深度神经网络
    小白成长之路:初识python(六) --python线程池
  • 原文地址:https://www.cnblogs.com/silentdoer/p/14179598.html
Copyright © 2011-2022 走看看