簡體   English   中英

使用JAX-WS和WS-Addressing來使用SOAP

[英]Consuming SOAP with JAX-WS and WS-Addressing

我使用wsimport創建了一些JAX-WS工件。 這是我使用的wsimport命令。

wsimport -b "C:\temp\Customization.xml" -B-XautoNameResolution -d C:\temp -extension -J-Djavax.xml.accessExternalSchema=all -J-Djavax.xml.accessExternalDTD=all -keep -verbose -XadditionalHeaders -Xnocompile https://api.sendwordnow.com/webservices/v3/users.svc?wsdl

這似乎有效。 至少我認為確實如此。 wsimport創建了許多包甚至是公共接口。 現在這里是我試圖連接到我認為是有效端點的代碼片段。

HttpTransportPipe.dump = true;
Users users = new Users();
IUsers iUsers = users.getWSHttpBindingIUsers();
((BindingProvider)iUsers).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://api.sendwordnow.com/webservices/v3/Users.svc");
((BindingProvider)iUsers).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "somename");
((BindingProvider)iUsers).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "somepassword");
iUsers.echoAuthenticated("This is an echo test.");
System.out.println();

我嘗試了不同的方法來設置用戶名和密碼,但我總是收到HTTP傳輸錯誤:java.net.ConnectException:連接超時:連接。 我跳轉到SoapUI並使用來自https://api.sendwordnow.com/webservices/v3/users.svc?wsdl的WSDL創建了一個新項目。 我可以向https://api.sendwordnow.com/webservices/v3/Users.svc提交請求,並通過適當的身份驗證並將WSS-Password Type屬性設置為PasswordText,我可以成功調用EchoAuthenticated。 這是SoapUI的輸出。

Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "POST /webservices/v3/Users.svc HTTP/1.1[\r][\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "Accept-Encoding: gzip,deflate[\r][\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "Content-Type: application/soap+xml;charset=UTF-8;action="http://www.sendwordnow.com/contract/users/v3/IUsers/EchoAuthenticated"[\r][\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "Authorization: Basic c2lyaXVzYXBpOnRlbXBvcmFyeTEyMw==[\r][\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "Content-Length: 1224[\r][\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "Host: api.sendwordnow.com[\r][\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "Connection: Keep-Alive[\r][\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "User-Agent: Apache-HttpClient/4.1.1 (java 1.5)[\r][\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "[\r][\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:v3="http://www.sendwordnow.com/contract/users/v3">[\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "   <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsse:Security soap:mustUnderstand="true" 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-838F7D896DB9148E2414907229483491"><wsse:Username>somename</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">somepassword</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">QpJwiZjZex+3ikqVWZp+Yw==</wsse:Nonce><wsu:Created>2017-03-28T17:42:28.348Z</wsu:Created></wsse:UsernameToken></wsse:Security><wsa:Action>http://www.sendwordnow.com/contract/users/v3/IUsers/EchoAuthenticated</wsa:Action></soap:Header>[\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "   <soap:Body>[\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "      <v3:EchoAuthenticated>[\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "         <!--Optional:-->[\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "         <v3:value>This is an echo test.</v3:value>[\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "      </v3:EchoAuthenticated>[\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "   </soap:Body>[\n]"
Tue Mar 28 12:42:28 CDT 2017:DEBUG:>> "</soap:Envelope>"
Tue Mar 28 12:42:29 CDT 2017:DEBUG:<< "HTTP/1.1 200 OK[\r][\n]"
Tue Mar 28 12:42:29 CDT 2017:DEBUG:<< "Content-Type: application/soap+xml; charset=utf-8[\r][\n]"
Tue Mar 28 12:42:29 CDT 2017:DEBUG:<< "Server: Microsoft-IIS/8.5[\r][\n]"
Tue Mar 28 12:42:29 CDT 2017:DEBUG:<< "Date: Tue, 28 Mar 2017 17:42:29 GMT[\r][\n]"
Tue Mar 28 12:42:29 CDT 2017:DEBUG:<< "Content-Length: 887[\r][\n]"
Tue Mar 28 12:42:29 CDT 2017:DEBUG:<< "[\r][\n]"
Tue Mar 28 12:42:29 CDT 2017:DEBUG:<< "<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><s:Header><a:Action s:mustUnderstand="1">http://www.sendwordnow.com/contract/users/v3/IUsers/EchoAuthenticatedResponse</a:Action><o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><u:Timestamp u:Id="_0"><u:Created>2017-03-28T17:42:29.695Z</u:Created><u:Expires>2017-03-28T17:47:29.695Z</u:Expires></u:Timestamp></o:Security></s:Header><s:Body><EchoAuthenticatedResponse xmlns="http://www.sendwordnow.com/contract/users/v3"><EchoAuthenticatedResult>This is the Users service answering back. The value you sent was: This is an echo test.</EchoAuthenticatedResult></EchoAuthenticatedResponse></s:Body></s:Envelope>"

看起來我缺少wsa:Action,wsse:password類型和wsse:Nonce EncodingType。 至少那是SoapUI請求中的內容。 我覺得我這一切都錯了。 我顯然需要那些缺少的組件,但我似乎無法弄清楚如何實際實現它們。 任何建議都會非常有幫助。 我以為我會收到一些身份驗證錯誤,但我似乎無法達到那么遠。

更新1

我不確定我是否接近或不接近這一點。 這是SoapUI請求。

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:v3="http://www.sendwordnow.com/contract/users/v3">
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsse:Security soap:mustUnderstand="true" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>
*********</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">
**********</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">
zhVqVXnkOsPCFRZolLSWtw==</wsse:Nonce>
<wsu:Created>
2017-03-29T14:05:54.820Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
<wsa:Action>
http://www.sendwordnow.com/contract/users/v3/IUsers/EchoAuthenticated
</wsa:Action>
</soap:Header>
   <soap:Body>
      <v3:EchoAuthenticated>
         <v3:value>
Test Message</v3:value>
      </v3:EchoAuthenticated>
   </soap:Body>
</soap:Envelope>

這是一個代碼片段。

SOAPElement security = header.addChildElement("Security", "wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
SOAPElement userToken = security.addChildElement("UsernameToken", "wsse");
userToken.addChildElement("Username", "wsse").addTextNode("someusername");
userToken.addChildElement("Password", "wsse").addTextNode("somepassword");
userToken.addChildElement("Nonce", "wsse").addTextNode("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
userToken.addChildElement("Password", "wsse").addTextNode("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
userToken.addChildElement("Action", "wsa");

我不再超時,但我現在收到此錯誤。

com.sun.xml.internal.ws.streaming.XMLStreamReaderException: unexpected XML tag. expected: {http://www.sendwordnow.com/contract/users/v3}EchoAuthenticatedResponse but found: {http://www.sendwordnow.com/contract/users/v3}EchoAuthenticated

我是否需要將wsa:action設置為特定的URL?

雖然這不是唯一的一部分,但它是這個難題的主要部分。

security.addAttribute(soapFactory.createName("S:mustUnderstand"),"1");

我一直在用這個。

security.addAttribute(soapFactory.createName("SOAP:mustUnderstand"),"True");

構建WSSE和WSA元素樹的其他一些變化已經完成,但是mustUnderstand真的讓我感到很悲傷。 一切都很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM