1

我的项目使用 xfire 作为 Web 服务客户端 api。我的项目在旧版 Servlet/JSP 中。我们使用 XFire eclipse 插件来生成客户端存根。

Web 服务已迁移到 SLL (HTTPS)。有没有简单的方法在 XFire 中通过 SSL 使用 Web 服务。

我在http://docs.codehaus.org/display/XFIRE/HTTP+Transport找到了一些代码。我也有一些困惑。它激发了使用not-so-common-sslAlpha 版本的动机,我不知道它是否足够稳定以用于生产。

// Technique similar to http://juliusdavies.ca/commons-    ssl/TrustExample.java.html
HttpSecureProtocol protocolSocketFactory = new HttpSecureProtocol();

// "/thecertificate.cer" can be PEM or DER (raw ASN.1).  Can even be several PEM certificates in one file.
TrustMaterial trustMaterial = new TrustMaterial(getClass().getResource("/thecertificate.cer"));

// We can use setTrustMaterial() instead of addTrustMaterial() if we want to remove
// HttpSecureProtocol's default trust of TrustMaterial.CACERTS.
protocolSocketFactory.addTrustMaterial(trustMaterial);

// Maybe we want to turn off CN validation (not recommended!):
protocolSocketFactory.setCheckHostname(false);

Protocol protocol = new Protocol("https", (ProtocolSocketFactory) protocolSocketFactory, 8443);
Protocol.registerProtocol("https", protocol);

现在上面是一种创建协议工厂并将其注册到 Apache HTTPclient api 的方法。但是 id 没有说明如何进一步处理生成的存根。

如果有的话,请随时询问更多信息。

我们不能转移到其他 Web 服务客户端 api,所以这不是一个选择。

4

1 回答 1

0

设法解决了我自己的问题。我就是这样做的。XFire 在内部使用 Apache Http 客户端,因此在此 Api 上设置安全证书详细信息就可以完成这项工作。为此,我们将使用 no-yet-common-ssl.jar。

首先,我们将org.apache.commons.ssl.TrustMaterial使用 commons 创建,然后在 HttpSecureProtocol 中设置它,它是javax.net.ssl.SSLSocketFactory.

假设XYZ.cer是服务提供商提供的客户端证书。

HttpSecureProtocol protocolSocketFactory = new HttpSecureProtocol();
protocolSocketFactory.addTrustMaterial(TrustMaterial.DEFAULT); //for trusting all the certifects in java trusted Store. 
protocolSocketFactory.addTrustMaterial(new TrustMaterial(getClass().getResource("/XYZ.cer")));
Protocol protocol = new Protocol("https", (ProtocolSocketFactory)protocolSocketFactory, 443);
Protocol.registerProtocol("https", protocol);

如果这是一个 Web 应用程序,您可以在 ServletContextListener 或应用程序启动时执行的任何代码部分中执行此操作。

现在您可以使用 Xfire 客户端存根使用任何 ssl 服务。任何实现上述证书的服务。

现在为什么这个工作。因为 XFire 使用 Apache Http Client 作为连接 api,我们告诉 Http 客户端在使用 HTTPS 时使用上面的 TrustManager。

于 2015-05-13T13:01:43.873 回答