CXF中的拦截器

参考:http://blog.csdn.net/lzwjavaphp/article/details/14224899

1.框架自带的拦截器

服务器端日志拦截器

package com.hous.test;
import java.util.List;
import javax.xml.ws.Endpoint;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.message.Message;
import com.hous.service.IBook;
import com.hous.service.impl.BookImpl;

public class MyTest {
	public static void main(String[] args) {
		String address = "http://127.0.0.1:10086/Service/book";
		IBook book =  new BookImpl(); 
		Endpoint endpoint = Endpoint.publish(address, book);
		EndpointImpl endpointImpl = (EndpointImpl) endpoint;
		//入拦截器
		List<Interceptor<? extends Message>> inInterceptors = endpointImpl.getInInterceptors();
		inInterceptors.add(new LoggingInInterceptor());
		//出拦截器
		List<Interceptor<? extends Message>> outInterceptors = endpointImpl.getOutInterceptors();
		outInterceptors.add(new LoggingOutInterceptor());
		
		System.out.println("发布成功...");
	}
}

客户端日志拦截器

import java.util.List;

import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.message.Message;
import org.apache.cxf.endpoint.Client;

import com.hous.service.impl.BookImplService;
import com.hous.service.impl.IBook;

public class MyClient {
	public static void main(String[] args) {
		BookImplService factory = new BookImplService();
		IBook bookImplPort = factory.getBookImplPort();
		Client client = ClientProxy.getClient(bookImplPort);
		//入拦截器
		List<Interceptor<? extends Message>> inInterceptors = client.getInInterceptors();
		inInterceptors.add(new LoggingInInterceptor());
		//出拦截器
		List<Interceptor<? extends Message>> outInterceptors = client.getOutInterceptors();
		outInterceptors.add(new LoggingOutInterceptor());
		
		System.out.println(bookImplPort.getBookName());
	}
}

2.自定义拦截器

服务器端认证拦截器

package com.hous.interceptor;
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;  
  
public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {  
  
    public AuthInterceptor() {  
        //拦截器在调用方法之前拦截SOAP消息  
        super(Phase.PRE_PROTOCOL);
    }  
      
    /** 
     * @Description: 拦截器操作 
     * @param msg 被拦截到的SOAP消息 
     * @throws Fault 
     */  
    @Override  
    public void handleMessage(SoapMessage msg) throws Fault {  
          
        System.out.println("=====自定义拦截器=======");  
        //获取SOAP消息的Header
        List<Header> headers = msg.getHeaders();  
        //如果没有Header  
        if(headers == null || headers.size() < 1) {  
            throw new Fault(new IllegalArgumentException("没有Header,拦截器实施拦截"));  
        }  
        //获取Header携带是用户和密码信息  
        Header firstHeader = headers.get(0);  
        Element ele = (Element) firstHeader.getObject();  
          
        NodeList userNameEle = ele.getElementsByTagName("userName");  
        NodeList passwordEle = ele.getElementsByTagName("password");  
          
        if (userNameEle.getLength() != 1) {  
            throw new Fault(new IllegalArgumentException("用户名格式不对"));  
        }  
              
        if (passwordEle.getLength() != 1) {  
            throw new Fault(new IllegalArgumentException("用户密码格式不对"));  
        }  
          
        //获取元素的文本内容  
        String userName = userNameEle.item(0).getTextContent();  
        String password = passwordEle.item(0).getTextContent();  
        //错误只能异常抛出
        if (!userName.equals("shanshanbox") || !password.equals("123456")) {  
            throw new Fault(new IllegalArgumentException("用户和密码不正确"));  
        }  
    }  
  
}  

客户端认证拦截器

package com.hous.interceptor;

import java.util.List;  
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;  
  
public class AddHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage>{  
      
    private String userName;  
    private String password;  
      
    public AddHeaderInterceptor(String userName, String password) {  
        super(Phase.PREPARE_SEND);  
        this.userName = userName;  
        this.password = password;  
    }  
  
    @Override  
    public void handleMessage(SoapMessage msg) throws Fault {  
          
        List<Header> headers = msg.getHeaders();  
        //创建Document对象  
        Document doc = DOMUtils.createDocument();  
        Element ele = doc.createElement("authHeader");  
        //配置服务器端Head信息的用户密码  
        Element eleId= doc.createElement("userName");  
        eleId.setTextContent(userName);  
        Element elePass = doc.createElement("password");  
        elePass.setTextContent(password);  
          
        ele.appendChild(eleId);  
        ele.appendChild(elePass);  
        /** 
         * 生成的XML文档 
         * <authHeader> 
         *      <userName>shanshanbox</userName> 
         *      <password>123456</password> 
         * </authHeader> 
         */  
        headers.add(new Header(new QName(""), ele));  
    }  
  
}

 别忘了添加到拦截器组中

猜你喜欢

转载自shuizhongyue.iteye.com/blog/2290657