首页  编辑  

SpringBoot RestTemplate忽略SSL证书验证

Tags: /Java/   Date Created:
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的方法:
  1. @Configuration
  2. public class RestTemplateConfig{
  3.  
  4.  @Bean
  5.  public RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
  6.       TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
  7.  
  8.       SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
  9.                       .loadTrustMaterial(null, acceptingTrustStrategy)
  10.                       .build();
  11.  
  12.       SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
  13.  
  14.       CloseableHttpClient httpClient = HttpClients.custom()
  15.                       .setSSLSocketFactory(csf)
  16.                       .build();
  17.  
  18.       HttpComponentsClientHttpRequestFactory requestFactory =
  19.                       new HttpComponentsClientHttpRequestFactory();
  20.  
  21.       requestFactory.setHttpClient(httpClient);
  22.       RestTemplate restTemplate = new RestTemplate(requestFactory);
  23.      return restTemplate;
  24.   }
  25. }

HttpClient5的方法,需要配合Spring-Web 6.x 使用:
  1.         <dependency>
  2.             <groupId>org.springframework</groupId>
  3.             <artifactId>spring-web</artifactId>
  4.             <version>6.0.6</version>
  5.         </dependency>
  6.         <dependency>
  7.             <groupId>org.apache.httpcomponents.client5</groupId>
  8.             <artifactId>httpclient5</artifactId>
  9.             <version>5.2.1</version>
  10.         </dependency>
代码:
  1. import org.apache.hc.client5.http.classic.HttpClient;
  2. import org.apache.hc.client5.http.impl.classic.HttpClients;
  3. import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
  4. import org.apache.hc.client5.http.io.HttpClientConnectionManager;
  5. import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
  6. import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
  7. import org.apache.hc.client5.http.ssl.TrustAllStrategy;
  8. import org.apache.hc.core5.ssl.SSLContexts;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.http.client.ClientHttpRequestFactory;
  12. import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  13. import org.springframework.web.client.RestTemplate;
  14. import javax.net.ssl.SSLContext;
  15. import java.security.KeyManagementException;
  16. import java.security.KeyStoreException;
  17. import java.security.NoSuchAlgorithmException;
  18. @Configuration
  19. public class RestTemplateConfig {
  20.     @Bean
  21.     public RestTemplate restTemplate() throws NoSuchAlgorithmExceptionKeyStoreExceptionKeyManagementException {
  22.         final SSLContext sslcontext = SSLContexts.custom()
  23.                 .loadTrustMaterial(null, new TrustAllStrategy())
  24.                 .build();
  25.         final SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create()
  26.                 .setSslContext(sslcontext)
  27.                 .build();
  28.         final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
  29.                 .setSSLSocketFactory(sslSocketFactory)
  30.                 .build();
  31.         HttpClient httpClient = HttpClients.custom()
  32.                 .setConnectionManager(cm)
  33.                 .evictExpiredConnections()
  34.                 .build();
  35.         ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
  36.         RestTemplate restTemplate = new RestTemplate(requestFactory);
  37.         return restTemplate;
  38.     }
  39. }