0

I want to add following security header through java code to web service request header.

"<wsse:Security soapenv:mustUnderstand="1">

<wsse:UsernameToken>

<tenant>DEFAULT</tenant>

<wsse:Username>Admin</wsse:Username>

<wsse:Password Type="http://www.visual-rules.com/wss#PasswordText">Password</wsse:Password>

</wsse:UsernameToken>

</wsse:Security>

</soapenv:Header>"

I am using apache axis.
I want to know where and how to add this programatically.
Pls help.

mhaller
  • 14,122
  • 1
  • 42
  • 61
vishal
  • 25
  • 1
  • 8

1 Answers1

0

You can add the custom SOAP header in Axis 2 v1.6.2 in the following way:

    OMFactory fac = OMAbstractFactory.getOMFactory();
    SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
    OMNamespace nsWSSE = fac
            .createOMNamespace(
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
                    "wsse");
    SOAPHeaderBlock header = factory.createSOAPHeaderBlock("Security",
            nsWSSE);
    header.setMustUnderstand(true);
    OMElement usernameToken = fac.createOMElement("UsernameToken", nsWSSE);
    OMElement tenant = fac.createOMElement("tenant", null);
    tenant.setText("DEFAULT");
    usernameToken.addChild(tenant);
    OMElement username = fac.createOMElement("Username", nsWSSE);
    username.setText("Admin");
    usernameToken.addChild(username);
    OMElement password = fac.createOMElement("Password", nsWSSE);
    password.addAttribute("Type",
            "http://www.visual-rules.com/wss#PasswordText", null);
    password.setText("Admin");
    usernameToken.addChild(password);
    header.addChild(usernameToken);

    System.out.println(header);

    ServiceClient sender = new ServiceClient();
    sender.addHeader(header);
mhaller
  • 14,122
  • 1
  • 42
  • 61
  • Thank mhaller for your reply. – vishal Jul 05 '14 at 04:40
  • 1
    @vishal - if my solution helped, please click on Accept my answer. If you have solved it in another way, add your own solution as a second answer and accept your own answer. – mhaller Jul 22 '14 at 07:39