【问题标题】:Ignore invalid certificate with Axis client忽略 Axis 客户端的无效证书
【发布时间】:2013-02-22 04:15:38
【问题描述】:

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

【问题讨论】:

标签: java soap axis


【解决方案1】:

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

    // 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
    }

【讨论】:

  • 这实际上对我不起作用,但是,我确实发布了最终起作用的内容。我理解您的建议,但我不确定为什么它对我不起作用。
  • 很有趣,但是如果您设置默认的 SSL 套接字工厂,它就不起作用,但是如果您改为使用SSLContext.setDefault(sc);,它就起作用了。 Oracle Java 1.6
【解决方案2】:

在尝试了多种解决方案后,最终奏效的是安装自定义协议处理程序并将其与特定的 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 连接,如果颁发了新证书,这可能会破坏事情的发展。如果颁发新证书,这个肯定会中断,但它只是一个连接,而不是所有连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-10
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多