Remote host closed connection during handshake

悠悠 2022-12-10 05:55 297阅读 0赞
  • https通过TSLv1和TSLv1.2协议通信。

    jdk1.7默认是TSLv1, 可以支持TSLv1.1,TSLv1.2,

    jdk1.8默认是TSLv1.2

    假如服务器端设置是TSLv1.2,而客服端是TSLv1, 访问就会出现Remote host closed connection during handshake的错误.

    **解决办法:**强制通过TLSv1.2或TLSv1通信,前提服务端也采用相应协议。

    ​ SSLContext ctx = SSLContext.getInstance(“TLSv1.2”);

    或者 SSLContext ctx = SSLContext.getInstance(“TLSv1”);

    1. public class SSLClient extends DefaultHttpClient {
    2. public SSLClient() throws Exception {
    3. super();
    4. SSLContext ctx = SSLContext.getInstance("TLSv1.2");
    5. X509TrustManager tm = new X509TrustManager() {
    6. @Override
    7. public void checkClientTrusted(X509Certi<a target=_blank target="_blank" href="http://superuser.com/questions/747377/enable-tls-1-1-and-1-2-for-clients-on-java-7">http://superuser.com/questions/747377/enable-tls-1-1-and-1-2-for-clients-on-java-7</a>ficate[] chain, String authType) throws CertificateException {
    8. }
    9. @Override
    10. public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    11. }
    12. @Override
    13. public X509Certificate[] getAcceptedIssuers() {
    14. return null;
    15. }
    16. };
    17. ctx.init(null, new TrustManager[] { tm }, null);
    18. org.apache.http.conn.ssl.SSLSocketFactory ssf = new org.apache.http.conn.ssl.SSLSocketFactory(ctx,
    19. org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    20. ClientConnectionManager ccm = this.getConnectionManager();
    21. SchemeRegistry sr = ccm.getSchemeRegistry();
    22. sr.register(new Scheme("https", 443, ssf));
    23. }
    24. }

    查看当前jvm支持的通信协议:

    1. public static void main(String[] args) throws Exception {
    2. SSLContext context = SSLContext.getInstance("TLS");
    3. context.init(null, null, null);
    4. SSLSocketFactory factory = (SSLSocketFactory) context.getSocketFactory();
    5. SSLSocket socket = (SSLSocket) factory.createSocket();
    6. String[] protocols = socket.getSupportedProtocols();
    7. System.out.println("Supported Protocols: " + protocols.length);
    8. for (int i = 0; i < protocols.length; i++) {
    9. System.out.println(" " + protocols[i]);
    10. }
    11. protocols = socket.getEnabledProtocols();
    12. System.out.println("Enabled Protocols: " + protocols.length);
    13. for (int i = 0; i < protocols.length; i++) {
    14. System.out.println(" " + protocols[i]);
    15. }
    16. }

    Supported Protocols: 5
    SSLv2Hello
    SSLv3
    TLSv1
    TLSv1.1
    TLSv1.2
    Enabled Protocols: 3
    TLSv1
    TLSv1.1
    TLSv1.2

    Process finished with exit code 0

发表评论

表情:
评论列表 (有 0 条评论,297人围观)

还没有评论,来说两句吧...

相关阅读