我发现你们阅读csdn之后居然不去看我有道云笔记,所以我把他们cv过来了!!!
1.绕过安全证书
自己写一个RestTemplate
给这个Bean取一个别名(为了和原有的restTemplate区分开)
@Configuration public class RestTemplateObdConfig { private static boolean ignoreSSL = true; //给Bean取一个别名 @Bean public RestTemplate obdRestTemplate(){ RestTemplate restTemplate = new RestTemplate(); if (ignoreSSL) {//ignoreSSL为true时,绕过证书 HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); factory.setConnectionRequestTimeout(300000); factory.setConnectTimeout(300000); factory.setReadTimeout(300000); // https CloseableHttpClient httpClient = getHttpsClient(); factory.setHttpClient(httpClient); restTemplate = new RestTemplate(factory); } // reInitMessageConverter(restTemplate); return restTemplate; } public static CloseableHttpClient getHttpsClient() { CloseableHttpClient httpClient; if (ignoreSSL) {//ignoreSSL为true时,绕过证书 SSLContext sslContext = null; try { sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return true; } }).build(); } catch (NoSuchAlgorithmException e) { e.getStackTrace(); } catch (KeyManagementException e) { e.getStackTrace(); } catch (KeyStoreException e) { e.getStackTrace(); } httpClient = HttpClients.custom().setSSLContext(sslContext). setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); } else { httpClient = HttpClients.createDefault(); } return httpClient; } }
2在业务层调用的时候指定名字
@Resource(name = “obdRestTemplate”)
private RestTemplate restTemplate;
今天的文章post请求https安全证书问题2.0分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/32165.html