JAVA调用 web service时加入下面:
call.addHeader(getSoapHeader(qName));
public static SOAPHeaderElement getSoapHeader(QName qName){ Name headerName = new PrefixedQName(qName); SOAPHeaderElement header = new SOAPHeaderElement(headerName); // header.setActor(null); try{ header.addChildElement("username").addTextNode("admin"); header.addChildElement("password").addTextNode("admin"); }catch(Exception e){ e.printStackTrace(); } return header; }
服务器端方法里面调用这个类的check方法:
import java.util.Iterator; import org.apache.axiom.om.OMElement; import org.apache.axiom.soap.SOAPHeader; import org.apache.axis2.context.MessageContext;
public class SoapHeaderUtil {
@SuppressWarnings("unchecked") public static boolean check(){ MessageContext msgContext = MessageContext.getCurrentMessageContext(); SOAPHeader header = msgContext.getEnvelope().getHeader(); if(header == null){ return false; } Iterator list = (Iterator)header.getFirstElement().getChildren(); String username = ""; String password = ""; while (list.hasNext()) { OMElement element = (OMElement) list.next(); if (element.getLocalName().equals("username")) { username = element.getText(); } if (element.getLocalName().equals("password")) { password = element.getText(); } } if (username.equals("qinan") && password.equals("admin")){ return true; }else { return false; } } }
|
关注微信公众号福利!!!
回复关键字「666」获取一份最新 Java 架构资料,你要的都有!
回复关键字「Java」获取JVM, 多线程等Java技术系列教程;
回复关键字「spring」获取Spring, Spring Boot, Spring Cloud教程;
回复关键字「架构」获取分布式、微服务、架构、高并发等系列干货;
回复关键字「面试」获取各种 Java 面试题及答案、面试实战经验;