我的项目使用xfire作为web服务客户端api。我的项目在遗留的Servlet/JSP中。我们使用XFire eclipse插件来生成客户端存根。
Web服务已经迁移到SLL (HTTPS)。有什么简单的方法可以在XFire中通过SSL使用Webservice吗?
我在http://docs.codehaus.org/display/XFIRE/HTTP+Transport找到了一些代码。我在那里也有点困惑。它的动机是使用not-so-common-ssl,它在Alpha中,我不知道它是否足够稳定,可以用于生产。
// 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中的方法。但是id没有说明如何进一步处理生成的存根。
如果有的话,请随时询问更多信息。
我们不能转移到其他web服务客户端api,所以这不是一个选项。
发布于 2015-05-13 13:01:43
解决了我自己的问题。我就是这么做的。XFire在内部使用Apache客户端,因此在此Api上设置安全证书详细信息将完成这项工作。为此,我们将使用尚未使用的通用ssl.jar。
首先,我们将使用公域创建org.apache.commons.ssl.TrustMaterial,然后在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作为连接api,所以当使用HTTPS时,我们告诉Http客户机使用上面的TrustManager。
https://stackoverflow.com/questions/29963903
复制相似问题