How to add header to SOAP request?

前端 未结 4 949
攒了一身酷
攒了一身酷 2020-12-04 13:58

I try to invoke HTTPS SOAP web service through java code:

    URL url = new URL(\"https://somehost:8181/services/\"SomeService?wsdl\");
    QName qname = new         


        
4条回答
  •  难免孤独
    2020-12-04 14:06

    You can also use Apache wss4j to easily add the header and also encrypt you password.

    import org.apache.ws.security.WSConstants;
    import org.apache.ws.security.message.WSSecHeader;
    import org.apache.ws.security.message.WSSecUsernameToken;
    
    import javax.xml.namespace.QName;
    import javax.xml.soap.SOAPMessage;
    import javax.xml.soap.SOAPPart;
    import javax.xml.ws.handler.MessageContext;
    import javax.xml.ws.handler.soap.SOAPHandler;
    import javax.xml.ws.handler.soap.SOAPMessageContext;
    import java.io.ByteArrayOutputStream;
    import java.util.Set;
    
    public class WSSecurityHeaderSOAPHandler implements SOAPHandler {
    
    
        private final String usernameText;
        private final String passwordText;
    
        public WSSecurityHeaderSOAPHandler(String usernameText, String passwordText) {
            this.usernameText = usernameText;
            this.passwordText = passwordText;
        }
    
        @Override
        public boolean handleMessage(SOAPMessageContext context) {
            Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    
            if (outboundProperty.booleanValue()) {
    
                try {
                    SOAPMessage soapMessage = context.getMessage();
                    soapMessage.removeAllAttachments();
    
                    SOAPPart soappart = soapMessage.getSOAPPart();
                    WSSecHeader wsSecHeader = new WSSecHeader();
                    wsSecHeader.insertSecurityHeader(soappart);
                    WSSecUsernameToken token = new WSSecUsernameToken();
                    token.setPasswordType(WSConstants.PASSWORD_DIGEST);
                    token.setUserInfo(usernameText, passwordText);
                    token.build(soappart, wsSecHeader);
    
                    soapMessage.saveChanges();
                } catch (Exception e) {
                    throw new RuntimeException("Error on wsSecurityHandler: " + e.getMessage());
                }
    
            }
    
            return true;
        }
    
        @Override
        public boolean handleFault(SOAPMessageContext context) {
            return false;
        }
    
        @Override
        public void close(MessageContext context) {
    
        }
    
        @Override
        public Set getHeaders() {
            return null;
        }
    }
    

    And you need to update your request like this:

    // This is the block that apply the Ws Security to the request
    BindingProvider bindingProvider = (BindingProvider) portType;
    List handlerChain = new ArrayList<>();
    handlerChain.add(new WSSecurityHeaderSOAPHandler("username", "password"));
    bindingProvider.getBinding().setHandlerChain(handlerChain);
    

    Maven Dependency:

            
                org.apache.ws.security
                wss4j
                1.6.19
            
    

提交回复
热议问题