How to properly format a SOAP message envelope using a custom SOAPHandler

后端 未结 3 667
花落未央
花落未央 2021-02-06 14:31

I have a class that implements the SOAPHandler interface. The handleMessage is defined as:

public boolean handleMessage(SOAPMessageContext context) {

  SOAPMes         


        
相关标签:
3条回答
  • 2021-02-06 14:57

    Just posting my solution if someone is still wondering --

    Name name = soapenv.createName("Security", "sse", "URL");
    SOAPHeaderElement security = soapenv.getHeader().addHeaderElement(name);
    security.setMustUnderstand(true);
    SOAPElement usernameToken = security.addChildElement("UsernameToken", "sse");
    SOAPElement username = usernameToken.addChildElement("Username", "sse");
    username.addTextNode("TestUser");
    SOAPElement password = usernameToken.addChildElement("Password", "sse");
    password.addTextNode("TestPassword");
    
    0 讨论(0)
  • 2021-02-06 14:58

    There's enough problems in this code that I'm thinking it's a troll, but heres a start:

    The line :

    element = envelope.addHeader().addChildElement(qname);
    

    should read:

    SOAPHeaderElement secHdrElement = envelope.addHeader().addHeaderElement(qname);
    

    next, instead of:

    qname = envelope.createName("mustUnderstand");
    element.addAttribute(qname, "1");
    

    probably:

    secHdrElement.setMustUnderstand(true);
    

    and

    qname = envelope.createName("UsernameToken", "sse", "http://example.com/user.xsd");
    element = envelope.getHeader().addHeaderElement(qname);
    element.addTextNode("user1");
    

    should be something like:

    qname = envelope.createName("UsernameToken", "sse", "http://example.com/user.xsd");
    element = secHdrElement.addHeaderElement(
              envelope.createName("UsernameToken", "sse", "http://example.com/user.xsd"));
    

    and so on...

    0 讨论(0)
  • 2021-02-06 15:05

    This is taken from my working handler. Hope it works for you.

    public static final String WSSE_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
    public static final String PASSWORD_TEXT_TYPE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText";
    public static final String WSSE_SECURITY_LNAME = "Security";
    public static final String WSSE_NS_PREFIX = "wsse";
    
    private String username;
    private String password;
    private boolean mustUnderstand = false;
    
    public boolean handleMessage(SOAPMessageContext messageContext) {
        Object bOutbound = messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (bOutbound == Boolean.TRUE) {
            try {
                if (username != null && username.length() != 0) {
                    addSecurityHeader(messageContext);
                    LOG.debug("Added security header");
                } else {
                    LOG.debug("No username configured thus not adding a security header");
                }
            } catch (Exception e) {
                LOG.error("Exception in handleMessage", e);
                return false;
            }
        }
        return true;
    }
    
    private void addSecurityHeader(SOAPMessageContext messageContext) throws SOAPException {
        SOAPFactory sf = SOAPFactory.newInstance();
        SOAPHeader header = messageContext.getMessage().getSOAPPart().getEnvelope().getHeader();
        if (header == null) {
            header = messageContext.getMessage().getSOAPPart().getEnvelope().addHeader();
        }
    
        Name securityName = sf.createName(WSSE_SECURITY_LNAME, WSSE_NS_PREFIX, WSSE_NS);
        SOAPHeaderElement securityElem = header.addHeaderElement(securityName);
        securityElem.setMustUnderstand(mustUnderstand);
    
        Name usernameTokenName = sf.createName("UsernameToken", WSSE_NS_PREFIX, WSSE_NS);
        SOAPElement usernameTokenMsgElem = sf.createElement(usernameTokenName);
    
        Name usernameName = sf.createName("Username", WSSE_NS_PREFIX, WSSE_NS);
        SOAPElement usernameMsgElem = sf.createElement(usernameName);
        usernameMsgElem.addTextNode(username);
        usernameTokenMsgElem.addChildElement(usernameMsgElem);
    
        Name passwordName = sf.createName("Type", WSSE_NS_PREFIX, WSSE_NS);
        SOAPElement passwordMsgElem = sf.createElement("Password", WSSE_NS_PREFIX, WSSE_NS);
    
        passwordMsgElem.addAttribute(passwordName, PASSWORD_TEXT_TYPE);
        passwordMsgElem.addTextNode(password);
        usernameTokenMsgElem.addChildElement(passwordMsgElem);
    
        securityElem.addChildElement(usernameTokenMsgElem);
    }
    
    0 讨论(0)
提交回复
热议问题