【问题标题】:ning asynch http client how to accept any certificates宁异步http客户端如何接受任何证书
【发布时间】:2012-10-29 21:54:32
【问题描述】:

我在这个页面上看到了如何做 https

http://sonatype.github.com/async-http-client/ssl.html

但是如果我只是想忽略并接受任何证书,因为在这个环境中,我现在不关心中间人,因为它处于一个孤立的环境中,我只是在为 QA 的自动化测试做一些事情数据。

也许我的问题是如何在 java 的 SSL 堆栈中伪造 SSL,以便它接受另一端的任何证书(这不是双向的,因为它是 https)。

上面链接中客户端的常用代码是

    char[] keyStorePassword = "changeit".toCharArray();
    KeyStore ks = KeyStore.getInstance("JKS");
    //ks.load(keyStoreStream, keyStorePassword);

    char[] certificatePassword = "changeit".toCharArray();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(ks, certificatePassword);

    KeyManager[] keyManagers = kmf.getKeyManagers();
    javax.net.ssl.TrustManager tm = new MyTrustMgr();
    javax.net.ssl.TrustManager[] trustManagers = new javax.net.ssl.TrustManager[]{tm };
    SecureRandom secureRandom = new SecureRandom();

    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(keyManagers, trustManagers, secureRandom);
    return ctx;

好的,解决这个问题,我发现由于某种原因仍然无法正常工作

    X509TrustManager tm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] xcs,
                String string) throws CertificateException {
        }
        public void checkServerTrusted(X509Certificate[] xcs,
                String string) throws CertificateException {
        }
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, new TrustManager[] { tm }, null);
    return ctx;

谢谢, 院长

【问题讨论】:

    标签: ssl netty ning asynchttpclient


    【解决方案1】:

    晚了 5 年,但我今天遇到了同样的问题,你的问题在 Google 搜索中的排名很高。所以也许我的回答会帮助别人。

    使用您创建 SSLContext 的代码,此代码将创建一个 AsyncHttpClient,它将忽略(或盲目接受)所有 SSL 证书:

        AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder()
                .setSSLContext(createSslContext())
                .build();
    
        httpClient = new AsyncHttpClient(config);
    

    如上所述,createSslContext 方法是您在答案中的代码的精确复制和粘贴:

        private SSLContext createSslContext() throws Exception {
            X509TrustManager tm = new X509TrustManager() {
    
                public void checkClientTrusted(X509Certificate[] xcs,
                                           String string) throws CertificateException {
                }
    
                public void checkServerTrusted(X509Certificate[] xcs,
                                           String string) throws CertificateException {
                }
    
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
    
            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(null, new TrustManager[] { tm }, null);
            return ctx;
        }
    

    以上示例适用于异步 HTTP 客户端 1.9.40 和 Java 1.8

    【讨论】:

      猜你喜欢
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 2016-08-19
      • 1970-01-01
      • 2013-09-03
      • 1970-01-01
      • 2012-03-19
      相关资源
      最近更新 更多