SOAPFaultException “MustUnderstand headers (oasis-200401-wss-wssecurity-secext-1.0.xsd) are not understood”

前端 未结 4 927
我寻月下人不归
我寻月下人不归 2020-12-14 18:32

I try to get information from web service that uses PasswordText WSS type. Firstly, I test it using soapUI and successfully got data. Then I implemented authentication on Ja

4条回答
  •  Happy的楠姐
    2020-12-14 19:09

    Here is what worked for me. Basically, it's an application of the idea pronounced by @Joseph Rajeev Motha (although I found it elsewhere, here: https://dwuysan.wordpress.com/2012/04/02/jax-ws-wsimport-and-the-error-mustunderstand-headers-not-understood/#comment-215 ), but his answer does not provide boilerplate, and without it, the answer is pretty mysterious.

    Please note that this sequence applies to the standalone case (where you publish an Endpoint yourself).

    Step 1

    Create a SOAPHandler that will 'understand' the header:

    public class WSSESecurityUnderstandPretender implements SOAPHandler {
        @Override
        public Set getHeaders() {
            final QName securityHeader = new QName(
                "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
                "Security",
                "wsse");
    
            final Set headers = new HashSet<>();
            headers.add(securityHeader);
    
            // notify the runtime that this is handled
            return headers;
        }
    
        @Override
        public boolean handleMessage(SOAPMessageContext context) {
            // we must return true, or else the runtime will return
            // wrong wrapper element name (like makeTransfer instead of
            // makeTransferResponse)
            return true;
        }
    
        @Override
        public boolean handleFault(SOAPMessageContext context) {
            // we must return true, or else the runtime will return
            // wrong wrapper element name (like makeTransfer instead of
            // makeTransferResponse)
            return true;
        }
    
        @Override
        public void close(MessageContext context) {
        }
    }
    

    Step 2

    Create a handler-chain.xml file and put it on classpath:

    
    
      
        
          com.mypackage.WSSESecurityUnderstandPretender
        
      
    
    

    Step 3

    Annotate your implementation class (class that is annotated with @WebService) with a reference to the handler chain file:

    @HandlerChain(file = "handler-chain.xml")
    

    Step 4

    Publish your endpoint:

    Endpoint endpoint = Endpoint.publish(url, impl);
    

    An important note

    handleMessage() and handleFault() defined by the handler must return true. Otherwise, you will get strange errors like 'Unexpected wrapper element' because a different wrapper element name will be used.

提交回复
热议问题