• RestTemplate调用https接口跳过证书验证


    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSession;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    
    import java.nio.charset.StandardCharsets;
    import java.security.cert.CertificateException;
    
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
    import org.springframework.http.converter.StringHttpMessageConverter;
    import org.springframework.web.client.RestTemplate;
    
    public class NoHttpsClientUtils {
    
        /**
         * 跳过证书效验的sslcontext
         * 
         * @return
         * @throws Exception
         */
        private static SSLContext createIgnoreVerifySSL() throws Exception {
            SSLContext sc = SSLContext.getInstance("TLS");
    
            // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
            X509TrustManager trustManager = new X509TrustManager() {
                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                        String paramString) throws CertificateException {
                }
    
                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                        String paramString) throws CertificateException {
                }
    
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            sc.init(null, new TrustManager[] { trustManager }, null);
            return sc;
        }
    
        /**
         * 构造RestTemplate
         * 
         * @return
         * @throws Exception
         */
        public static RestTemplate getRestTemplate() throws Exception {
            HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
            // 超时
            factory.setConnectionRequestTimeout(5000);
            factory.setConnectTimeout(5000);
            factory.setReadTimeout(5000);
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createIgnoreVerifySSL(),
                    // 指定TLS版本
                    null,
                    // 指定算法
                    null,
                    // 取消域名验证
                    new HostnameVerifier() {
                        @Override
                        public boolean verify(String string, SSLSession ssls) {
                            return true;
                        }
                    });
            CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            factory.setHttpClient(httpClient);
            RestTemplate restTemplate = new RestTemplate(factory);
            // 解决中文乱码问题
            restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
            return restTemplate;
        }
    }
  • 相关阅读:
    spring框架面试
    为什么人们都讨厌开会?
    使用IDEA连接远程主机上的Redis失败. 抛出redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host xxx
    192405实验六 Metasploit攻击渗透实践
    实验七 网络欺诈与防范
    gradle implementation、compileOnly、api
    C# 数组对比和字符串分割后转成指定类型的数组
    sql 2012 的 问题 无法 打开的原因。
    PhpStudyV8新版本_Win安装PHP扩展
    算法相关
  • 原文地址:https://www.cnblogs.com/liangblog/p/15005997.html
Copyright © 2020-2023  润新知