2016-11-10 81 views
0

我使用Spring-WS消耗下面的WSDL添加到SOAP動作: https://pz.gov.pl/pz-services/SignatureVerification?wsdl 我已經生成的Java類要做到這一點,就像在本教程:https://spring.io/guides/gs/consuming-web-service/春-WS如何屬性的請求主體

這個WSDL文件的

文件規定,即要求必須有一個屬性CALLID和requestTimestamp僅有在下面的例子一樣:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tpus="http://verification.zp.epuap.gov.pl"> <soapenv:Header/> <soapenv:Body> <tpus:verifySignature callId="6347177294896046332" requestTimestamp="2014-06-30T12:01:30.048+02:00"> <tpus:doc>PD94bWwgdmVyc2E+</tpus:doc> <tpus:attachments> <tpus:Attachment> <tpus:content>PD94bWwgdmVyc2+</tpus:content> <tpus:name>podpis.xml</tpus:name> </tpus:Attachment> </tpus:attachments> </tpus:verifySignature> </soapenv:Body> </soapenv:Envelope>

我的要求是這樣的:

<SOAP-ENV:Envelope 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
<SOAP-ENV:Header/> 
<SOAP-ENV:Body 
      xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-82BA5532C"> 
      <ns3:verifySignature 
       xmlns:ns3="http://verification.zp.epuap.gov.pl" 
       xmlns=""> 
       <doc>PD94bWwgdmVyc2E+</doc> 
       <attachments> 
        <Attachment> 
         <content>PD94bWwgdmVyc2+</content> 
         <name>podpis.xml</name> 
        </Attachment> 
       </attachments> 
      </ns3:verifySignature> 
     </SOAP-ENV:Body> 
    </SOAP-ENV:Envelope> 

因此,您可以看到我缺少callId和requestTimestamp屬性。如果我的代碼發送請求看起來像這樣,我可以如何添加它們?

public class TrustedProfileValidator extends WebServiceGatewaySupport { 
private static final Logger tpLogger = Logger.getLogger(TrustedProfileValidator.class); 

/** 
* Trusted profile validator constructor 
*/ 
public TrustedProfileValidator() { 
    tpLogger.info("Trusted profile validator service."); 
} 

public VerifySignatureResponse validate(byte[] documentInByte64, ArrayOfAttachment arrayOfAttachments) { 
    tpLogger.info("Checking trusted profile validation"); 
    VerifySignature request = new VerifySignature(); 
    request.setDoc(documentInByte64); 
    request.setAttachments(arrayOfAttachments); 

    return (VerifySignatureResponse) getWebServiceTemplate().marshalSendAndReceive(
      "https://int.pz.gov.pl/pz-services/SignatureVerification", request, 
      new SoapActionCallback("verifySignature")); 
} 

}

+0

嗯,我想這是錯的。在你給的WSDL中,我看不到有關reqGetTpUserObjectsInfo,callId和requestTimestamp的信息;所以或yuo都讀取另一個文檔o您發佈了不同的WSDL –

+0

,因爲示例(在文檔中)是用於其他方法的,callId和requestTimestamp是必須爲每個請求設置的參數。所以我的請求也應該有這些參數。我將編輯這個例子,所以不存在誤解 – Dario3d

+0

奇怪的是,在WSDL定義中沒有引用屬性callId和requestTimestamp以及對象reqGetTpUserObjectsInfo;所以在我看來,或者有一些錯誤(也許是另一個WSDL),或者在文檔中有一些與安全有關的事情(例如ws-security);在ws-security的情況下,有一些參數類似於你編寫的參數,但它們是用soap標題寫的;你也可以通過使用SOAP-UI來測試WS來獲得與spring-ws相同的結果 –

回答

0

,因爲您提供的樣品不reagard肥皂行動似乎有點怪;但正如我在示例中看到的,有一些參數添加到肥皂主體中,並且這些參數未映射到WS模式中

在任何情況下,如果文檔說soap操作字符串必須具有這些參數,您仍然可以用你用什麼,但你必須通過屬性的SoapActionCallback: 例如,你可以做以下

wsTemplate.marshalSendAndReceive("wsUri", youRequestObj, new SoapActionCallback("verifySignature callId=\""+callId+"\" requestTimestamp=\""+requestTimestamp+"\"")); 

這樣春天WS將通過增加2寫肥皂action屬性

但我假定它是要修改的肥皂體內容;所以在這種情況下,你可以使用:

  • org.springframework.ws.client.core.WebServiceTemplate
  • 的WebServiceTemplate的sendSourceAndReceive方法
  • 您的自定義SourceExtractor

例如,你可以使用這樣的XML模板(通過使用速度完成)並且稱爲「requestTemplate.vm」

<?xml version="1.0" encoding="UTF-8" ?> 
<tpus:verifySignature callId="${callId}" requestTimestamp="${timeStamp}" xmlns:tpus="http://verification.zp.epuap.gov.pl"> 
     <tpus:doc>${doc}</tpus:doc> 
      <tpus:attachments> 
       <tpus:Attachment> 
        <tpus:content>${docContent}</tpus:content> 
         <tpus:name>${docName}</tpus:name> 
       </tpus:Attachment> 
      </tpus:attachments> 
    </tpus:verifySignature> 

然後在你的代碼,你可以做這樣的事情:

Map<String, Object> params = new HashMap<String, Object>(5); 
params.put("callId", "myCallId"); 
params.put("timeStamp", "thetimeStamp"); 
params.put("doc", "theDoc"); 
params.put("docName", "theDocName"); 
params.put("docContent", "theDocContent"); 
String xmlRequest = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "requestTemplate.vm", "UTF-8", params).replaceAll("[\n\r]", ""); 
StreamSource requestMessage = new StreamSource(new StringReader(xmlRequest)); 
wsTemplate.sendSourceAndReceive("wsUrl", requestMessage,new new SoapActionCallback("verifySignature"),new CustomSourceExtractor()); 

其中CustomSourceExtractor在這裏你可以閱讀SOAP響應

我做了這樣的事情

public class VieSourceExtractor implements SourceExtractor<YourObj> 
{ 
@Override 
public List<YourObj> extractData(Source src) throws IOException, TransformerException 
{ 
XMLStreamReader reader = null; 
try 
{ 
reader = StaxUtils.getXMLStreamReader(src); 
//read the xml and create your obj 
return yourResult; 
} 
catch (Exception e) 
{ 
throw new TransformerException(e); 
} 
finally 
{ 
if (reader != null) 
{ 
try 
{ 
reader.close(); 
} 
catch (XMLStreamException e) 
{ 
logger.error("Error " + e.getMessage(), e); 
} 
} 
} 
} 
} 

我希望這可以幫助你

安傑洛

+0

不幸的是,寫入''verifySignature callId = \「」+ callId +「\」requestTimestamp = \「」+ requestTimestamp +「\」「'to SoapActionCallback returned with:'給定的SOAPAction verifySignature callId =」6347177294896046332「requestTimestamp =」2016-11 -14T10:14:24.524 + 01:00與操作不匹配,我檢查了發送的請求,xml沒有被修改,所以不可能修改這樣的soap動作,我會嘗試你的其他提示:) – Dario3d

+0

我很確定這是XML身體被改變,就像我告訴你的...讓我知道...現在我很好奇;順便說一句...我寫的虛擬機模板必須改進...它只是一個樣本 –

+0

我最終修改了java spring生成的類,結果證明這些參數在文檔中,但文檔已過時...感謝您的幫助 – Dario3d