如何在Spring中设置信任管理器以通过https连接到Web服务

时间:2013-07-01 10:56:30

标签: java spring ssl

我真的被困住了!我试过在互联网上说的一切,但没有运气。有一个配置了SSL的Web服务,现在我们想通过https向该服务器发送SOAP消息。这是我为Spring创建的消息发送者:

public class KeyStoreAwareMessageSender extends HttpsUrlConnectionMessageSender {
public MyKeyStoreAwareMessageSender() {

    try {
        KeyStore ks = KeyStore.getInstance("JKS");
        InputStream is = new FileInputStream("D:\\Dev\\keystore\\my-keystore.jks");
        ks.load(is, "mypass1".toCharArray());
        setSslProtocol("TLSv1");
        SSLContext c = SSLContext.getInstance("TLSv1");
        CertificateFactory cf = CertificateFactory.getInstance("X.509");

        TrustManagerFactory tmf =
                TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ks);
        super.setTrustManagers(tmf.getTrustManagers());
        //X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0];

        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, "cbis1".toCharArray());

        super.setKeyManagers(kmf.getKeyManagers());
        c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
        SSLContext.setDefault(c);

    } catch (Exception ex) { 
        // TODO Auto-generated catch block
        ex.printStackTrace();
    }


}




}

这根本不起作用,我不断收到错误说:

{

main, SEND TLSv1 ALERT:  warning, description = close_notify
Padded plaintext before ENCRYPTION:  len = 32
0000: 01 00 1D 98 EC 71 99 52   6C 48 42 70 08 9E E1 8C  .....q.RlHBp....
0010: 4E A4 22 98 73 63 09 09   09 09 09 09 09 09 09 09  N.".sc..........
main, WRITE: TLSv1 Alert, length = 32
[Raw write]: length = 37
0000: 15 03 01 00 20 6A 5C D0   F6 5B DE 70 BF 9B DE 0C  .... j\..[.p....
0010: CE 66 80 79 AB C0 D5 D0   6F C9 02 40 0B 75 C4 AB  .f.y....o..@.u..
0020: 6F B2 6B E6 6E                                     o.k.n
main, called closeSocket(selfInitiated)
20:45:52.686 [main] DEBUG o.s.w.client.MessageTracing.received - Received response [SaajSoapMessage {http://schemas.xmlsoap.org/soap/envelope/}Fault] for request [SaajSoapMessage {http://www.aegon.co.uk/namespaces/TrustedThirdParty}NewEntrantInvestorRequest]
20:45:52.686 [main] DEBUG o.s.w.client.core.WebServiceTemplate - Received Fault message for request [SaajSoapMessage {http://www.aegon.co.uk/namespaces/TrustedThirdParty}NewEntrantInvestorRequest]
Exception in thread "main" org.springframework.ws.soap.client.SoapFaultClientException: Could not access envelope: Unable to create envelope from given source: ; nested exception is com.sun.xml.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source: 
    at org.springframework.ws.soap.client.core.SoapFaultMessageResolver.resolveFault(SoapFaultMessageResolver.java:37)
    at org.springframework.ws.client.core.WebServiceTemplate.handleFault(WebServiceTemplate.java:774)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:600)
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:537)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:384)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:378)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:370)

}

请帮助我!

0 个答案:

没有答案