1   /*
2    * Copyright 2005-2010 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.springframework.core.io.ClassPathResource;
20  import org.springframework.ws.context.MessageContext;
21  import org.springframework.ws.soap.SoapMessage;
22  import org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean;
23  
24  import org.apache.ws.security.components.crypto.Crypto;
25  import org.apache.ws.security.components.crypto.Merlin;
26  import org.junit.Test;
27  import org.w3c.dom.Document;
28  
29  public abstract class Wss4jMessageInterceptorX509TestCase extends Wss4jTestCase {
30  
31      protected Wss4jSecurityInterceptor interceptor;
32  
33      @Override
34      protected void onSetup() throws Exception {
35          interceptor = new Wss4jSecurityInterceptor();
36          interceptor.setSecurementActions("Signature");
37          interceptor.setValidationActions("Signature");
38          CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();
39          cryptoFactoryBean.setCryptoProvider(Merlin.class);
40          cryptoFactoryBean.setKeyStoreType("jceks");
41          cryptoFactoryBean.setKeyStorePassword("123456");
42          cryptoFactoryBean.setKeyStoreLocation(new ClassPathResource("private.jks"));
43  
44          cryptoFactoryBean.afterPropertiesSet();
45          interceptor.setSecurementSignatureCrypto((Crypto) cryptoFactoryBean
46                  .getObject());
47          interceptor.setValidationSignatureCrypto((Crypto) cryptoFactoryBean
48                  .getObject());
49          interceptor.afterPropertiesSet();
50  
51      }
52  
53      @Test
54      public void testAddCertificate() throws Exception {
55  
56          interceptor.setSecurementPassword("123456");
57          interceptor.setSecurementUsername("rsaKey");
58          SoapMessage message = loadSoap11Message("empty-soap.xml");
59          MessageContext messageContext = getSoap11MessageContext(message);
60  
61          interceptor.setSecurementSignatureKeyIdentifier("DirectReference");
62  
63          interceptor.secureMessage(message, messageContext);
64          Document document = getDocument(message);
65  
66          assertXpathExists("Absent BinarySecurityToken element",
67                  "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/wsse:BinarySecurityToken", document);
68  
69          // lets verify the signature that we've just generated
70          interceptor.validateMessage(message, messageContext);
71      }
72  
73  }