Axis2访问验证(SOAP方式)

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;
   }
}
}

深圳人才网 深圳招聘网 深圳人才招聘网 深圳人才大市场 

企业、个人免费注册,获取想要的 深圳 软件工程师招聘信息 月薪最低3000-8000,更有高端猎头职位! 

www.szrcwz.com                                                                                                                                           

                  Google 提供的广告

posted @ 2010-09-26 17:21  栈长  阅读(210)  评论(0编辑  收藏  举报