4

我在这个页面上看到如何做 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;

谢谢,院长

4

1 回答 1

6

聚会晚了 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

于 2017-09-22T09:59:07.897 回答