1、定义一个自定义的拦截器
public class HelloWorldService {
public static void main(String[] args) {
HelloWorld hw = new HelloWorldImpl();
EndpointImpl ep = (EndpointImpl)Endpoint.publish("http://192.168.123.47/ws", hw);
ep.getInInterceptors().add(new AuthInterceptor());
System.out.println("web service服务启动成功");
}
}
2、自定义拦截器
package cn.xjs.util;
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() {
super(Phase.PRE_INVOKE);
}
@Override
public void handleMessage(SoapMessage mes) throws Fault {
List<Header> headers = mes.getHeaders();
if(headers==null || headers.size()<1) {
throw new Fault(new Exception("没有请求header头信息"));
}
Header header = headers.get(0);
Element ele =(Element)header.getObject();
NodeList userIds = ele.getElementsByTagName("userId");
NodeList passList = ele.getElementsByTagName("pass");
if(userIds.getLength()!=1 ) {
throw new Fault(new Exception("用户名不对"));
}
if(passList.getLength()!=1 ) {
throw new Fault(new Exception("密码不对"));
}
String userId = userIds.item(0).getTextContent();
String pass= passList.item(0).getTextContent();
//....
}
}
原文地址:https://www.cnblogs.com/fengfengshaonian/p/8641814.html