Question

I'm in the process of creating a Java web services client from a wsdl. I used Eclipses's Dynamic Web Project and new Web Services Client to generate the code with wsdl2java with Apache Axis 1.4. I need to add SOAP authentication to this code in order for it to work with the service. I couldn't find a place to do that in the generated code. After copious research I found this, which I've used as the backbone for my code so far.

Adding ws-security to wsdl2java generated classes

Before I was getting a "Error occurred while processing security for the message" or something along those lines. Now I am getting

"Exception: Did not understand "MustUnderstand" header(s):{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}Security Message: null"

I've tried many things to get past this exception. This is the code I've arrived at now.

javax.xml.namespace.QName headerName = new javax.xml.namespace.QName(
                "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
        org.apache.axis.message.SOAPHeaderElement header = new org.apache.axis.message.SOAPHeaderElement(headerName);
        header.setActor(null);
        header.setMustUnderstand(true);

        // Add the UsernameToken element to the WS-Security header
        javax.xml.soap.SOAPElement utElem = header.addChildElement("UsernameToken");
        utElem.setAttribute("Id", "uuid-3453f017-d595-4a5b-bc16-da53e5831cd1-1");
        javax.xml.soap.SOAPElement userNameElem = utElem.addChildElement("Username");
        userNameElem.setValue("username");
        javax.xml.soap.SOAPElement passwordElem = utElem.addChildElement("Password");
        passwordElem.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
        passwordElem.setValue("password");

        header.setProcessed(true);

        // Finally, attach the header to the binding.
        setHeader(header)

This code is located in my Binding_ServiceStub class (in its' createCall method).

We have created clients in both C# and VB with this wsdl, and there it's as easy as just changing the ClientCredentials variable which is an extension of the proxy class generated. I was hoping for something similar here.

Here's the security policy from the wsdl code as well.

<wsp:Policy><sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient"><wsp:Policy><sp:WssUsernameToken10/></wsp:Policy></sp:UsernameToken></wsp:Policy>

Does anyone know what else I can do here? Why this exception is happening? I've tried many different combinations of prefixes and setProcesses and setMustUnderstand values all in vain (and based on my research of this exception).

And if anyone knows a way in which to add Soap header authentication to wsdl2java code I would take that too. Just need this to work and you would think something like this would be a little more straightforward or at least have more examples out there.

Update- Confirmed that the same header passed using SOAPUI works fine. Must be something with the framework? I created a custom handler to process the SOAP Message but that didn't help. Is Axis 1.4 and JAX-RPC the problem? (I know they're outdated but still...)

Was it helpful?

Solution

Cool. I decided to just use Apache CXF as my framework and using this it's as easy as adding

javax.xml.ws.BindingProvider bp = (javax.xml.ws.BindingProvider) port;
    bp.getRequestContext().put("ws-security.username", username);
    bp.getRequestContext().put("ws-security.password", password);

Wow that's much better. Don't use Axis 1.4 lesson learned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top