JDK1.7不支持Https TLS1.2协议

1前言

     jdk1.7 支持协议 :SSLv2Hello 、SSLv3、TLSv1、TLSv1.1、TLSv1.2五种协议,但是默认协议是:TLSv1,因为https 请求是双向认证的也就是jdk的支持协议最低是TLSv1以上的版本,所有jdk1.7不能访问https请求。

2解决思路

  新建类 TrustAnyHostnameVerifier  implements HostnameVerifier,TrustAnyTrustManager implements X509TrustManager。

 关键代码如下:

  HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
  if (con instanceof HttpsURLConnection) {
SSLContext sc = SSLContext.getInstance("TLSv1.2");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
((HttpsURLConnection) con).setSSLSocketFactory(sc.getSocketFactory());
((HttpsURLConnection) con).setHostnameVerifier(new TrustAnyHostnameVerifier());

  }

3完整代码如下: 

public class TrustAnyHostnameVerifier implements HostnameVerifier {

    public boolean verify(String hostname, SSLSession session) {
        // 直接Pass
        return true;
    }

 }

public class TrustAnyTrustManager implements X509TrustManager {
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }
    @Override
    public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateExcep      tion {
    }
    @Override
    public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateException {
    }
    @Override
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return new java.security.cert.X509Certificate[0];
    }

}

ReuqestHead和RequestBody封装:

 public static Map getToken() throws Exception {
        Map requestTokenInform = Maps.newHashMap();
        SslUtils.ignoreSsl();
        URL url = new URL("XXXXX");
        String wasUserName = "XXXX";
        String wasPassword = "XXXXX";
        String authorization = "Basic " + new String(Base64.encodeBase64(new String(wasUserName + ":" + wasPassword).getBytes()));
        // 创建http链接对象
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        if (con instanceof HttpsURLConnection) {
            SSLContext sc = SSLContext.getInstance("TLSv1.2");
            sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
            ((HttpsURLConnection) con).setSSLSocketFactory(sc.getSocketFactory());
            ((HttpsURLConnection) con).setHostnameVerifier(new TrustAnyHostnameVerifier());
        }
        con.setRequestProperty("Authorization", authorization);
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置请求方式
        con.setRequestMethod("POST");
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setUseCaches(false);
        String payload = "XXXXX";
        OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
        osw.write(payload);
        osw.flush();
        osw.close();
        //获取请求状态 code 和 Message
        int requestStatus = con.getResponseCode();
        String requestMessage =con.getResponseMessage();
        requestTokenInform.put("requestStatus",requestStatus);
        requestTokenInform.put("requestMessage",requestMessage);
        // 获取请求返回内容并设置编码为UTF-8
        BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
        // 将返回数据拼接为字符串
        StringBuffer sb = new StringBuffer();
        // 临时字符串
        String temp = null;
        // 获取数据
        while ((temp = reader.readLine()) != null) {
            sb.append(temp);
        }
        // 关闭流
        reader.close();
        requestTokenInform.put("tokenData",sb.toString());
        return requestTokenInform;
    }




你可能感兴趣的:(JDK1.7不支持Https TLS1.2协议)