利用RestTemplate发送https请求-忽略ssl证书

打印 上一主题 下一主题

主题 968|帖子 968|积分 2906

RestTemplate调用https服务的时候,由于服务方的ssl证书并非正式证书,不被jdk继承,故会报雷同:“No subject alternative names matching IP address xxxxxx found”的错误。网上找了一下,处置处罚也比力简朴,基本上是配置下:HttpRequestFactory。但找到的内容很多都是需要注入bean,通过@Config等方式配置,这样的话,不是很便利。
以下代码,通过直接调用main函数,处置处罚了RestTemplate,使其忽略SSL证书。
  1. import org.apache.http.conn.ssl.NoopHostnameVerifier;
  2. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  3. import org.apache.http.impl.client.CloseableHttpClient;
  4. import org.apache.http.impl.client.HttpClientBuilder;
  5. import org.apache.http.impl.client.HttpClients;
  6. import org.apache.http.ssl.SSLContexts;
  7. import org.apache.http.ssl.TrustStrategy;
  8. import org.springframework.http.HttpHeaders;
  9. import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  10. import org.springframework.web.client.RestTemplate;
  11. import javax.net.ssl.SSLContext;
  12. import java.security.KeyManagementException;
  13. import java.security.KeyStoreException;
  14. import java.security.NoSuchAlgorithmException;
  15. public class TestNoSSLRest {
  16.     static String URL = "https://192.168.88.70:8443/oauth/oauth2/token?grant_type=authorization_code&code=iDCQAU";
  17.     public static void main(String[] args) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
  18.         RestTemplate restTemplate = new RestTemplate(generateHttpRequestFactory());
  19.         String responseStr = restTemplate.postForObject(URL, initHttpHeaders(), String.class);
  20.         System.out.println(responseStr);
  21.     }
  22.     private static HttpHeaders initHttpHeaders() {
  23.         HttpHeaders headers = new HttpHeaders();
  24.         headers.add("Authorization", "Basic Soiuawe12asildfSEjasdfewe22");
  25.         return headers;
  26.     }
  27.     private static HttpComponentsClientHttpRequestFactory generateHttpRequestFactory() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
  28.         TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;
  29.         SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
  30.         SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
  31.         HttpClientBuilder httpClientBuilder = HttpClients.custom();
  32.         httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
  33.         CloseableHttpClient httpClient = httpClientBuilder.build();
  34.         HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
  35.         factory.setHttpClient(httpClient);
  36.         return factory;
  37.     }
  38. }
复制代码
其中,该class依赖的jar包为:
  1.                 <dependency>
  2.                         <groupId>org.apache.httpcomponents</groupId>
  3.                         <artifactId>httpclient</artifactId>
  4.                         <version>4.5.10</version>
  5.                 </dependency>
  6.                 <dependency>
  7.                         <groupId>org.apache.httpcomponents</groupId>
  8.                         <artifactId>httpcore</artifactId>
  9.                         <version>4.4.15</version>
  10.                 </dependency>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

用多少眼泪才能让你相信

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表