CXF自定义拦截器

服务器端:

main:

public static void main(String[] args) {
		System.out.println("start");
		
		HelloWorld implementor=new HelloWorldImpl();
		String address="http://192.168.77.1:8121/helloWorld";
		JaxWsServerFactoryBean jaxWsServerFactoryBean=new JaxWsServerFactoryBean();
		jaxWsServerFactoryBean.setAddress(address);
		jaxWsServerFactoryBean.setServiceBean(implementor);
		jaxWsServerFactoryBean.setServiceClass(HelloWorld.class);
		jaxWsServerFactoryBean.getInInterceptors().add(new LoggingInInterceptor());
		jaxWsServerFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
		
		jaxWsServerFactoryBean.getInInterceptors().add(new MyInterceptor());
		
		jaxWsServerFactoryBean.create();
		System.out.println("started");

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 MyInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
	public MyInterceptor() {
		super(Phase.PRE_INVOKE);//在调用方法之前拦截
	}
	public void handleMessage(SoapMessage message) throws Fault {
		List<Header> headers=message.getHeaders();
		if(headers==null && headers.size()==0){
			throw new Fault(new IllegalArgumentException("没有Header,拦截器实施拦截"));
		}
		Header firstHeader=headers.get(0);
		Element ele=(Element) firstHeader.getObject();
		NodeList uList=ele.getElementsByTagName("userName");
		NodeList pList=ele.getElementsByTagName("password");
		if(uList.getLength()!=1){
			throw new Fault(new IllegalArgumentException("用户名格式不对"));
		}
		if(pList.getLength()!=1){
			throw new Fault(new IllegalArgumentException("密码格式不对"));
		}
		String userName=uList.item(0).getTextContent();
		String password=pList.item(0).getTextContent();
		
		if(!userName.equals("yefan")||!password.equals("123456")){
			throw new Fault(new IllegalArgumentException("用户名或者密码错误!"));
		}
	}

}

客户端调用:

main:

                org.apache.cxf.endpoint.Client client=ClientProxy.getClient(helloWorld);
		client.getOutInterceptors().add(new AddHeaderInterceptor("yefan","123456")); // 添加自定义拦截器
		client.getInInterceptors().add(new LoggingInInterceptor()); // 添加In拦截器 日志拦截器
		client.getOutInterceptors().add(new LoggingOutInterceptor()); // 添加Out拦截器 日志拦截器

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); // 准备发送SOAP消息的时候调用拦截器
		this.userName=userName;
		this.password=password;
	}

	public void handleMessage(SoapMessage message) throws Fault {
		List<Header> headerList=message.getHeaders();
		
		Document doc=DOMUtils.createDocument();
		Element ele=doc.createElement("authHeader");
		Element uElement=doc.createElement("userName");
		uElement.setTextContent(userName);
		Element pElement=doc.createElement("password");
		pElement.setTextContent(password);
		
		ele.appendChild(uElement);
		ele.appendChild(pElement);
		
		headerList.add(new Header(new QName("yefan"),ele));
	}
	
}

SOAP:

<soap:Header><authHeader>
<userName>yefan</userName><password>123456</password>
</authHeader></soap:Header>
<soap:Body><ns2:getRoles xmlns:ns2="http://service.yefan.com/"/></soap:Body>
</soap:Envelope>

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页