使用 XFire 通过 ssl 使用 Web 服务
Consume Web service over ssl using XFire
我的项目使用 xfire 作为 Web 服务客户端 api。我的项目处于遗留状态 Servlet/JSP。我们使用 XFire eclipse 插件生成客户端存根。
Web 服务已迁移到 SLL (HTTPS)。有什么简单的方法可以在 XFire 中通过 SSL 使用 Web 服务。
我在 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 HTTP 客户端 api 的方法。但是 id 没有说明如何进一步处理生成的存根。
如有更多信息,请随时询问。
我们无法转移到其他 Web 服务客户端 api,因此这不是一个选项。
设法解决了我自己的问题。
我就是这样做的。 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 客户端作为连接 api 并且我们告诉 Http 客户端在使用 HTTPS 时使用上面的 TrustManager。
我的项目使用 xfire 作为 Web 服务客户端 api。我的项目处于遗留状态 Servlet/JSP。我们使用 XFire eclipse 插件生成客户端存根。
Web 服务已迁移到 SLL (HTTPS)。有什么简单的方法可以在 XFire 中通过 SSL 使用 Web 服务。
我在 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 HTTP 客户端 api 的方法。但是 id 没有说明如何进一步处理生成的存根。
如有更多信息,请随时询问。
我们无法转移到其他 Web 服务客户端 api,因此这不是一个选项。
设法解决了我自己的问题。 我就是这样做的。 XFire 在内部使用 Apache Http 客户端,因此在此 Api 上设置安全证书详细信息将完成这项工作。为此,我们将使用 no-yet-common-ssl.jar。
首先,我们将使用 commons 创建 org.apache.commons.ssl.TrustMaterial
,然后将其设置在 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 客户端作为连接 api 并且我们告诉 Http 客户端在使用 HTTPS 时使用上面的 TrustManager。