SOAP Header Authentication for every method call in JAVA

532 Views Asked by At

Basically every method call to my SOAP web service required user authentication. So this is what SOAP header should look like:

<soap:Header>
  <HTNGHeader xmlns="http://htng.org/1.1/Header/">
    <From>
      <systemId />
      <Credential>
        <userName>string</userName>
        <password>string</password>
      </Credential>
    </From>
    <To>
      <systemId />
    </To>
    <timestamp />
    <echoToken />
      <transactionId />
    <action />
  </HTNGHeader>
</soap:Header>

This is what I found so far link. I'm sure that I have to make change in this block

      try {
        SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
        SOAPHeader header = envelope.addHeader();

        SOAPElement security =
                header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

        SOAPElement usernameToken =
                security.addChildElement("UsernameToken", "wsse");
        usernameToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

        SOAPElement username =
                usernameToken.addChildElement("Username", "wsse");
        username.addTextNode("TestUser");

        SOAPElement password =
                usernameToken.addChildElement("Password", "wsse");
        password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
        password.addTextNode("TestPassword");

        //Print out the outbound SOAP message to System.out
        message.writeTo(System.out);
        System.out.println("");

    } catch (Exception e) {
        e.printStackTrace();
    }

But I can't make it right. I would appreciate all your idea.

Thanks

0

There are 0 best solutions below