【问题标题】:Consume Web service over ssl using XFire使用 XFire 通过 ssl 使用 Web 服务
【发布时间】:2015-04-30 09:25:28
【问题描述】:

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

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

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

// 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,因此这不是一个选项。

【问题讨论】:

    标签: java ssl https webservice-client xfire


    【解决方案1】:

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

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

    假设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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 2010-10-03
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 2012-10-03
      相关资源
      最近更新 更多