在Spring中设置Soap请求的标头

时间:2015-10-21 11:14:54

标签: java xml spring rest soap

我一直在努力将SOAP API与我的Spring API集成。我必须在我的请求中添加标题,但我得到的是

Could not complete request
org.springframework.ws.soap.client.SoapFaultClientException: No WS-Security header found

请在下面找到我的代码

public String createSaleOrder(Suborder suborder)
{
    SaleOrder saleorder = new SaleOrder();
    saleorder = setSaleOrderObject(suborder);
    CreateSaleOrderRequest request = new CreateSaleOrderRequest();
    request.setSaleOrder(saleorder);
    this.getWebServiceTemplate().marshalSendAndReceive(uri, request, 
            new WebServiceMessageCallback() {
                public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException 
        {
                    SoapMessage soapmessage = (SoapMessage)message;                
                    SoapHeader header = soapmessage.getSoapHeader();

                    StringBuilder soapheader = new StringBuilder();
                    soapheader.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://wewewqdad.com/services/\">");
                    soapheader.append("<soapenv:Header>");
                    soapheader.append("<wsse:Security soapenv:mustUnderstand=\"1\" xmlns:wsse=\"http://docs.oasis-open.asdasda-1.0.xsd\">  ");
                    soapheader.append("<wsse:UsernameToken wsu:Id=\"UsernameToken-2\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oaasdasd-dasdasd-1.0.xsd\">");
                    soapheader.append("<wsse:Username>username</wsse:Username>");
                    soapheader.append("<wsse:Password Type=\"http://docs.aseasdasd-wss-username-token-profile-1.0#PasswordText\">password</wsse:Password>");
                    soapheader.append("<wsse:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">VMEZT//J0bZw7HfZZyXAZQ==</wsse:Nonce>");
                    soapheader.append("<wsu:Created>2014-09-04 T1015.41.649Z</wsu:Created>");
                    soapheader.append("</wsse:UsernameToken>");
                    soapheader.append("</wsse:Security>");
                    soapheader.append("</soapenv:Header>");
                    soapheader.append("<soapenv:Body>");
                    soapheader.append("</soapenv:Body>");
                    soapheader.append("</soapenv:Envelope>");

                    StringSource HeaderSource = new StringSource(soapheader.toString());
                    Transformer transformer = TransformerFactory.newInstance().newTransformer();
                    transformer.transform(HeaderSource,header.getResult());
                    }
    });
    return "Pushed";
}

更新

标题已设置。但是请求对象现在包含额外的标记,这些标记不是必需的,因此会抛出错误。还有标签,比如我们如何将这些更改为

生肥皂请求如下:

 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><soapenv:Envelope xmlns:ser="http://abc.efg.com/services/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/asdasdasd-1.0.xsd" soapenv:mustUnderstand="1">  <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-2"><wsse:Username>asdasdasd</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oaasdasda-1.0#PasswordText">adsasdasdasda</wsse:Password><wsse:Nonce EncodingType="http://dasdasda004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">VMESASDASZT//asdasdasd</wsse:Nonce><wsu:Created>2014-09-04 T1015.41.649Z</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header></soapenv:Envelope></SOAP-ENV:Header><SOAP-ENV:Body><ns2:CreateSaleOrderRequest xmlns:ns2="http://asdasd.com/services/"><ns2:SaleOrder><ns2:DisplayOrderCode>156</ns2:DisplayOrderCode></ns2:SaleOrder></ns2:CreateSaleOrderRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>

1 个答案:

答案 0 :(得分:1)

在你的soapHeader中,你写的不只是实际的标题,而是整个肥皂信封。不确定这是预期的。

无论如何,您可以使用库来处理WS Security,如 WSS4J (通常包含在其他框架中)。