Spring WS 如何获取服务器端 soap 响应 xml 用于添加带符号的 soap header
Spring WS how to get server side soap response xml for adding soap header with sign
我有springweb-service。我需要签署服务器(我的网络服务)为 soap 请求提供的 soap 响应。 xwss 和 wss4j 拦截器不适合这个,因为我需要这种格式:
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#gost34310-gost34311"/>
<ds:Reference URI="#b0525e8a-dbcb-45da-abfd-d1bdecf6ccbb">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#gost34311"/>
<ds:DigestValue>valval=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>
valuevalue==
</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>
valuevaluevalue=
</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
我需要在我的回复中将其添加到 soap header 块中。为此,我必须获得 xml 整个 soap 响应,包括 soap env 等
如何获取 xml,通过添加 soap header 对其进行修改,最后将其作为响应发送?
请帮助我
使用 spring-ws 您可以将 MessageContext messageContext 参数添加到任何处理 soap 请求的方法:
@PayloadRoot(localPart = LOCAL_PART, namespace = NAMESPACE)
@ResponsePayload
public <return type> handleRequest(@RequestPayload final <request type> request,
MessageContext messageContext)
使用消息上下文,您可以检索响应 header 并根据需要进行编辑。
好的,终于用自定义的SmartSoapEndpointInterceptor解决了这个问题。我创建了实现 SmartSoapEndpointInterceptor 的 MyCustomEndpointInterceptor 并根据我的需要更改了 handleResponse。源代码是:
public class MyCustomEndpointInterceptor implements SmartSoapEndpointInterceptor {
@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
return true;
}
@Override
public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext.getResponse();
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(strMsg);
Document doc = soapResponse.getDocument();
// Look for the SOAP header
Element headerElement = null;
NodeList nodes = doc.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Header");
if (nodes.getLength() == 0) {
System.out.println("Adding a SOAP Header Element");
headerElement = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "Header");
nodes = doc.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Envelope");
if (nodes != null) {
Element envelopeElement = (Element) nodes.item(0);
headerElement.setPrefix(envelopeElement.getPrefix());
envelopeElement.appendChild(headerElement);
}
} else {
System.out.println("Found " + nodes.getLength() + " SOAP Header elements.");
headerElement = (Element) nodes.item(0);
}
Provider kncaProvider = new IolaProvider();
Security.addProvider(kncaProvider);
KeyStore ks = KeyStore.getInstance("pkcs12", kncaProvider.getName());
ks.load(new FileInputStream(keypath), keypass.toCharArray());
PrivateKey privateKey = (PrivateKey) ks.getKey(keyalias, keypass.toCharArray());
XMLSignature sig = new XMLSignature(doc, "", "http://www.w3.org/2001/04/xmldsig-more#gost34310-gost34311");
headerElement.appendChild(sig.getElement());
Transforms transforms = new Transforms(doc);
transforms.addTransform("http://www.w3.org/2000/09/xmldsig#enveloped-signature");
transforms.addTransform("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments");
sig.addDocument("", transforms,"http://www.w3.org/2001/04/xmldsig-more#gost34311");
X509Certificate cert = (X509Certificate) ks.getCertificate(somealias);
sig.addKeyInfo(cert);
sig.sign(privateKey);
return true;
}
@Override
public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {
return true;
}
@Override
public boolean shouldIntercept(MessageContext messageContext, Object endpoint) {
return true;
}
@Override
public boolean understands(SoapHeaderElement header) {
return true;
}
}
我有springweb-service。我需要签署服务器(我的网络服务)为 soap 请求提供的 soap 响应。 xwss 和 wss4j 拦截器不适合这个,因为我需要这种格式:
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#gost34310-gost34311"/>
<ds:Reference URI="#b0525e8a-dbcb-45da-abfd-d1bdecf6ccbb">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#gost34311"/>
<ds:DigestValue>valval=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>
valuevalue==
</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>
valuevaluevalue=
</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
我需要在我的回复中将其添加到 soap header 块中。为此,我必须获得 xml 整个 soap 响应,包括 soap env 等
如何获取 xml,通过添加 soap header 对其进行修改,最后将其作为响应发送?
请帮助我
使用 spring-ws 您可以将 MessageContext messageContext 参数添加到任何处理 soap 请求的方法:
@PayloadRoot(localPart = LOCAL_PART, namespace = NAMESPACE)
@ResponsePayload
public <return type> handleRequest(@RequestPayload final <request type> request,
MessageContext messageContext)
使用消息上下文,您可以检索响应 header 并根据需要进行编辑。
好的,终于用自定义的SmartSoapEndpointInterceptor解决了这个问题。我创建了实现 SmartSoapEndpointInterceptor 的 MyCustomEndpointInterceptor 并根据我的需要更改了 handleResponse。源代码是:
public class MyCustomEndpointInterceptor implements SmartSoapEndpointInterceptor {
@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
return true;
}
@Override
public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext.getResponse();
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(strMsg);
Document doc = soapResponse.getDocument();
// Look for the SOAP header
Element headerElement = null;
NodeList nodes = doc.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Header");
if (nodes.getLength() == 0) {
System.out.println("Adding a SOAP Header Element");
headerElement = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "Header");
nodes = doc.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Envelope");
if (nodes != null) {
Element envelopeElement = (Element) nodes.item(0);
headerElement.setPrefix(envelopeElement.getPrefix());
envelopeElement.appendChild(headerElement);
}
} else {
System.out.println("Found " + nodes.getLength() + " SOAP Header elements.");
headerElement = (Element) nodes.item(0);
}
Provider kncaProvider = new IolaProvider();
Security.addProvider(kncaProvider);
KeyStore ks = KeyStore.getInstance("pkcs12", kncaProvider.getName());
ks.load(new FileInputStream(keypath), keypass.toCharArray());
PrivateKey privateKey = (PrivateKey) ks.getKey(keyalias, keypass.toCharArray());
XMLSignature sig = new XMLSignature(doc, "", "http://www.w3.org/2001/04/xmldsig-more#gost34310-gost34311");
headerElement.appendChild(sig.getElement());
Transforms transforms = new Transforms(doc);
transforms.addTransform("http://www.w3.org/2000/09/xmldsig#enveloped-signature");
transforms.addTransform("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments");
sig.addDocument("", transforms,"http://www.w3.org/2001/04/xmldsig-more#gost34311");
X509Certificate cert = (X509Certificate) ks.getCertificate(somealias);
sig.addKeyInfo(cert);
sig.sign(privateKey);
return true;
}
@Override
public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {
return true;
}
@Override
public boolean shouldIntercept(MessageContext messageContext, Object endpoint) {
return true;
}
@Override
public boolean understands(SoapHeaderElement header) {
return true;
}
}