Java笔记-CXF增加拦截器与自定义拦截器
程序员文章站
2023-12-27 08:53:52
...
如下搭建的Webservice:
在服务端填写如下代码:
这里就是添加进和出的拦截器
public class Main {
public static void main(String[] args) {
MyService myService = new MyServiceImpl();
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.setAddress("http://127.0.0.1/MyService"); //设置暴露地址
factoryBean.setServiceClass(MyService.class); //接口
factoryBean.setServiceBean(myService); //设置实现类
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.create(); //创建webservice接口
}
}
客户端增加拦截器:
public class Main {
public static void main(String[] args){
MyServiceService service = new MyServiceService();
MyService myService = service.getMyServicePort();
Client myClient = ClientProxy.getClient(myService);
myClient.getInInterceptors().add(new LoggingInInterceptor());
myClient.getOutInterceptors().add(new LoggingOutInterceptor());
myService.getRoles();
}
}
发送请求后可以看到打印了,服务端:
客户端:
下面是自定义的,加个用户名和密码:
他这里还是把用户名密码放到xml里面,如下:
客户端去判断:
客户关键端源码如下:
package cn.it1995.interceptor;
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;
import javax.xml.namespace.QName;
import java.util.List;
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;
}
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("java"), ele));
}
}
服务端关键源码如下:
package cn.it1995.interceptor;
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;
import java.util.List;
public class MyInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
public MyInterceptor(){
super(Phase.PRE_INVOKE);
}
@SuppressWarnings("null")
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("ABC") || !password.equals("123456")){
throw new Fault(new IllegalArgumentException("用户名密码错误"));
}
}
}
源码打包下载地址:
客户端:
https://github.com/fengfanchen/Java/tree/master/CXFClientDemoInterceptor
服务端:
https://github.com/fengfanchen/Java/tree/master/CXFServiceDemoInterceptor