简单的WebService 拦截器进行身份验证

服务端新增一个 获得请求的拦截器

import java.util.List;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * In拦截器
 * @author lin
 *
 */
class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage>
{
    public AuthInterceptor()
    {
        //显示调用父类的构造器,在发送消息之前调用,in拦截器
        super(Phase.PRE_INVOKE);
    }


    @Override
    public void handleMessage(SoapMessage msg) throws Fault
    {
        // 得到soap消息的所有header消息
        List<Header> headers = msg.getHeaders();
        if (headers.size() < 1 || headers.isEmpty())
        {
            throw new Fault(new IllegalArgumentException("没有header"));
        }

        for(Header header:headers){
            Element element= (Element) header.getObject();
            NodeList userId = element.getElementsByTagName("userId");
            String user = userId.item(0).getTextContent();
            if(user==null){
                throw new Fault(new IllegalArgumentException("用户名格式不正确"));
            }
            if(!"aa".equals(user)){
                throw new Fault(new IllegalArgumentException("用户名不正确"));
            }
            NodeList passId = element.getElementsByTagName("userPass");
            String pass = passId.item(0).getTextContent();
            if(pass==null){
                throw new Fault(new IllegalArgumentException("密码格式不正确"));
            }
            if(!"123".equals(pass)){
                throw new Fault(new IllegalArgumentException("密码不正确"));
            }
        }
    }


}

服务端进行发布:endpoint.publish(),同时新增拦截器

import javax.xml.ws.Endpoint;

import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.phase.Phase;

public class Server {

    protected Server() throws Exception {
        // START SNIPPET: publish
        System.out.println("Starting Server");
        HelloServiceImpl implementor = new HelloServiceImpl();
        String address = "http://localhost:8111/helloWorld";

        //有header 身份验证
        EndpointImpl publish =(EndpointImpl) Endpoint.publish(address, implementor);
        publish.getInInterceptors().add(new AuthInterceptor());

        //无header 验证
//        Endpoint.publish(address, implementor);
    }

    public static void main(String[] args) throws Exception {
        new Server();
        System.out.println("Server ready...");

    }
}

 

java 客户端访问新增out 拦截器 

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * soap消息分两部分,第一部分Header中携带身份等信息,第二部分body里面携带具体的操作信息
 * AbstractPhaseInterceptor<SoapMessage>中的SoapMessage就是操作其Header信息,
 * 在Header中添加身份信息。
 * @author xushuai
 *    Out拦截器
 */
class AddHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage>
{

	private String userId;
	private String pwd;

	public AddHeaderInterceptor(String userid, String pwd)
	{
		// 准备发送消息的时候启用该拦截器
		super(Phase.PREPARE_SEND);
		this.pwd = pwd;
		this.userId = userid;
	}

	@Override
	public void handleMessage(SoapMessage mgs) throws Fault
	{
		List<Header> headers = mgs.getHeaders();

		Document doc = DOMUtils.createDocument();
		/**
		 * <authElement>
		 *    <userId></userId>
		 *    <userPass></userPass>
		 * </authElement>
		 */
		Element ele = doc.createElement("authElement");
		Element useridEle = doc.createElement("userId");
		useridEle.setTextContent(userId);
		Element pwdEle = doc.createElement("userPass");
		pwdEle.setTextContent(pwd);

		ele.appendChild(useridEle);
		ele.appendChild(pwdEle);
		System.err.println(ele.getTagName());
		headers.add(new Header(new QName("web"),ele));
	}

}

 

Soap格式访问:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.cdyne.com/">
   <soapenv:Header>
   	<authElement>
	   	<userId>aa</userId>
	   	<userPass>123</userPass>
   	</authElement>
   </soapenv:Header>
   <soapenv:Body>
      <ws:sayHi>
         <!--Optional:-->
         <ws:name>2</ws:name>
      </ws:sayHi>
   </soapenv:Body>
</soapenv:Envelope>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值