在Spring WS中向SOAP头添加自定义元素

4
我正在使用Spring WS和JAXB绑定编写Web服务客户端应用程序。我使用的服务需要通过SOAP头中的wsse:security元素(以及另一个自定义标头)进行身份验证。我已经编译了所有必要的模式,包括wsse.xsd编译为org.xmlsoap.schemas.ws._2002._12.secext.Security。
然而,我找不到将此或任何其他元素插入SOAP头的方法。我知道可以使用拦截器或SoapActionCallback,但它们只允许我手动构建头并通过((SaajSoapMessage)webServiceMessage).getSoapHeader().addHeaderElement(qName)等方式将其添加到头部部分。但是我不想手动构建此头,因为我有一个相应的类可以轻松地进行编组。
我的问题是 - 在Spring WS中调用Web服务调用时是否有一种将对象插入SOAP头(或信封的其他部分)的方法?我使用Apache CXF和Axis2来消耗Web服务,这从未成为问题 - 框架通常通过服务存根机制在幕后为我完成。

1
你有查看过 http://docs.spring.io/spring-ws/site/reference/html/security.html 吗?据我所记,你需要定义一个 Wss4jSecurityInterceptor(它使用与 CXF、WSS4J 相同的 WS Security 库),配置正确的操作,然后它会为你完成工作... - GPI
@GPI 谢谢您的回复。是的,我看到了那个,但那不是我要找的(我编辑了我的帖子和标题)。安全头只是一个例子,我的服务使用了另一个自定义头(eb:MessageHeader)。再次说明,我有相应的模式编译为Java类,但我无法将其插入消息中。Wss4jSecurityInterceptor只是从头开始创建头的另一种方式,而我有完全组合的对象,我想将其插入头部。 - nachteil
看起来没有简单的方法。请注意,由于您将模式编译为对象,因此肯定有一种方法可以让JAXB生成DOM树并将其插入标头。请参见:https://dev59.com/eWQn5IYBdhLWcg3wIUJn - GPI
1个回答

4
我已经设法解决了这个问题,感谢@GPI提供的提示。我对Spring WS和javax.xml.whatever都比较新,所以无法确定这是正确或优雅的方式,但它确实做到了我想要的效果。
此代码基于通过JAXB从XSD模式生成的对象将自定义头元素添加到中。我不知道转换器如何知道我想把这些元素放在哪里,但它会将它们正确地放在头部节中。
public class HeaderComposingCallback implements WebServiceMessageCallback {

    private final String action;

    public HeaderComposingCallback( String action ) {
        this.action = action;
    }

    @Override
    public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {

    SoapHeader soapHeader = ((SoapMessage)webServiceMessage).getSoapHeader();

    try {
        JAXBContext context = JAXBContext.newInstance( MessageHeader.class, Security.class );

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document securityDocument = builder.newDocument();
        Document headerDocument = builder.newDocument();

        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal( HeaderFactory.getHeader( action ), headerDocument );
        marshaller.marshal( SecurityFactory.getSecurity(), securityDocument );

        Transformer t = TransformerFactory.newInstance().newTransformer();

        DOMSource headerSource = new DOMSource( headerDocument );
        DOMSource securitySource = new DOMSource( securityDocument );

        t.transform( headerSource, soapHeader.getResult() );
        t.transform( securitySource, soapHeader.getResult() );

    } catch (JAXBException | ParserConfigurationException e) {
        e.printStackTrace();
    }
}

}

然后,在服务调用期间,我只需要将HeaderComposingCallback对象传递给marshalSendAndReceive()方法即可。

编辑(根据Arjen的评论)

Arjen是正确的。我想要做的事情可以更简单地实现。现在我的doWithMessage方法如下所示:

    @Override
    public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {

    SoapHeader soapHeader = ((SoapMessage)webServiceMessage).getSoapHeader();

    try {
        JAXBContext context = JAXBContext.newInstance( MessageHeader.class, Security.class );

        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal( header, soapHeader.getResult() );
        marshaller.marshal( security, soapHeader.getResult() );

    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

2
不确定为什么需要在此处创建文档,为什么不直接转换到头文件的结果中呢?如:marshaller.marshal(marshaller.marshal(HeaderFactory.getHeader(action), soapHeader.getResult())。此外,您可能需要考虑将Spring OXM Jaxb2Marshaller配置连接并注入回调类中。这将为您保存创建JAXB上下文和编组器的麻烦。 - Arjen Poutsma
@ArjenPoutsma 你是正确的,谢谢,我编辑了我的帖子。 - nachteil
marshaller.marshal(header, soapHeader.getResult()); marshaller.marshal(security, soapHeader.getResult()); header和security - 这些是类实例吗? - Preety Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接