4

我的客户端通过 HTTP 成功地从服务器获得响应。

SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();

SOAPMessage soapMessageResponse = connection.call(soapRequest, new URL(serviceLocation));

我想要客户端/服务器之间的 SSL 通信。

在另一个项目中,我成功地从SSLSocketFactory一个KeyStoreSSL握手创建。TrustManagerFactory

如何SSLSocketFactory在 webservice 客户端中使用代码使客户端SSL通信成功调用服务器。

4

3 回答 3

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);

以下是上述字符串参数的一些其他值。

如果您需要更完整的控制,您可以实现自己的 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

}
于 2012-06-01T11:06:43.290 回答
0

嗨,如果您将此代码添加到您的网络服务类中,我认为您的问题将得到解决。

     `

   //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());

    }
于 2014-01-15T08:04:18.817 回答
-2

这行代码在 SSL 的情况下不起作用。

SOAPMessage soapMessageResponse = connection.call(soapRequest, new URL(serviceLocation));

从这里创建 trustmanager 和 keymanagers 。

为了从axis2 webservice通过SSL获得响应,您需要打开此处给出的流

于 2012-07-20T05:51:19.880 回答