简体   繁体   中英

Upgrading from axis to axis2 for adding security header to SOAP request

I am having to upgrade from axis to axis2 but am unsure how to upgrade this functionality to work with axis2.

Here is the code that I have using axis for adding a security header to a SOAP request:

    public void addSecurityHeader(WSAPIExternal api, SecurityHeaderBean credentials) {
    try {
        Stub s = (Stub) api;
        QName namespace = new QName("http://default-url", "localPart", "prefix");
        SOAPHeaderElement header = new SOAPHeaderElement(namespace);
        SOAPElement nodeCredentials = header.addChildElement("credentials");

        SOAPElement nodeUsername = nodeCredentials.addChildElement("username");
        nodeUsername.addTextNode(credentials.getUsername());

        SOAPElement nodePassword = nodeCredentials.addChildElement("password");
        nodePassword.addTextNode(credentials.getPassword());

        s.setHeader(header);

    } catch (SOAPException e) {
        logger.error("{}", e);
    }
}

The WSAPIExternal class extends Remote which is why it can be cast to Stub .

I have tried creating something similar with OMElement but am getting confused.

So far I have this:

    public void addSecurityHeader(WSAPIExternal api, SecurityHeaderBean credentials) {
        Stub s = (Stub) api;
        ServiceClient serviceClient = s._getServiceClient();

        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMNamespace namePptData = factory.createOMNamespace("http://default-url", "prefix");

        OMElement nodeCredentials = factory.createOMElement("credentials", namePptData);

        OMElement nodeUsername = factory.createOMElement("username", namePptData);
        nodeUsername.setText(credentials.getUsername());
        nodeCredentials.addChild(nodeUsername);

        OMElement nodePassword = factory.createOMElement("password", namePptData);
        nodePassword.setText(credentials.getPassword());
        nodeCredentials.addChild(nodePassword);

        serviceClient.addHeader(nodeCredentials);
}

Any help would be greatly appreciated.

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("test");

            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("test321");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM