title: Adding soap header authentication to wsdl2java generated code
body: <p>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. </p>
  
  <p><a href="https://stackoverflow.com/questions/14266237/adding-ws-security-to-wsdl2java-generated-classes/14332481#14332481">Adding ws-security to wsdl2java generated classes</a></p>
  
  <p>Before I was getting a "Error occurred while processing security for the message" or something along those lines. Now I am getting</p>
  
  <p>"Exception: Did not understand "MustUnderstand" header(s):{<a href="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" rel="nofollow noreferrer">http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd</a>}Security Message: null"</p>
  
  <p>I've tried many things to get past this exception. This is the code I've arrived at now.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>This code is located in my Binding_ServiceStub class (in its' createCall method).</p>
  
  <p>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.</p>
  
  <p>Here's the security policy from the wsdl code as well.</p>
  
  <pre><code>&lt;wsp:Policy&gt;&lt;sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient"&gt;&lt;wsp:Policy&gt;&lt;sp:WssUsernameToken10/&gt;&lt;/wsp:Policy&gt;&lt;/sp:UsernameToken&gt;&lt;/wsp:Policy&gt;
  </code></pre>
  
  <p>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).</p>
  
  <p>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.</p>
  
  <p><strong>Update-</strong>
  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...)</p>