Groovy中在soap中使用标头的基本身份验证凭据

时间:2019-05-22 22:52:15

标签: soap groovy

我正在尝试连接到WSDL服务器,该服务器需要基本身份验证以及主体。我尝试使用SOAPUI进行匹配,并且能够做到这一点。在groovy中使用相同的方法并使用wslite包进行SOAP调用时,出现“需要密码”错误。

我在https://github.com/jwagenleitner/groovy-wslite上尝试了该教程,但是(在“用法”块中)描述的方法没有帮助。 我为此使用groovy。

下面是SOAP UI中的原始代码,响应为200OK

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:w="http://xmlns.oracle.com/Enterprise/Tools/schemas/W_CHKLST_CREATE_REQ.1">
   <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-83C962CC24EAB67F1D15586306447678"><wsse:Username>userName</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Password</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">JLTdeK6Yg3D64+2qz5xnbA==</wsse:Nonce><wsu:Created>2019-05-23T16:57:24.767Z</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header>
   <soapenv:Body>
      <w:helloWorld/>
   </soapenv:Body>
</soapenv:Envelope>

这会导致SOAPUI达到200OK

HTTP/1.1 200 OK
Date: Wed, 22 May 2019 21:54:12 GMT
Content-Type: text/xml; charset=UTF-8
Content-Length: 3570

我的Groovy代码,

def client = new SOAPClient("URL of WSDL")
def response = client.send(SOAPAction: "someAction",
                connectTimeout:10000,
                readTimeout:20000,
                useCaches:false,
                followRedirects:false) {
            version SOAPVersion.V1_2 // SOAPVersion.V1_1 is default
            soapNamespacePrefix "soapenv"      // "soap-env" is default
            envelopeAttributes "xmlns:ns":"http://example.weather.org"
            header {
                security("xmlns:ns20":"http://SecurityOpenXSD"){
                    "ns20:Username"("username")
                    "ns20:Password"("password")
                    }
                }



            body {
                "ns:helloWorld" {

                }
            }
        }

将soap xml设为

    <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-83C962CC24EAB67F1D15586299636605'>
<wsse:Username>userName</wsse:Username>
<wsse:Password 
Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>
password
</wsse:Password>
<wsse:Nonce 
    EncodingType='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary'>
    JLTdeK6Yg3D64+2qz5xnbA==
</wsse:Nonce>
<wsu:Created>2019-05-23T10:07:52.913Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
  </soapenv:Header>

但是我输入密码错误,

<?xml version="1.0" ?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
    <SOAP-ENV:Body>
    <SOAP-ENV:Fault>
    <Code><Value>SOAP-ENV:Receiver</Value></Code>
    <Reason><Text>null</Text></Reason>
    <Detail><IBResponse type="error" xmlns=""><DefaultTitle> Error Response</DefaultTitle>
    <StatusCode>20</StatusCode>
    <DefaultMessage><![CDATA[User Password required]]></DefaultMessage>
    </IBResponse></Detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

0 个答案:

没有答案