@WebService @HandlerChain public class UserService { ... }
package com.xx.ws.header; import org.w3c.dom.Node; import javax.xml.namespace.QName; import javax.xml.soap.*; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; import javax.xml.ws.handler.soap.SOAPMessageContext; import javax.xml.ws.soap.SOAPFaultException; import java.util.Set; public class MySoapHandler implements SOAPHandler<SOAPMessageContext> { public boolean handleMessage(SOAPMessageContext context) { Boolean out = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (!out) { SOAPMessage message = context.getMessage(); try { SOAPHeader header = message.getSOAPPart().getEnvelope().getHeader(); if (header != null) { Node userNameNode = header.getElementsByTagName("wsse:Username").item(0); Node passwordNode = header.getElementsByTagName("wsse:Password").item(0); if ("admin".equals(userNameNode.getTextContent()) && "password".equals(passwordNode.getTextContent())) { return true; }
return false; } else { return false; } } catch (Exception e) { e.printStackTrace(); return false; } } return true; } @Override public boolean handleFault(SOAPMessageContext context) { return true; } @Override public void close(MessageContext context) { } @Override public Set<QName> getHeaders() { return null; } }