title: UserNameToken in soap from Java
body: <p>I am trying to use a gSoap-generated wsdl from Netbeans. The webservice requires that the UserNameToken be passed in. When I use the wsdl from SoapUI (which works), it sends this:</p>
  
  <pre><code>&lt;wsse:Username&gt;myname&lt;/wsse:Username&gt;
  &lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"&gt;mypass&lt;/wsse:Password&gt;
  </code></pre>
  
  <p>
     </p>
  
  <p>However, when I try it from Netbeans using a handler (referred to by <a href="https://stackoverflow.com/questions/830691">How do I add a SOAP Header using Java JAX-WS</a>) like this:</p>
  
  <pre><code>            String prefix = "wsse";
              String uri = "http://...wsssecurity...";
  
              SOAPElement securityElem = factory.createElement("Security",prefix,uri);
  
                  SOAPElement UserNametokenElem = factory.createElement("UserNameToken",prefix,uri);
  
                      SOAPElement UsernameElem = factory.createElement("wsse:Username");
                      UsernameElem.addTextNode("myname");
                      SOAPElement PasswordElem = factory.createElement("Password");
                      PasswordElem.addTextNode("mypass");
  
                      UserNametokenElem.addChildElement(UsernameElem);
                      UserNametokenElem.addChildElement(PasswordElem);
  
                      securityElem.addChildElement(UserNametokenElem);
              SOAPHeader header = envelope.addHeader();
              header.addChildElement(securityElem);
  </code></pre>
  
  <p>it generates the soap headers like this:</p>
  
  <pre><code>    &lt;wsse:Security xmlns:wsse="http://...wsssecurity..."&gt;
      &lt;wsse:UserNameToken xmlns:wsse="http://...wsssecurity..."&gt;
      &lt;Username xmlns=""&gt;myname&lt;/Username&gt;
      &lt;Password xmlns=""&gt;mypass&lt;/Password&gt;
      &lt;/wsse:UserNameToken&gt;
      &lt;/wsse:Security&gt;
  </code></pre>
  
  <p>which causes the webservice to reply with an authentication error.</p>
  
  <p>My questions are therefore:</p>
  
  <ol>
  <li>How do I generate the UserNameToken in the correct way (as SoapUi does)?</li>
  <li>Is it better to do it using a handler, or an external WSSE library like Apache WSS4J (can someone show me some sample code for this)</li>
  </ol>