我的客户端通过HTTP成功获得服务器响应。
SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();
SOAPMessage soapMessageResponse = connection.call(soapRequest, new URL(serviceLocation));
我想在客户端/服务器之间进行SSL通信。
在另一个项目中,我从SSLSocketFactory
和KeyStore
成功为 SSL 握手创建了TrustManagerFactory
。
如何在webservice客户端中使用SSLSocketFactory
代码使客户端 SSL 通信成功调用服务器。
答案 0 :(得分:1)
我很确定它会使用默认的SSLContext。您可以使用SSLContext.setDefault()更改它。
SSLContext c = SSLContext.getInstance("SSL");
TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
tmf.init(yourKeystore);
TrustManager tm = tmf.getTrustManagers()[0];
tm.
c.init(null, tm, null);
以上是字符串参数的other values。
如果你需要更完整的控制,你可以实现自己的SSLContext子类,它返回你自己的SSLSocketFactory实现,并将SSLContext设置为默认值:
public class MySSLContext extends SSLContext {
private SSLContext wrapped;
private SSLSocketFactory mySocketFactory;
public MySSLContext(SSLContext toWrap, SSLSocketFactory mySocketFactory) {
wrapped = toWrap;
this.mySocketFactory = mySocketFactory;
}
public SSLSocketFactory getSocketFactory() {
return mySocketFactory;
}
public SSLSessionContext getClientSessionContext() {
return wrapped;
}
// other delegates
}
答案 1 :(得分:0)
这行代码在SSL的情况下不起作用。
SOAPMessage soapMessageResponse = connection.call(soapRequest, new URL(serviceLocation));
从here创建信任管理员和密钥管理员。
为了通过来自axis2 webservice的SSL获得响应,您需要打开类似于给定here的流
答案 2 :(得分:0)
如果您添加此代码,您的Web服务类ı认为您的问题将得到解决。
`
//just put it your somewhere
public static class miTM implements javax.net.ssl.TrustManager,
javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
}
// CAll This function in your webservice class .
private static void trustAllHttpsCertificates() throws Exception {
// Create a trust manager that does not validate certificate chains:
javax.net.ssl.TrustManager[] trustAllCerts =
new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc =
javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(
sc.getSocketFactory());
}