sree
sree

Reputation: 31

Setting custom tag in Soap Security Header (Spring WS)

I'm using SpringWSTemplate Client to send a message. Using the method sendSourceAndReceiveToResult(Source requestPayload, WebServiceMessageCallback requestCallback, Result responseResult).

In this I'm setting a few security credentials using wss4jsecurityinterceptor. But currently I'm in need of setting a custom tag (RegisterKey) inside usernametoken as similar to shown below.

<wsse:Security soap:mustUnderstand="true" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <wsse:UsernameToken wsu:Id="UsernameToken-11" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:Username>test</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">a287645857cfaaddf82e2d333651b3e0</wsse:Password>
        <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">oKGlwEkbkhYJH6upsbiqeQ==</wsse:Nonce>
        <wsu:Created>2011-10-25T13:10:11.958Z</wsu:Created>
        <RegisterKey>UUUiiiIUBGGGTTT</RegisterKey>
    </wsse:UsernameToken>
</wsse:Security>

I tried using Transformer (java.xml.transform.transformer) to inject the custom tag, but it's getting injected straight to SoapHeader and not within UsernameToken.

Upvotes: 3

Views: 1492

Answers (2)

Anupam
Anupam

Reputation: 517

These details are already provided by spring-ws dependency

<dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-security</artifactId>
            <version>1.5.6</version>
</dependency>

Add the below details in the spring-config.xml to get the below details

<bean id="xwsSecurityInterceptor"
        class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
        <property name="securementActions" value="UsernameToken" />
        <property name="securementUsername" value="UNAME" />
        <property name="securementPasswordType" value="PasswordText" />
        <property name="securementPassword" value="Pass" />
        <property name="securementMustUnderstand" value="false" />
</bean>

And add the interceptor to the webserviceTemplate

Upvotes: 1

sree
sree

Reputation: 31

I had done this using implementing ClientInterceptor

org.springframework.ws.client.support.interceptor.ClientInterceptor

ClientInterceptor does have a handleRequest(MessageContext context) method in which we can manipulate the request message(including the SoapHeaders).

Using org.apache.axiom.om.OMContainer you can iterate through the elements and using org.apache.axiom.om.impl.llom.OMElementImpl you can add a new element and set its value too.

Upvotes: 0

Related Questions