2016-11-10 78 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