ning async http客户端如何接受任何证书

时间:2012-11-09 15:01:23

标签: ssl netty ning asynchttpclient

我在此页面上看到如何执行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;

感谢, 迪安

1 个答案:

答案 0 :(得分:1)

晚会结束了5年,但今天我遇到了同样的问题,你的问题在谷歌搜索中得到了很高的评价。所以也许我的回答会帮助其他人。

使用创建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;
    }

以上示例适用于Async HTTP Client 1.9.40& Java 1.8