Skip to content

Commit 4ce9e67

Browse files
committed
SWS-884: Optionally keep security header inside Wss4jSecurityInterceptor
1 parent 739d2f1 commit 4ce9e67

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
*
8383
* @author Tareq Abed Rabbo
8484
* @author Arjen Poutsma
85+
* @author Greg Turnquist
8586
* @see <a href="http://ws.apache.org/wss4j/">Apache WSS4J</a>
8687
* @since 1.5.0
8788
*/
@@ -137,6 +138,9 @@ public class Wss4jSecurityInterceptor extends AbstractWsSecurityInterceptor impl
137138

138139
private boolean securementUseDerivedKey;
139140

141+
// To maintain same behavior as default, this flag is set to true
142+
private boolean removeSecurityHeader = true;
143+
140144
public void setSecurementActions(String securementActions) {
141145
this.securementActions = securementActions;
142146
securementActionsVector = new ArrayList<Integer>();
@@ -502,7 +506,15 @@ public void setSamlIssuer(SAMLIssuer samlIssuer) {
502506
this.samlIssuer = samlIssuer;
503507
}
504508

505-
@Override
509+
public boolean getRemoveSecurityHeader() {
510+
return removeSecurityHeader;
511+
}
512+
513+
public void setRemoveSecurityHeader(boolean removeSecurityHeader) {
514+
this.removeSecurityHeader = removeSecurityHeader;
515+
}
516+
517+
@Override
506518
public void afterPropertiesSet() throws Exception {
507519
Assert.isTrue(validationActions != null || securementActions != null,
508520
"validationActions or securementActions are required");
@@ -628,7 +640,9 @@ protected void validateMessage(SoapMessage soapMessage, MessageContext messageCo
628640

629641
soapMessage.setDocument(envelopeAsDocument);
630642

631-
soapMessage.getEnvelope().getHeader().removeHeaderElement(WS_SECURITY_NAME);
643+
if (this.getRemoveSecurityHeader()) {
644+
soapMessage.getEnvelope().getHeader().removeHeaderElement(WS_SECURITY_NAME);
645+
}
632646
}
633647

634648
/**

spring-ws-security/src/test/java/org/springframework/ws/soap/security/wss4j/Wss4jMessageInterceptorHeaderTestCase.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,26 @@
2121
import java.io.ByteArrayOutputStream;
2222
import java.util.Iterator;
2323
import java.util.Properties;
24-
2524
import javax.xml.namespace.QName;
2625

2726
import org.junit.Test;
27+
2828
import org.springframework.ws.context.DefaultMessageContext;
2929
import org.springframework.ws.context.MessageContext;
3030
import org.springframework.ws.soap.SoapHeaderElement;
3131
import org.springframework.ws.soap.SoapMessage;
3232
import org.springframework.ws.soap.security.WsSecurityValidationException;
3333
import org.springframework.ws.soap.security.wss4j.callback.SimplePasswordValidationCallbackHandler;
3434

35+
/**
36+
* @author Arjen Poutsma
37+
* @author Tareq Abedrabbo
38+
* @author Greg Turnquist
39+
*/
3540
public abstract class Wss4jMessageInterceptorHeaderTestCase extends Wss4jTestCase {
3641

3742
private Wss4jSecurityInterceptor interceptor;
43+
private Wss4jSecurityInterceptor interceptorThatKeepsSecurityHeader;
3844

3945
@Override
4046
protected void onSetup() throws Exception {
@@ -48,6 +54,14 @@ protected void onSetup() throws Exception {
4854
callbackHandler.setUsers(users);
4955
interceptor.setValidationCallbackHandler(callbackHandler);
5056
interceptor.afterPropertiesSet();
57+
58+
interceptorThatKeepsSecurityHeader = new Wss4jSecurityInterceptor();
59+
interceptorThatKeepsSecurityHeader.setValidateRequest(true);
60+
interceptorThatKeepsSecurityHeader.setSecureResponse(true);
61+
interceptorThatKeepsSecurityHeader.setValidationActions("UsernameToken");
62+
interceptorThatKeepsSecurityHeader.setValidationCallbackHandler(callbackHandler);
63+
interceptorThatKeepsSecurityHeader.setRemoveSecurityHeader(false);
64+
interceptorThatKeepsSecurityHeader.afterPropertiesSet();
5165
}
5266

5367
@Test
@@ -75,6 +89,31 @@ public void testValidateUsernameTokenPlainText() throws Exception {
7589

7690
}
7791

92+
@Test
93+
public void testValidateUsernameTokenPlainTextButKeepSecurityHeader() throws Exception {
94+
SoapMessage message = loadSoap11Message("usernameTokenPlainTextWithHeaders-soap.xml");
95+
MessageContext messageContext = new DefaultMessageContext(message, getSoap11MessageFactory());
96+
interceptorThatKeepsSecurityHeader.validateMessage(message, messageContext);
97+
Object result = getMessage(message);
98+
assertNotNull("No result returned", result);
99+
100+
boolean foundSecurityHeader = false;
101+
for (Iterator<SoapHeaderElement> i = message.getEnvelope().getHeader().examineAllHeaderElements(); i.hasNext();) {
102+
SoapHeaderElement element = i.next();
103+
QName name = element.getName();
104+
if (name.getNamespaceURI()
105+
.equals("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")) {
106+
foundSecurityHeader = true;
107+
}
108+
109+
}
110+
assertTrue(foundSecurityHeader);
111+
112+
assertXpathExists("header1 not found", "/SOAP-ENV:Envelope/SOAP-ENV:Header/header1", getDocument(message));
113+
assertXpathExists("header2 not found", "/SOAP-ENV:Envelope/SOAP-ENV:Header/header2", getDocument(message));
114+
115+
}
116+
78117
@Test(expected=WsSecurityValidationException.class)
79118
public void testEmptySecurityHeader() throws Exception {
80119
SoapMessage message = loadSoap11Message("emptySecurityHeader-soap.xml");

0 commit comments

Comments
 (0)