在Spring客户端中向soap头添加安全性详细信息

时间:2014-12-20 20:42:28

标签: web-services soap spring-ws

我正在尝试使用Spring WS模板作为客户端来使用SOAP(外部系统)Web服务。

我需要将标题信息(安全性详细信息)作为标题和soap消息一起传递。

我尝试使用java xml SOAPMessage(javax.xml.soap.SoapMessage)将该信息添加到Header中,并尝试将其转换为spring SOAPMessage(org.springframework.ws.soap)。添加标题详细信息后,SoapMessage)

但是它无法投射它,因为它们都不是heirarchy而得到类强制转换异常。

请帮我介绍如何在spring soap message

中传递标题信息的安全详细信息

我的代码如下

   public void doWithMessage(WebServiceMessage message) throws IOException,
                                    TransformerException

    SaajSoapMessage saajSoapMessage =(SaajSoapMessage)message;      
    SOAPMessage soapMessage = saajSoapMessage.getSaajMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
    SOAPHeader soapHeader = soapEnvelope.getHeader();
    Name headerElementName = soapEnvelope.createName("Security","wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

    // Add "Security" soapHeaderElement to soapHeader
    SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(headerElementName);

    // This may be important for some portals!
    soapHeaderElement.setActor(null);

    // Add usernameToken to "Security" soapHeaderElement 
    SOAPElement usernameTokenSOAPElement = soapHeaderElement.addChildElement("UsernameToken");

    // Add username to usernameToken
    SOAPElement userNameSOAPElement = usernameTokenSOAPElement.addChildElement("Username");
    userNameSOAPElement.addTextNode("myUserName");          
    // Add password to usernameToken
    SOAPElement passwordSOAPElement = usernameTokenSOAPElement.addChildElement("Password");

    passwordSOAPElement.addTextNode("myPassword");
    ((SoapMessage) soapMessage).setSoapAction("GetMetaDataLookUpRequestType");//exception while casting
}

我的请求xml如下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://emc.com/it/enterprise/contract/CMCContractLookupService/v1" xmlns:v11="http://emc.com/it/enterprise/data/v1">
   <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-20">
            <wsse:Username>xxxx</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxxx</wsse:Password>

          </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>

      <v1:GetContractMetaDataLookUpRequest>
         <v11:GetMetaDataLookUpRequestDocument>
            <v11:ContractID>123456</v11:ContractID>
             <v11:BadgeID>1</v11:BadgeID>

         </v11:GetMetaDataLookUpRequestDocument>
      </v1:GetContractMetaDataLookUpRequest>
   </soapenv:Body>
</soapenv:Envelope>

我需要帮助主要是如何将头信息(如上面的xml请求中所示)添加到soap标头消息中。

非常感谢帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

这应该是saajSoapMessage.setSoapAction("GetMetaDataLookUpRequestType")而不是((SoapMessage)soapMessage).setSoapAction("GetMetaDataLookUpRequestType")