title: Validate SOAP response xml timestamp and signature X509 spring-ws-security
body: <p>I am new to spring-ws-security and have read almost all articles on google and stacktrace but could not get this working.<br/>
  I have requirement to validate response XML signature, timestamp and then retrieve data. When I am skipping validation, then no problems but moment I add validation code, I receive error :</p>
  
  <p>WARN :  Could not validate request: The signature or decryption was invalid; nested exception is org.apache.ws.security.WSSecurityException: The signature or decryption was invalid</p>
  
  <pre><code>@Configuration
  public class SoapClientConfig {
  
  final String generatedResource = "packageName";
  
  @Bean
  public KeyStoreCallbackHandler securityCallbackHandler() {
      KeyStoreCallbackHandler callbackHandler = new KeyStoreCallbackHandler();
      callbackHandler.setPrivateKeyPassword("serverkeystorepassword");
      return callbackHandler;
  }
  
  @Bean
  public Wss4jSecurityInterceptor securityInterceptor() throws Exception {
      Wss4jSecurityInterceptor securityInterceptor = new Wss4jSecurityInterceptor();
  
      // set security actions
      securityInterceptor.setSecurementActions("Timestamp Signature");
      securityInterceptor.setSecurementUsername("clientkeystoreusername");
      securityInterceptor.setSecurementPassword("clientkeystorepassword");
  
      //sign both body and timestamp - default body will be signed
      securityInterceptor.setSecurementSignatureParts("{}{http://schemas.xmlsoap.org/soap/envelope/}Body;{}{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Timestamp");
  
      //This will generate binarySecurityToken in header
      securityInterceptor.setSecurementSignatureKeyIdentifier("DirectReference");
      securityInterceptor.setSecurementSignatureCrypto(getRequestCryptoBean().getObject());
  
      //This is validation code, which is not validating response.
      securityInterceptor.setValidationActions("Timestamp Signature");
      securityInterceptor.setValidationSignatureCrypto(getResponseCryptoBean().getObject());
      securityInterceptor.setValidationCallbackHandler(securityCallbackHandler());
  
      return securityInterceptor;
  }
  
  @Bean
  public CryptoFactoryBean getRequestCryptoBean() throws IOException, URISyntaxException {
  
      CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();
      cryptoFactoryBean.setKeyStorePassword("clientkeystorepassword");
      cryptoFactoryBean.setKeyStoreLocation("client.jks");
      return cryptoFactoryBean;
  }
  
  @Bean
  public CryptoFactoryBean getResponseCryptoBean() throws Exception {
  
      CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();
      cryptoFactoryBean.setDefaultX509Alias("1");
      cryptoFactoryBean.setKeyStorePassword("serverkeystorepassword");
      cryptoFactoryBean.setKeyStoreLocation("server.jks");
      cryptoFactoryBean.afterPropertiesSet();
      return cryptoFactoryBean;
  }
  
  @Bean
  public Jaxb2Marshaller getMarshaller() {
      Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
      marshaller.setContextPath(generatedResource);
      return marshaller;
  }
  
  @Bean
  public Card getAvailableCardsClient() throws Exception {
      Card memberCard = new Card();
      memberCard.setMarshaller(getMarshaller());
      memberCard.setUnmarshaller(getMarshaller());
  
      //Set timeout for soap service
      HttpComponentsMessageSender sender = new HttpComponentsMessageSender();
      sender.setConnectionTimeout(2000);
      sender.setReadTimeout(2000);
      memberCard.setMessageSender(sender);
      //end timeout
  
      memberCard.setDefaultUri("url");
  
      //add interceptor for adding and validating signature
      ClientInterceptor[] interceptors = new ClientInterceptor[]{securityInterceptor()};
      memberCard.setInterceptors(interceptors);
  
      return memberCard;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>** server.jks contains public key of server. And also this authentication is X509 certificates.
  Please help me figure out, how to validate response.</p>