Spring WS如何获取服务器端soap响应xml以添加带符号的soap头

时间:2022-06-04 20:17:05

I have spring web-service. I need to sign soap response that server(my web service) give for the soap request. xwss and wss4j interceptors not suitable for this, because i need this format:

我有春天的web服务。我需要签署服务器(我的web服务)为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>

I need to add this to soap header block in my response. To do this i have to get xml of whole soap response including soap env , etc.

我需要在响应中将其添加到soap头块。为此,我必须获得整个soap响应的xml,包括soap env等。

How to get that xml, modify it by adding soap header, and at last send it as response?

如何获得那个xml,通过添加soap头来修改它,最后作为响应发送它?

Plz help me

请帮助我

2 个解决方案

#1


4  

Ok, I finally solved the problem with custom SmartSoapEndpointInterceptor. I created MyCustomEndpointInterceptor that implements SmartSoapEndpointInterceptor and changed handleResponse for my needs. Sourcecode is:

好的,我最终用定制的SmartSoapEndpointInterceptor解决了这个问题。我创建了MyCustomEndpointInterceptor,它实现SmartSoapEndpointInterceptor,并根据我的需要更改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;
    }
}

#2


0  

with spring-ws you can add MessageContext messageContext parameter to any method that handle a soap request:

使用spring-ws,您可以向任何处理soap请求的方法添加MessageContext参数:

@PayloadRoot(localPart = LOCAL_PART, namespace = NAMESPACE)
@ResponsePayload
public <return type> handleRequest(@RequestPayload final <request type> request,
                                         MessageContext messageContext)

With the message context you can retrieve the response header and edit it as you like.

使用消息上下文,您可以检索响应头并按自己的喜好编辑它。

#1


4  

Ok, I finally solved the problem with custom SmartSoapEndpointInterceptor. I created MyCustomEndpointInterceptor that implements SmartSoapEndpointInterceptor and changed handleResponse for my needs. Sourcecode is:

好的,我最终用定制的SmartSoapEndpointInterceptor解决了这个问题。我创建了MyCustomEndpointInterceptor,它实现SmartSoapEndpointInterceptor,并根据我的需要更改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;
    }
}

#2


0  

with spring-ws you can add MessageContext messageContext parameter to any method that handle a soap request:

使用spring-ws,您可以向任何处理soap请求的方法添加MessageContext参数:

@PayloadRoot(localPart = LOCAL_PART, namespace = NAMESPACE)
@ResponsePayload
public <return type> handleRequest(@RequestPayload final <request type> request,
                                         MessageContext messageContext)

With the message context you can retrieve the response header and edit it as you like.

使用消息上下文,您可以检索响应头并按自己的喜好编辑它。