一 使用系统拦截器: jdk写的程序不支持拦截器,需要使用cxf方式,导入cxf的jar包。 服务端: Endpont仍然使用jdk包里的,Endpoint.publish(address, new HelloWSImpl());返回一个Endpoint,打印一下,看
一 使用系统拦截器:
jdk写的程序不支持拦截器,需要使用cxf方式,导入cxf的jar包。
服务端:
Endpont仍然使用jdk包里的,Endpoint.publish(address, new HelloWSImpl());返回一个Endpoint,打印一下,看看是哪个包里的,然后调用这个包里的EndpointImpl,使用对
Endpoint强转得到。
服务端使用拦截器的程序:
package ws.intercepter.server; 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; public class Test { public static void main(String[] args) { String address="http://192.168.1.101:8899/WS/HelloWS"; Endpoint endpoint=javax.xml.ws.Endpoint.publish(address, new HelloWSImpl()); System.out.println(endpoint); //看看endpoint使用的是哪个包,EndpointImpl也使用对应的包 EndpointImpl ei=(EndpointImpl) endpoint; //获取服务器端的入拦截器集合 List<Interceptor<? extends Message>> list= ei.getInInterceptors(); list.add(new LoggingInInterceptor()); //添加系统日志拦截器 //获取服务器端的出拦截器集合 List<Interceptor<? extends Message>> list1=ei.getOutInterceptors(); list1.add(new LoggingOutInterceptor()); System.out.println("发布成功"); } }
客户端:
先获取Client,有Client获取拦截器集合。
客户端程序:
package ws.intercepter.server; import java.util.List; import org.apache.cxf.endpoint.Client; 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; public class Test { public static void main(String[] args) { HelloWSImplService factory=new HelloWSImplService(); HelloWS hw=factory.getHelloWSImplPort(); //拦截器要写在调用方法之前 Client client=ClientProxy.getClient(hw); //获取出客户端拦截器集合 List<Interceptor<? extends Message>> outInterceptors=client.getOutInterceptors(); outInterceptors.add(new LoggingOutInterceptor()); //获取入客户端的拦截器集合 List<Interceptor<? extends Message>> inInterceptors=client.getInInterceptors(); inInterceptors.add(new LoggingInInterceptor()); String s=hw.sayHello("tom"); System.out.println("client:"+s); } }
二 自定义拦截器
使用一个自定义的拦截器检查客户端用户名和密码
客户端拦截器:
package ws.client.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.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.apache.xml.utils.DOMHelper; import org.w3c.dom.Document; import org.w3c.dom.Element; public class MyClientInterceptor extends AbstractPhaseInterceptor<SoapMessage> { //自定义的校验字段,姓名和密码 private String name; private String pwd; public MyClientInterceptor(String name,String pwd) { super(Phase.PRE_PROTOCOL);//配置拦截时机,一定要有,准备协议化得时候拦截 this.name=name; this.pwd=pwd; } //封装数据到消息里,格式如下 /* <Envelop> * <head> //里面的标签可以有多个 * <atguigu> //自定义标签,随意 * <name>zhangsan</name> * <pwd>123123</pwd> * </atguigu> * <atguigu1> //自定义标签,随意 * <name>zhangsan</name> * <pwd>123123</pwd> * </atguigu1> * </head> * <body> * <sayHello> * <arg0>BOB</arg0> * </sayHello> * </body> * </Envelop> * * * * * * * * */ @Override public void handleMessage(SoapMessage msg) throws Fault { List<Header> headers=msg.getHeaders(); //创建Head //创建head里的元素,添加到head Document document=DOMHelper.createDocument(); Element atguigu=document.createElement("atguigu"); Element name=document.createElement("name"); name.setTextContent(this.name); atguigu.appendChild(name); Element pwd=document.createElement("pwd"); pwd.setTextContent(this.pwd); atguigu.appendChild(pwd); headers.add(new Header(new QName("atguigu"), atguigu));//QName里的参数要和标签名一样 System.out.println("client:handleMessage"); } }
客户端测试类:
package ws_interceptor_server1; import java.util.List; import org.apache.cxf.endpoint.Client; 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 ws.client.interceptor.MyClientInterceptor; public class ClientTest { public static void main(String[] args) { HelloWSImplService factory=new HelloWSImplService(); HelloWS ws=factory.getHelloWSImplPort(); Client client=ClientProxy.getClient(ws); //获取客户端出拦截器组(自定义拦截器) List<Interceptor<? extends Message>> outs=client.getOutInterceptors(); outs.add(new MyClientInterceptor("zhangsan", "123")); // outs.add(new LoggingOutInterceptor()); //获取客户端入拦截器组 // List<Interceptor<? extends Message>> ins=client.getInInterceptors(); // ins.add(new LoggingInInterceptor()); String res=ws.sayHello("tom"); System.out.println("client:"+res); } }
执行测试类,会在服务器端的入拦截器组的日志拦截器打印的信息,包含header里的消息,说明客户端的自定义拦截器添加成功。
服务器端自定义拦截器
package ws.server.interceptor; import javax.xml.namespace.QName; 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; /** * 检查用户名称和密码 * @author * */ public class MyServerInterceptor extends AbstractPhaseInterceptor<SoapMessage> { public MyServerInterceptor() { super(Phase.PRE_PROTOCOL); // TODO Auto-generated constructor stub } @Override public void handleMessage(SoapMessage msg) throws Fault { // 读header里的数据 Header header=msg.getHeader(new QName("atguigu"));//参数和客户端传的一样 if(header!=null){ Element atguigu=(Element) header.getObject(); String name=atguigu.getElementsByTagName("name").item(0).getTextContent(); String pwd=atguigu.getElementsByTagName("pwd").item(0).getTextContent(); if(name.equals("zhangsan")&&pwd.equals("123")){ System.out.println("server:check ok"); return; } } //没通过校验,抛出异常 System.out.println("没有通过校验"); throw new Fault(new RuntimeException("用户名或密码不对")); } }
服务器端测试类
package ws_interceptor_server1; 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 ws.server.interceptor.MyServerInterceptor; public class Test { public static void main(String[] args) { String url="http://192.168.1.100:8899/WS/HelloWS"; Endpoint endPoint=Endpoint.publish(url, new HelloWSImpl()); EndpointImpl endpointImpl=(EndpointImpl) endPoint; //获取服务器端入拦截器(自定义拦截器) List<Interceptor<? extends Message>> ins=endpointImpl.getInInterceptors(); ins.add(new MyServerInterceptor()); // ins.add(new LoggingInInterceptor()); //获取服务器端出拦截器 // List<Interceptor<? extends Message>> outs=endpointImpl.getOutInterceptors(); // outs.add(new LoggingOutInterceptor()); System.out.println("发布成功"); } }