2

我正在为由供应商控制的 SOAP Web 服务构建客户端。不幸的是,他们的开发服务器有一个我无法验证的不安全(自签名)证书。每次我尝试发出请求时,Apache Axis 都会失败。有没有办法忽略 SSL 验证错误?我显然不想在生产中这样做,但在我的开发环境中会很好。

4

2 回答 2

2

尝试禁用证书验证,在发出请求调用之前输入以下代码 -

    // Create a trust manager that does not validate certificate chains
    final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        }
    };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception ex) {
        // take action
    }
于 2013-02-22T08:03:27.163 回答
1

在尝试了多种解决方案后,最终奏效的是安装自定义协议处理程序并将其与特定的相关联ServiceClient

private void configureServiceClient(ServiceClient client) {
       SSLContext ctx;
        try {
            KeyStore truststore = KeyStore.getInstance("JKS");
            truststore.load(getClass().getResourceAsStream("/truststore.jks"),
                    "latitude".toCharArray());

            ctx = SSLContext.getInstance("SSL");
            TrustManagerFactory tmf = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(truststore);
            ctx.init(null, tmf.getTrustManagers(), null);
        } catch (Exception e) {
            logger.error("Exception loading Bold trust store", e);
            throw new RuntimeException(e);
        }

        SSLProtocolSocketFactory sslFactory = new SSLProtocolSocketFactory(ctx);
        Protocol prot = new Protocol("https",
                (ProtocolSocketFactory) sslFactory, 443);
        client.getOptions().setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER,
                prot);
}

这样做的好处是它不需要我为我的应用程序覆盖所有 SSL 连接,如果颁发了新证书,这可能会破坏事情的发展。如果颁发新证书,这个肯定会中断,但这只是一个连接,而不是所有连接。

于 2013-04-01T19:59:53.540 回答