• javax.net.ssl.SSLException: Certificate doesn't match any of the subject alternative names


    问题:在使用 org.apache.http.*下的 CloseableHttpClient 发送https请求时报了以上错误

    解决方案一:使用java.net.HttpURLConnection 

    import java.net.HttpURLConnection;
    
    public static HttpURLConnection connectToWeb(String uri) {
        HttpURLConnection connection = null;
        try {
            URL url = new URL(uri);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return connection;
    }

    解决方案二:在创建SSLConnectionSocketFactory时,添加NoopHostnameVerifier.INSTANCE参数

     public static CloseableHttpClient createSSLClientDefault() {
            CloseableHttpClient client = null;
            try {
                SSLContext sslContext = null;
                sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                    @Override
                    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        return true;
                    }
                }).build();
                SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);//这里的红色部分
                client = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
                e.printStackTrace();
            }
            return client;
        }

    原理扩展:

    在org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname方法中有如下代码:

    而NoopHostnameVerifier源代码如下;verify方法直接返回true

    /**
    * The NO_OP HostnameVerifier essentially turns hostname verification
    * off. This implementation is a no-op, and never throws the SSLException.
    * 关闭主机名验证,直接返回true
    * @since 4.4
    */
    @Contract(threading = ThreadingBehavior.IMMUTABLE)
    public class NoopHostnameVerifier implements HostnameVerifier {
    
        public static final NoopHostnameVerifier INSTANCE = new NoopHostnameVerifier();
    
        @Override
        public boolean verify(final String s, final SSLSession sslSession) {
            return true;
        }
    
        @Override
        public final String toString() {
            return "NO_OP";
        }
    
    }
  • 相关阅读:
    用JS + WCF打造轻量级WebPart
    提高WCF服务并发能力的简单处理办法
    利用JQuery实现更简单的Ajax跨域请求
    WCF Testing Tool(转)
    [转贴]一个有趣的布局
    [转贴].net中上传视频并将各种视频文件转换成.flv格式
    IE5,IE6,IE7,IE8的css兼容性列表[转自MSDN]
    [转贴]Castle 开发系列文章
    ie6,ie7,ff 的css兼容hack写法
    ExtJs学习笔记(23)ScriptTagProxy+XTemplate+WCF跨域取数据
  • 原文地址:https://www.cnblogs.com/liaojie970/p/9361320.html
Copyright © 2020-2023  润新知