java Soap SSL 握手

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10848924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:49:57  来源:igfitidea点击:

Soap SSL handshake

javasecuritysoapssl

提问by Muhammad Imran Tariq

My client is successfully getting response from server through HTTP.

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

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

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

I want SSL communication between client/server.

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

In another project I am successfully creating SSLSocketFactoryfrom a KeyStoreand TrustManagerFactoryfor SSLhandshake.

在另一个项目中,我成功创建SSLSocketFactory从一个KeyStoreTrustManagerFactory用于SSL握手。

How can I use SSLSocketFactorycode in webservice client to make client SSLcommunication successful to call server.

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

采纳答案by Muhammad Imran Tariq

This line of code will not work in case of SSL.

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

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

Create trustmanager and keymanagers from here.

这里创建 trustmanager 和 keymanager 。

In order to get response through SSL from axis2 webservice you need to open streams like given here

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

回答by John Watts

I'm pretty sure it will use the default SSLContext. You can change that with SSLContext.setDefault().

我很确定它会使用默认的 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);

Here are some other valuesfor the string parameters above.

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

If you need more complete control, you can implement your own subclass of SSLContext which returns your own implementation of SSLSocketFactory and set that SSLContext as the default:

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

}

回答by Rusty Umt

Hi if you add this code your webservice class ? think your problem will be solve .

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

     `

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

    }