1   /*
2    * Copyright 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.security.wss4j;
18  
19  import org.apache.ws.security.components.crypto.Crypto;
20  import org.apache.ws.security.components.crypto.Merlin;
21  import org.w3c.dom.Document;
22  
23  import org.springframework.core.io.ClassPathResource;
24  import org.springframework.ws.context.MessageContext;
25  import org.springframework.ws.soap.SoapMessage;
26  import org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean;
27  
28  public abstract class Wss4jMessageInterceptorX509TestCase extends Wss4jTestCase {
29  
30      protected Wss4jSecurityInterceptor interceptor;
31  
32      protected void onSetup() throws Exception {
33          interceptor = new Wss4jSecurityInterceptor();
34          interceptor.setSecurementActions("Signature");
35          interceptor.setValidationActions("Signature");
36          CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();
37          cryptoFactoryBean.setCryptoProvider(Merlin.class);
38          cryptoFactoryBean.setKeyStoreType("jceks");
39          cryptoFactoryBean.setKeyStorePassword("123456");
40          cryptoFactoryBean.setKeyStoreLocation(new ClassPathResource("private.jks"));
41  
42          cryptoFactoryBean.afterPropertiesSet();
43          interceptor.setSecurementSignatureCrypto((Crypto) cryptoFactoryBean
44                  .getObject());
45          interceptor.setValidationSignatureCrypto((Crypto) cryptoFactoryBean
46                  .getObject());
47          interceptor.afterPropertiesSet();
48  
49      }
50  
51      public void testAddCertificate() throws Exception {
52  
53          interceptor.setSecurementPassword("123456");
54          interceptor.setSecurementUsername("rsaKey");
55          SoapMessage message = loadSoap11Message("empty-soap.xml");
56          MessageContext messageContext = getSoap11MessageContext(message);
57  
58          interceptor.setSecurementSignatureKeyIdentifier("DirectReference");
59  
60          interceptor.secureMessage(message, messageContext);
61          Document document = getDocument(message);
62  
63          assertXpathExists("Absent BinarySecurityToken element",
64                  "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/wsse:BinarySecurityToken", document);
65  
66          // lets verfiy the signature that we've just generated
67          interceptor.validateMessage(message, messageContext);
68      }
69  
70  }