简体   繁体   English

如何在 Spring Integration 中设置肥皂头?

[英]How to set soap header in Spring Integration?

I want to send a soap message with Spring Integration.我想用 Spring Integration 发送一个 soap 消息。 I use Java Config.我使用 Java 配置。 I've tried the flolowing interceptor, but the spring integration converts the angle brackets (<>) into html escape characters.我已经尝试过 flolowing 拦截器,但是 spring 集成将尖括号 (<>) 转换为 html 转义字符。

import org.springframework.ws.client.WebServiceClientException;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapHeaderElement;
import org.springframework.ws.soap.SoapMessage;

public class MyAuthInterceptor implements ClientInterceptor {
    @Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
        SoapMessage soapMessage = (SoapMessage) messageContext.getRequest();
        SoapHeader sh = soapMessage.getSoapHeader();
        QName name = new QName("http://...", "myAuth", "aut");
        sh.addHeaderElement(name).setText("<username>TestUser</username>" + "<password>TestPass</password>");
        return true;
    }

Here is the generated soap header:这是生成的肥皂头:

<SOAP-ENV:Header>
    <aut:myAuth xmlns:aut="http://.../">&lt;username&gt;TestUser&lt;/username&gt;&lt;password&gt;TestPass&lt;/password&gt;</aut:myAuth>
</SOAP-ENV:Header>

Here is my configuration:这是我的配置:

@Configuration
@EnableIntegration
public class SpringIntegrationConfiguration {

    @Bean
    public PublishSubscribeChannel inputChannel() {
        return new PublishSubscribeChannel();
    }

    @Bean
    public ClientInterceptor myAuthInterceptor() {
        return new MyAuthInterceptor();
    }

    @Bean
    @ServiceActivator(inputChannel = "inputChannel")
    public SimpleWebServiceOutboundGateway myOutput(ClientInterceptor mekAuthInterceptor) {
        SimpleWebServiceOutboundGateway simpleWebServiceOutboundGateway = new SimpleWebServiceOutboundGateway("http://...");
        simpleWebServiceOutboundGateway.setInterceptors(myAuthInterceptor);
        return simpleWebServiceOutboundGateway;
    }
}

How can I set the soap header without escaping the angle brackets?如何在不转义尖括号的情况下设置肥皂头?

您必须使用addChildElement来构建它,而不是将其设置为文本。

You are setting the tags as text which escapes the string since it is being added to an xml.您将标签设置为文本,因为字符串被添加到 xml 中,因此该文本会转义字符串。 Those needs to be set as elements那些需要设置为元素

http://docs.oracle.com/javaee/5/api/javax/xml/soap/SOAPHeaderElement.html http://docs.oracle.com/javaee/5/api/javax/xml/soap/SOAPHeaderElement.html

Check the methods in the doc above and use it appropriately.检查上面文档中的方法并适当地使用它。 Leave a comment in case you need more help.如果您需要更多帮助,请发表评论。

@gary-russell - Your answer made sense from a logical point of view, but there are no "addChildElement" method in org.springframework.ws.soap. @gary-russell - 从逻辑的角度来看,您的回答是有道理的,但 org.springframework.ws.soap 中没有“addChildElement”方法。 . . I found them in javax.xml.soap.我在 javax.xml.soap 中找到了它们。 . .

So my result looks like the following:所以我的结果如下所示:

import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ws.client.WebServiceClientException;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessage;

public class MySecurityInterceptor implements ClientInterceptor
{
    private static final Logger log = LoggerFactory.getLogger(MySecurityInterceptor.class);

    @Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException
    {
        SoapMessage soapMessage = (SoapMessage) messageContext.getRequest();

        QName securityName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
            "Security",
            XMLConstants.DEFAULT_NS_PREFIX);

        QName usernameToken = new QName(null, "UsernameToken", XMLConstants.DEFAULT_NS_PREFIX);

        QName username = new QName(null, "Username", XMLConstants.DEFAULT_NS_PREFIX);

        QName password = new QName(null, "Password", XMLConstants.DEFAULT_NS_PREFIX);

        try
        {
            SOAPMessage mySoapMessage = ((SaajSoapMessage) soapMessage).getSaajMessage();

            SOAPHeader header = mySoapMessage.getSOAPHeader();

            SOAPHeaderElement securityElement = header.addHeaderElement(securityName);

            SOAPElement usernameTokenElement = securityElement.addChildElement(usernameToken);

            SOAPElement usernameElement = usernameTokenElement.addChildElement(username);

            SOAPElement passwordElement = usernameTokenElement.addChildElement(password);

            usernameElement.setTextContent("jxep-Zenmonics-1-i101@jxtest.local");
            passwordElement.setTextContent("3Sg%T~1q4z!QnH6#+5pD");
        }
        catch (SOAPException e)
        {
            log.error("Error!", e);
        }

        return true;
    }

    @Override
    public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException
    {
        // Auto-generated method stub
        return false;
    }

    @Override
    public boolean handleFault(MessageContext messageContext) throws WebServiceClientException
    {
        // Auto-generated method stub
        return false;
    }

    @Override
    public void afterCompletion(MessageContext messageContext, Exception ex) throws WebServiceClientException
    {
        // Auto-generated method stub
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM