title: How to set trust manager in Spring to connect to webservice over https
body: <p>I'm really stuck! I tried everything said in internet but no luck. There is a webservice configured with SSL and now we want to send a SOAP message to that server over https. Here is the message sender I've created for Spring:</p>
  
  <pre><code>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();
      }
  
  
  }
  
  
  
  
  }
  </code></pre>
  
  <p>This doesn't work at all and I keep getting an error says:</p>
  
  <pre><code>{
  
  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)
  </code></pre>
  
  <p>}</p>
  
  <p>Please help me on this!</p>