如何向apache axis web服务请求添加安全头?

时间:2014-06-03 04:40:46

标签: web-services web axis webservices-client visualrules

我想通过java代码将以下安全头添加到Web服务 请求标题。

"<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>"

我正在使用apache轴。
我想知道在哪里以及如何以编程方式添加它 请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式在Axis 2 v1.6.2中添加自定义SOAP标头:

    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);