Skip to content

Commit bd7ba3b

Browse files
Rob Lelandgregturn
authored andcommitted
Add ability to override Wss4j's addInclusivePrefixes.
Resolves #1300. Related: #1212.
1 parent d43b25f commit bd7ba3b

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

spring-ws-security/src/main/java/org/springframework/ws/soap/security/wss4j2/Wss4jSecurityInterceptor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public class Wss4jSecurityInterceptor extends AbstractWsSecurityInterceptor impl
179179

180180
private boolean bspCompliant;
181181

182+
private boolean addInclusivePrefixes = true;
182183
private boolean securementUseDerivedKey;
183184

184185
private CallbackHandler samlCallbackHandler;
@@ -541,6 +542,15 @@ public void setBspCompliant(boolean bspCompliant) {
541542
this.bspCompliant = bspCompliant;
542543
}
543544

545+
/**
546+
* Sets whether to add an InclusiveNamespaces PrefixList as a CanonicalizationMethod child
547+
* when generating Signatures using WSConstants.C14N_EXCL_OMIT_COMMENTS. Default is {@code true}.
548+
*/
549+
public void setAddInclusivePrefixes(boolean addInclusivePrefixes) {
550+
this.handler.setOption(WSHandlerConstants.ADD_INCLUSIVE_PREFIXES, addInclusivePrefixes);
551+
this.addInclusivePrefixes = addInclusivePrefixes;
552+
}
553+
544554
/**
545555
* Sets whether the RSA 1.5 key transport algorithm is allowed.
546556
*/
@@ -676,6 +686,7 @@ protected RequestData initializeValidationRequestData(MessageContext messageCont
676686
if (requestData.getBSPEnforcer() != null) {
677687
requestData.getBSPEnforcer().setDisableBSPRules(!bspCompliant);
678688
}
689+
requestData.setAddInclusivePrefixes(addInclusivePrefixes);
679690
// allow for qualified password types for .Net interoperability
680691
requestData.setAllowNamespaceQualifiedPasswordTypes(true);
681692

spring-ws-security/src/test/java/org/springframework/ws/soap/security/wss4j2/SaajWss4jMessageInterceptorSignTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,45 @@ public void testSignAndValidate() throws Exception {
8484

8585
interceptor.validateMessage(message, messageContext);
8686
}
87+
88+
@Test
89+
public void testSignWithoutInclusivePrefixesAndValidate() throws Exception {
90+
91+
Transformer transformer = TransformerFactoryUtils.newInstance().newTransformer();
92+
interceptor.setSecurementActions("Signature");
93+
interceptor.setEnableSignatureConfirmation(false);
94+
interceptor.setSecurementPassword("123456");
95+
interceptor.setSecurementUsername("rsaKey");
96+
interceptor.setAddInclusivePrefixes(false);
97+
SOAPMessage saajMessage = saajSoap11MessageFactory.createMessage();
98+
transformer.transform(new StringSource(PAYLOAD), new DOMResult(saajMessage.getSOAPBody()));
99+
SoapMessage message = new SaajSoapMessage(saajMessage, saajSoap11MessageFactory);
100+
MessageContext messageContext = new DefaultMessageContext(message, new SaajSoapMessageFactory(saajSoap11MessageFactory));
101+
102+
interceptor.secureMessage(message, messageContext);
103+
104+
SOAPHeader header = ((SaajSoapMessage) message).getSaajMessage().getSOAPHeader();
105+
Iterator<?> iterator = header.getChildElements(
106+
new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security"));
107+
108+
assertThat(iterator.hasNext()).isTrue();
109+
110+
SOAPHeaderElement securityHeader = (SOAPHeaderElement) iterator.next();
111+
iterator = securityHeader.getChildElements(new QName("http://www.w3.org/2000/09/xmldsig#", "Signature"));
112+
113+
assertThat(iterator.hasNext()).isTrue();
114+
115+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
116+
message.writeTo(bos);
117+
118+
MimeHeaders mimeHeaders = new MimeHeaders();
119+
mimeHeaders.addHeader("Content-Type", "text/xml");
120+
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
121+
122+
SOAPMessage signed = saajSoap11MessageFactory.createMessage(mimeHeaders, bis);
123+
message = new SaajSoapMessage(signed, saajSoap11MessageFactory);
124+
messageContext = new DefaultMessageContext(message, new SaajSoapMessageFactory(saajSoap11MessageFactory));
125+
126+
interceptor.validateMessage(message, messageContext);
127+
}
87128
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2005-2022 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.wss4j2;
18+
19+
import org.apache.wss4j.dom.handler.RequestData;
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.test.util.ReflectionTestUtils;
22+
import org.springframework.ws.context.DefaultMessageContext;
23+
import org.springframework.ws.context.MessageContext;
24+
import org.springframework.ws.soap.SoapMessage;
25+
import org.springframework.ws.soap.saaj.SaajSoapMessage;
26+
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
27+
import org.springframework.xml.transform.StringSource;
28+
import org.springframework.xml.transform.TransformerFactoryUtils;
29+
30+
import jakarta.xml.soap.SOAPException;
31+
import jakarta.xml.soap.SOAPMessage;
32+
import javax.xml.transform.Transformer;
33+
import javax.xml.transform.TransformerException;
34+
import javax.xml.transform.dom.DOMResult;
35+
36+
import static org.junit.jupiter.api.Assertions.assertFalse;
37+
import static org.junit.jupiter.api.Assertions.assertTrue;
38+
import static org.springframework.test.util.AssertionErrors.assertEquals;
39+
40+
public class SaajWss4jSecurityInterceptorDefaultsTest extends Wss4jTestCase {
41+
42+
private static final String PAYLOAD = "<tru:StockSymbol xmlns:tru=\"http://fabrikam123.com/payloads\">QQQ</tru:StockSymbol>";
43+
44+
@Test
45+
public void testThatTheDefaultValueForAddInclusivePrefixesMatchesWss4JDefaultValue() {
46+
Wss4jSecurityInterceptor subject = new Wss4jSecurityInterceptor();
47+
RequestData requestData = new RequestData();
48+
Boolean springDefault = (Boolean) ReflectionTestUtils.getField(subject, Wss4jSecurityInterceptor.class, "addInclusivePrefixes");
49+
assertEquals("Spring-ws default for addInclusivePrefixes matches Wss4j default", requestData.isAddInclusivePrefixes(), springDefault);
50+
}
51+
52+
@Test
53+
public void testThatInitializeValidationRequestDataSetsInclusivePrefixesUsingDefaults() throws TransformerException, SOAPException {
54+
Wss4jSecurityInterceptor subject = new Wss4jSecurityInterceptor();
55+
56+
Transformer transformer = TransformerFactoryUtils.newInstance().newTransformer();
57+
58+
SOAPMessage saajMessage = saajSoap11MessageFactory.createMessage();
59+
transformer.transform(new StringSource(PAYLOAD), new DOMResult(saajMessage.getSOAPBody()));
60+
SoapMessage message = new SaajSoapMessage(saajMessage, saajSoap11MessageFactory);
61+
MessageContext messageContext = new DefaultMessageContext(message, new SaajSoapMessageFactory(saajSoap11MessageFactory));
62+
63+
RequestData validationData = ReflectionTestUtils.invokeMethod(subject, "initializeValidationRequestData", messageContext);
64+
65+
assertTrue(validationData.isAddInclusivePrefixes());
66+
}
67+
68+
@Test
69+
public void testThatInitializeValidationRequestDataSetsInclusivePrefixesUsingNotUsingInclusivePrefixes() throws TransformerException, SOAPException {
70+
Wss4jSecurityInterceptor subject = new Wss4jSecurityInterceptor();
71+
subject.setAddInclusivePrefixes(false);
72+
Transformer transformer = TransformerFactoryUtils.newInstance().newTransformer();
73+
74+
SOAPMessage saajMessage = saajSoap11MessageFactory.createMessage();
75+
transformer.transform(new StringSource(PAYLOAD), new DOMResult(saajMessage.getSOAPBody()));
76+
SoapMessage message = new SaajSoapMessage(saajMessage, saajSoap11MessageFactory);
77+
MessageContext messageContext = new DefaultMessageContext(message, new SaajSoapMessageFactory(saajSoap11MessageFactory));
78+
79+
RequestData validationData = ReflectionTestUtils.invokeMethod(subject, "initializeValidationRequestData", messageContext);
80+
81+
assertFalse(validationData.isAddInclusivePrefixes());
82+
}
83+
}

0 commit comments

Comments
 (0)