使用Axis客户端忽略无效证书

时间:2013-02-22 04:15:39

标签: java soap axis

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

2 个答案:

答案 0 :(得分: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
    }

答案 1 :(得分: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连接,如果发出新证书,这可能会破坏未来的事情。如果颁发新证书,这肯定会破坏,但它只是一个连接而不是全部连接。