Signing and Validating Soap requests with Mule ESB
Join the DZone community and get the full member experience.
Join For Freefor months i have been busy implementing a web-service-proxy by using the mule esb ce . although it took some work to get it setup nicely it is working well now. in this post i will show you the result. lets start with the configuration of the incoming request that is signed and has to validated. by the way, i know there is also the webservice-proxy-pattern in mule but i couldn’t use this in combination with the signing/ validating which i needed to do. so here is the flow which validates the incoming soap request:
<flow name="order-status-update"> <https:inbound-endpoint address="${my.incoming.url}" connector-ref="httpsconnector"> <cxf:proxy-service> <cxf:ininterceptors> <spring:bean class="org.apache.cxf.interceptor.loggingininterceptor" /> <spring:bean class="org.apache.cxf.ws.security.wss4j.wss4jininterceptor"> <spring:constructor-arg> <spring:map> <spring:entry key="action" value="signature" /> <spring:entry key="signaturepropfile" value="ws-validate-security.properties" /> </spring:map> </spring:constructor-arg> </spring:bean> </cxf:ininterceptors> </cxf:proxy-service> </https:inbound-endpoint> <https:outbound-endpoint address="${my.internal.url}" connector-ref="httpsconnector"> <cxf:proxy-client /> </https:outbound-endpoint> </flow>
note: please note that the ‘${}’ values are properties read from a property file as described in this article.
as you can see i have defined a cxf proxy with two interceptors: one for logging and one to perform security actions by using the apache wss4j library. i supply two parameters to the wss4jininterceptor:
- ‘action’: describes which action has to be performed by the interceptor. possible values are ‘signature’, ‘timestamp’, ‘encrypt’ and more.
- ‘signaturepropfile’: parameter refers to the property file to be used by the interceptor.
the contents of the ‘ws-validate-security.properties’ which is placed on the mule classpath reads the following:
org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.merlin org.apache.ws.security.crypto.merlin.keystore.type=jks org.apache.ws.security.crypto.merlin.file=/usr/local/keystores/mykeystore.jks org.apache.ws.security.crypto.merlin.keystore.password=mypasswordfor more detailed description of these properties and the used technology see this article .
but this is it to have this part configured. i used mule ce 3.2 in this case. the mule config uses the following namespaces:
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:https="http://www.mulesoft.org/schema/mule/https" xsi:schemalocation=" http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/3.2/mule-cxf.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.mulesoft.org/schema/mule/https http://www.mulesoft.org/schema/mule/https/3.2/mule-https.xsd">for completeness the used dependencies in the ‘pom.xml’ are the following:
<dependency> <groupid>org.mule</groupid> <artifactid>mule-core</artifactid> <version>3.2.1</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.mule.modules</groupid> <artifactid>mule-module-spring-config</artifactid> <version>3.2.1</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.mule.transports</groupid> <artifactid>mule-transport-http</artifactid> <version>3.2.1</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.mule.modules</groupid> <artifactid>mule-module-cxf</artifactid> <version>3.2.1</version> <scope>provided</scope> </dependency>and here is the configuration for signing outgoing requests:
<flow name="place-order"> <https:inbound-endpoint address="${my.internal.url}" connector-ref="httpsconnector" responsetimeout="0"> <cxf:proxy-service enablemulesoapheaders="false" payload="envelope" /> </https:inbound-endpoint> <https:outbound-endpoint address="${my.outgoing.url}" connector-ref="httpsconnector" responsetimeout="0" > <cxf:proxy-client payload="envelope"> <cxf:outinterceptors> <spring:bean class="org.apache.cxf.ws.security.wss4j.wss4joutinterceptor"> <spring:constructor-arg> <spring:map> <spring:entry key="action" value="signature" /> <spring:entry key="user" value="${signature.user}" /> <spring:entry key="signaturepropfile" value="ws-sign-security.properties" /> <spring:entry key="passwordcallbackclass" value="net.pascalalma.mykeystorepasswordcallback"/> <spring:entry key="signaturekeyidentifier" value="directreference" /> </spring:map> </spring:constructor-arg> </spring:bean> <spring:bean class="org.apache.cxf.interceptor.loggingoutinterceptor" /> </cxf:outinterceptors> </cxf:proxy-client> </https:outbound-endpoint> </flow>the important interceptor here is of course the wss4joutinterceptor . the properties configured here are:
- ‘action’: describes which action has to be performed by the interceptor
- ‘user’: the certificate alias in the signature crypto config to sign the message with. the password is retrieved from the callback handler.
- ‘signaturepropfile’: defines the file name that contains a properties with the desired settings in it.
- ‘passwordcallbackclass’: the reference to the callback handler for retrieving passwords for private keys in the signature and encryption crypto configurations.
- ‘signaturekeyidentifier’: signature key attachment method. i want to put the token directly in the header and not use a reference.
the ‘signaturepropfile’ contains the following info:
org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.merlin org.apache.ws.security.crypto.merlin.keystore.type=jks org.apache.ws.security.crypto.merlin.file=/usr/local/keystores/mykeystore.jks org.apache.ws.security.crypto.merlin.keystore.password=mypasswordas you can see i added the password which is used to open the keystore here in plain text. of course this is not the way to do it in a production system but for development it is quite convenient. i use the mykeystorepasswordcallback to get this password as is shown in the following implementation of this class:
package net.pascalalma; import java.io.ioexception; import java.util.resourcebundle; import javax.security.auth.callback.callback; import javax.security.auth.callback.callbackhandler; import javax.security.auth.callback.unsupportedcallbackexception; import org.apache.ws.security.wspasswordcallback; /** * * @author pascal alma */ public class mykeystorepasswordcallback implements callbackhandler { private static final string bundle_location = "ws-sign-security"; private static final string password_property_name = "org.apache.ws.security.crypto.merlin.keystore.password"; private static string password; static { final resourcebundle bundle = resourcebundle.getbundle(bundle_location); password = bundle.getstring(password_property_name); } public void handle(callback[] callbacks) throws ioexception, unsupportedcallbackexception { wspasswordcallback pc = (wspasswordcallback) callbacks[0]; // set the password for our message. pc.setpassword(password); } }
this concludes the example of a complete configuration for signing and validating soap requests with the mule esb. although it is not rocket science it took quite some time to get the complete configuration together and working. and there is still room for improvement, mainly for the error handling but i leave that for another time
Published at DZone with permission of $$anonymous$$. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Future of Software Development: Generative AI Augmenting Roles and Unlocking Co-Innovation
-
Grow Your Skills With Low-Code Automation Tools
-
Hiding Data in Cassandra
-
How To Integrate the Stripe Payment Gateway Into a React Native Application
Comments