restTemplate访问https

1.引入依赖

        
            org.apache.httpcomponents
            httpclient
            4.5.3
        

2.配置

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;

        SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
                .loadTrustMaterial(null, acceptingTrustStrategy)
                .build();

        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext,
                new String[] { "TLSv1" },
                null,
                NoopHostnameVerifier.INSTANCE);

        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(csf)
                .build();

        HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory();

        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }
}

 

参考资料:https://www.jianshu.com/p/fd86b5b74301  restTemplate访问https

https://blog.csdn.net/RL_LEEE/article/details/87920903  httpClient,Certificate for ip/域名 doesn't match any of the subject altinative names: []问题处理

 

你可能感兴趣的:(springboot)