Secure Web Services - WS-Security

Security is one of the important key in the success of a IT project but most of the time only user authentication or data encryption are taken into account. So security of the application is often not adressed or leave aside due to complexity of the implementation. One of the reason explaining this situation comes from the fact that solutions or frameworks proposed to secure an application are difficult to configure and maintain. And this remark prevalls over the specification WS-Security.

In large company having deployed WS-Services to allow intra or inter connection between applications, Web application authentication with HTTPS protocol mechanisms are use to secure platforms. That means that users discovering the credentials used to connect to the web server can potentially have access to the services of the company.

WS-Security offers a way to authenticate the user connected to a web service or allow also a user to be trusted on the web server it is connected. This mechanism is interesting because it reinforce the security but provides also a way to restrict access to unauthorized users to web services.

Apache Camel and CXF frameworks offers a simplify way to implement this with only few lines of code and spring beans definition. Let’s see that in action :


We only need to use JAAS api to authenticate the user using the following java package “” and the project WS4J of Apache. Here is a simple example authenticate a user using a list and the password provided.

package org.apache.camel.example.reportincident;

import java.util.HashMap;
import java.util.Map;


 * Callback handler to handle passwords
public class UTPasswordCallback implements CallbackHandler {

    private Map<String, String> passwords = new HashMap<String, String>();

    public UTPasswordCallback() {
        passwords.put("claus", "sualc");
        passwords.put("charles", "selrahc");
        passwords.put("james", "semaj");
        passwords.put("abcd", "dcba");

     * Here, we attempt to get the password from the private alias/passwords map.
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {

        String user = "";

        for (Callback callback : callbacks) {
            WSPasswordCallback pc = (WSPasswordCallback) callback;
            user = pc.getIdentifier();

            String pass = passwords.get(user);
            if (pass != null) {

        // Password not found
        throw new IOException("Password does not exist for the user : " + user);

     * Add an alias/password pair to the callback mechanism.
    public void setAliasPassword(String alias, String password) {
        passwords.put(alias, password);


This is here that the magic will operates as we will use Spring beans definition with Apache Web Services Framework - CXF and Apache Camel to expose the web service

Instantiate your WS4J bean

    <bean id="wss4jInInterceptor" class="">
    </bean>    <bean id="wss4jInInterceptor" class="">
                <entry key="action" value="UsernameToken Timestamp">
                    <entry key="passwordType" value="PasswordDigest">
                        <entry key="passwordCallbackClass" value="org.apache.camel.example.reportincident.UTPasswordCallback">

Add it as an interceptor to CXF to allow CXF to authenticate the user using the credentials provided in the SOAP header definition.

<cxf:cxfendpoint id="reportIncident" address="http://localhost:9080/camel-example-reportincident/webservices/incident" wsdlurl="etc/report_incident.wsdl" serviceclass="org.apache.camel.example.reportincident.ReportIncidentEndpoint">        
            <ref bean="loggingInInterceptor">
                <ref bean="wss4jInInterceptor"></ref>
                    <ref bean="loggingOutInterceptor"> <!--  <ref bean="wss4jOutInterceptor"> --></ref>
                <cxf:cxfendpoint id="reportIncident" address="http://localhost:9080/camel-example-reportincident/webservices/incident" wsdlurl="etc/report_incident.wsdl" serviceclass="org.apache.camel.example.reportincident.ReportIncidentEndpoint">
    Here is an example of the SOAP header
    <pre name="code" class="xml"><soap:envelope soap="">
            <wsse:security wsse="" mustunderstand="1">
                <wsu:timestamp wsu="" id="Timestamp-2">
                <wsse:usernametoken wsse="" wsu="" id="UsernameToken-1">
                    <wsse:password type="">KoNvkEh9jwgvxTfcTza6+kHkKNI=</wsse:password>                    
                    <wsse:nonce encodingtype="">havIKNKvlRuatlp3CncMKw==</wsse:nonce>                    
            <ns2:inputreportincident ns2="">
                <details>Bla bla</details>
                <phone>0011 22 33 44</phone>

And finally, declare your camel route using the web services

<camel:camelcontext id="camel" xmlns="">
<camelcontext id="camel">
      <from uri="cxf:bean:reportIncident">
        <convertbodyto type="org.apache.camel.example.reportincident.InputReportIncident">

        <setheader headername="CamelFileName">
          <method bean="filenameGenerator" method="generateFilename">

        <to uri="velocity:etc/MailBody.vm">
        <to uri="file://target/subfolder">
            <method bean="myBean" method="getOK">

To play with the example, follow this example of Camel CXF and enjoy it !