Как добавить две опции безопасности "wsse" и "wsu" в одном заголовке - PullRequest
0 голосов
/ 02 марта 2019

Кто-нибудь может подсказать, пожалуйста, как конвертировать этот мыльный конверт в код Java?Это заголовок, пока я работаю с API из SOAPUI.

 <soapenv:Header>
 <wsse:Security soapenv:mustUnderstand="1"  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
                                        xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">

<wsse:UsernameToken wsu:Id="UsernameToken-A139C7EBE4BD7F3FA7155144588419221">

<wsse:Username>xxxxxx</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxxxxx</wsse:Password>

<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">CfPQR+H2TUhwnvV1k5jByw== 
</wsse:Nonce>
 <wsu:Created>2019-03-01T13:11:24.192Z</wsu:Created>
 </wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>

Вот мой java-код:

public class MessageHandlerCustomer implements SOAPHandler<SOAPMessageContext> {

@Override
public boolean handleMessage(SOAPMessageContext smc) {

    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    logger.debug("Entered in MessageHandlerCustomer");

    if (outboundProperty.booleanValue()) {

        String UserName = "xxx";
        String Password = "xxxx";
                    SimpleDateFormat timeStamp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                    timeStamp.setTimeZone(TimeZone.getTimeZone("GMT"));

        try {
            /*
             * add a header with the authentication info into the SOAP
             */
            // get SOAP envelope from SOAP message
            final SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
            // create instance of SOAP factory
            final SOAPFactory soapFactory = SOAPFactory.newInstance();
            // create SOAP elements specifying prefix and URI
            // final SOAPElement headerElm =
            // soapFactory.createElement(GRCBIZConstantes.HEADER_WS_SERVICE,
            // GRCBIZConstantes.PREFIX_WS_SERVICE,
            // GRCBIZConstantes.URI_CUSTOMER_SERVICE);

            final SOAPElement headerElm = soapFactory.createElement("Security", "wsse",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

                            headerElm.setAttribute("soapenv:mustUnderstand", "1");



            final SOAPElement securityElm = soapFactory.createElement("UsernameToken", "wsse",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
            securityElm.setAttribute("wsu:Id", "UsernameToken-A139C7EBE4BD7F3FA7155144588419221");


            final SOAPElement userNameElm = soapFactory.createElement("Username", "wsse",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
            userNameElm.addTextNode(UserName);

            final SOAPElement passwordElm = soapFactory.createElement("Password", "wsse",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
            passwordElm.addTextNode(Password);
            passwordElm.setAttribute("Type",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");

                            final SOAPElement nonce = soapFactory.createElement("Nonce", "wsse",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
                            nonce.setAttribute("EncodingType", 
                                            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
                            nonce.addTextNode("CfPQR+H2TUhwnvV1k5jByw==");

                            final SOAPElement created = soapFactory.createElement("Created", "wsu",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
            userNameElm.addTextNode(timeStamp.toString());

            securityElm.addChildElement(userNameElm);
            securityElm.addChildElement(passwordElm);
                            securityElm.addChildElement(nonce);
                            securityElm.addChildElement(created);


            headerElm.addChildElement(securityElm);

            // create SOAPHeader instance for SOAP envelope
            final SOAPHeader sh = envelope.addHeader();
            // add SOAP element for header to SOAP header object
            sh.addChildElement(headerElm);

                            logger.debug("Header: " + sh.getAllAttributes().toString());
        } catch (final Exception ex) {
            logger.error(ex.getMessage(), ex);
        }

    }
    return outboundProperty;

}

public static String elementToString(Element element) {
    Document document = element.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    String str = serializer.writeToString(element);
    return str;
}

@Override
public Set getHeaders() {
    return null;
}

@Override
public boolean handleFault(SOAPMessageContext context) {
    return true;
}

@Override
public void close(MessageContext context) {
}
}

Передается несколько заголовков, таких как mustUnderstand и тег безопасности wsu.Во-вторых, я хочу напечатать заголовок в журналах.На самом деле я получаю статический ответ от веб-службы, и веб-служба не сообщает, что именно отсутствует.

...