2012-06-01 178 views
4

我的客戶端通過HTTP成功地從服務器獲取響應。Soap SSL握手

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

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

我想在客戶端/服務器之間進行SSL通信。

在另一個項目中,我成功地從一個KeyStoreTrustManagerFactory創造SSLSocketFactorySSL握手

如何在webservice客戶端中使用SSLSocketFactory代碼使客戶端SSL通信成功地調用服務器。

回答

0

這行代碼不會在個案工作的SSL。

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

here創建trustmanager和keymanagers。

爲了打通從你需要打開等給出here

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上面的字符串參數。

如果您需要更完整的控制,可以實現你自己的SSL連接的子類,返回自己實現的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 

} 
+0

順便說一句,我討厭這樣的API,只允許每個虛擬機一次有一個活動的實例。在API中,唯一可以接受的靜態方法是工廠方法和常量。 –

+0

你說過:「很確定它會使用默認的SSLContext」。我可以在哪裏使用SSLContext?你能否簡單介紹一下。 –

+0

我正在這樣做。但是,在SOAP客戶端代碼中如何使用這個SSLContext?正如我討論的那樣,我從SSLContext構建SSLSocketFactory。 –

0

嗨流。如果您添加此代碼Axis2的web服務響應SSL的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()); 

    } 
+0

這意味着webservice信任所有SSL證書而不檢查它們,這在生產服務中可能不是一個好主意。 –