开发者社区> 问答> 正文

https请求验证问题

KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            FileInputStream instream = new FileInputStream(new File(KEYSTORE_FILE_PATH));
    //
    //          try {
        //  trustStore.load(instream, KEYSTORE_PASSWORD.toCharArray());
    //          } finally {
    //              instream.close();
    //          }

            // Trust own CA and all self-signed certs
            //SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
     //         SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
            // Allow TLSv1 protocol only
     //         SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
    //                          new String[] { "TLSv1" }, null,
    //                          SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    //          httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    //之前用过证书,后来证书有问题就没有用了,用的是任意url都信任的方式,如下:


     SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                 public boolean isTrusted(X509Certificate[] chain,
                                 String authType) throws CertificateException {
                     return true;
                 }
             }).build();

             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
             CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
                        HttpPost httpPost = new HttpPost("https://localhost:8080/login");
                        httpClient.excute(httpPost);

报错如下:javax.net.ssl.SSLException: hostname in certificate didn't match: !=

展开
收起
蛮大人123 2016-03-26 14:19:21 3569 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    参考示例:

    /**
         * 发送HTTPS    POST请求
         * 
         * @param 要访问的HTTPS地址,POST访问的参数Map对象
         * @return  返回响应值
         * */
        public static final String sendHttpsRequestByPost(String url, Map<String, String> params) {
            String responseContent = null;
            HttpClient httpClient = new DefaultHttpClient();
            //创建TrustManager
            X509TrustManager xtm = new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            //这个好像是HOST验证
            X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() {
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
                public void verify(String arg0, SSLSocket arg1) throws IOException {}
                public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException {}
                public void verify(String arg0, X509Certificate arg1) throws SSLException {}
            };
            try {
                //TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext
                SSLContext ctx = SSLContext.getInstance("TLS");
                //使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
                ctx.init(null, new TrustManager[] { xtm }, null);
                //创建SSLSocketFactory
                SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
                socketFactory.setHostnameVerifier(hostnameVerifier);
                //通过SchemeRegistry将SSLSocketFactory注册到我们的HttpClient上
                httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", socketFactory, 443));
                HttpPost httpPost = new HttpPost(url);
                List<NameValuePair> formParams = new ArrayList<NameValuePair>(); // 构建POST请求的表单参数
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
                httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity(); // 获取响应实体
                if (entity != null) {
                    responseContent = EntityUtils.toString(entity, "UTF-8");
                }
            } catch (KeyManagementException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭连接,释放资源
                httpClient.getConnectionManager().shutdown();
            }
            return responseContent;
        }
    2019-07-17 19:16:09
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里巴巴HTTP 2.0实践及无线通信协议的演进之路 立即下载
CDN助力企业网站进入HTTPS时代 立即下载
低代码开发师(初级)实战教程 立即下载