RestTemplate 忽略SSL证书验证,以便支持自签名证书
⚠
警告
忽略证书签名验证不是好的方法,如果要避免警告,简单的方法是,把自签名的根证书导入到JRE/JDK的root证书链(cacerts)当中,可以在JRE/JDK目录下lib/security 目录下运行指令导入自签名证书,这样避免certificate path validation问题:
..\..\bin\keytool.exe -import -alias 自己给个别名 -file 自签名根证书文件.cer -keystore cacerts --storepass changeit
HttpClient 4.x的方法:
- @Configuration
- public class RestTemplateConfig{
-
- @Bean
- public RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
- TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
-
- SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
- .loadTrustMaterial(null, acceptingTrustStrategy)
- .build();
-
- SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
-
- CloseableHttpClient httpClient = HttpClients.custom()
- .setSSLSocketFactory(csf)
- .build();
-
- HttpComponentsClientHttpRequestFactory requestFactory =
- new HttpComponentsClientHttpRequestFactory();
-
- requestFactory.setHttpClient(httpClient);
- RestTemplate restTemplate = new RestTemplate(requestFactory);
- return restTemplate;
- }
- }
HttpClient5的方法,需要配合Spring-Web 6.x 使用:
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>6.0.6</version>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents.client5</groupId>
- <artifactId>httpclient5</artifactId>
- <version>5.2.1</version>
- </dependency>
代码:
- import org.apache.hc.client5.http.classic.HttpClient;
- import org.apache.hc.client5.http.impl.classic.HttpClients;
- import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
- import org.apache.hc.client5.http.io.HttpClientConnectionManager;
- import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
- import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
- import org.apache.hc.client5.http.ssl.TrustAllStrategy;
- import org.apache.hc.core5.ssl.SSLContexts;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.client.ClientHttpRequestFactory;
- import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
- import org.springframework.web.client.RestTemplate;
- import javax.net.ssl.SSLContext;
- import java.security.KeyManagementException;
- import java.security.KeyStoreException;
- import java.security.NoSuchAlgorithmException;
- @Configuration
- public class RestTemplateConfig {
- @Bean
- public RestTemplate restTemplate() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
- final SSLContext sslcontext = SSLContexts.custom()
- .loadTrustMaterial(null, new TrustAllStrategy())
- .build();
- final SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create()
- .setSslContext(sslcontext)
- .build();
- final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
- .setSSLSocketFactory(sslSocketFactory)
- .build();
- HttpClient httpClient = HttpClients.custom()
- .setConnectionManager(cm)
- .evictExpiredConnections()
- .build();
- ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
- RestTemplate restTemplate = new RestTemplate(requestFactory);
- return restTemplate;
- }
- }