CXF的拦截器(七)


1.CXF的拦截器

1.1理解

• 为什么设计拦截器?

1. 为了在webservice请求过程中,能动态操作请求和响应数据, CXF设计了拦截器.

• 拦截器分类:

2. 按所处的位置分:服务器端拦截器,客户端拦截器

3. 按消息的方向分:入拦截器,出拦截器

4. 按定义者分:系统拦截器,自定义拦截

 

• 拦截器API

Interceptor(拦截器接口)

AbstractPhaseInterceptor(自定义拦截器从此继承)

LoggingInInterceptor(系统日志入拦截器类)

LoggingOutInterceptor(系统日志出拦截器类)

 

 

 

为了让程序员能访问或修改soap消息中的内容,cxf设计的拦截器


2.使用系统拦截器


   1.服务器端拦截器:
    //通过终端类将helloWS对象发布到address上
    EndpointImpl endpoint = (EndpointImpl) Endpoint.publish(address, helloWS);
    //在服务器端添加一个日志的in拦截器
    endpoint.getInInterceptors().add(new LoggingInInterceptor());
    //在服务器端添加一个日志的out拦截器
    endpoint.getOutInterceptors().add(new LoggingOutInterceptor());


   2.客户端拦截器:
    //创建生成SEI实现对象的工厂
    HelloWSImplService factory = new HelloWSImplService();
    //得到web service的SEI的实现类对象(动态生成的对象)
    HelloWS helloWS = factory.getHelloWSImplPort();
    //得到客户端对象
    Client client = ClientProxy.getClient(helloWS);
    //添加客户端的out拦截器
    client.getOutInterceptors().add(new LoggingOutInterceptor());
    //添加客户端的in拦截器
    client.getInInterceptors().add(new LoggingInInterceptor());
    Student s = helloWS.getStudentById(1);
    System.out.println("返回 "+s.getName());

 

3.编码实现自定义拦截器

 使用日志拦截器,实现日志记录

– LoggingInInterceptor

– LoggingOutInterceptor

• 使用自定义拦截器,实现用户名与密码的检验

– 服务器端的in拦截器

– 客户端的out拦截器

– xfzhang/123456

 

3.1自定义拦截器


   AbstractPhaseInterceptor:抽象过程拦截器,一般自定义的拦截器都会继承于它
   功能:通过自定义拦截器实现用户名和密码的检查


    1. 服务器端:
     设置in拦截器,从soap消息中获取用户名和密码数据,如果不满足条件就不执行web service的方法
     public class MyIntercept extends AbstractPhaseInterceptor<SoapMessage> {
      public MyIntercept() {
       super(Phase.PRE_PROTOCOL);
      }
      @Override
      public void handleMessage(SoapMessage msg) throws Fault {
       System.out.println("-----handleMessage");
       
       Header header = msg.getHeader(new QName("atguigu"));
       if(header==null) {
        throw new Fault(new RuntimeException("用户名密码不存在 "));
       } else {
        Element data = (Element) header.getObject();
        if(data==null) {
         throw new Fault(new RuntimeException("用户名密码不存在22"));
        } else {
         String name = data.getElementsByTagName("name").item(0).getTextContent();
         String password = data.getElementsByTagName("password").item(0).getTextContent();
         if(!"xfzhang".equals(name) || !"amomo163".equals(password)) {
          throw new Fault(new RuntimeException("用户名密码不正确"));
         }
        }
       }
       System.out.println("通过拦截器了!");
      }
     }

 

   server发布

public class ServerTest4 {

	public static void main(String[] args) {
		String address = "http://192.168.10.165:8888/day01_ws/datatypews";
		Endpoint endpoint = Endpoint.publish(address , new HelloWSImpl());
		System.out.println(endpoint);
		EndpointImpl endpointImpl  = (EndpointImpl) endpoint;
		
		//服务端的入拦截器
		List<Interceptor<? extends Message>> inInterceptors = endpointImpl.getInInterceptors();
		inInterceptors.add(new CheckUserInterceptor());
		
		System.out.println("发布webservice成功!");
	}
}

 


    2. 客户端:
     设置out拦截器,向soap消息中添加用户名和密码数据
     public class MyIntercept extends AbstractPhaseInterceptor<SoapMessage> {
      private String name;
      private String password;

      public MyIntercept(String name, String pasword) {
       super(Phase.PRE_PROTOCOL);
       this.name = name;
       this.password = pasword;

      }

      @Override
      public void handleMessage(SoapMessage msg) throws Fault {
       System.out.println("-----handleMessage");
       Document document = DOMUtils.createDocument();
       //<atguigu>
       Element atguiguEle = document.createElement("atguigu");
       //<name>xfzhang_amomo163</name>
       Element nameEle = document.createElement("name");
       nameEle.setTextContent(name);
       
       //<password>xfzhang_amomo163</password>
       Element passwordEle = document.createElement("password");
       passwordEle.setTextContent(password);
       
       atguiguEle.appendChild(nameEle);
       atguiguEle.appendChild(passwordEle);
       
       //添加为请求消息的<soap:Header>
       msg.getHeaders().add(new Header(new QName("atguigu"), atguiguEle));
      }

     }

     

    客户端

*
 * 测试自定义拦截器
 */
public class ClientTest4 {

	public static void main(String[] args) {
		HelloWSImplService factory = new HelloWSImplService();
		HelloWS hellWS = factory.getHelloWSImplPort();
		
		//发送请求的客户端对象
		Client client = ClientProxy.getClient(hellWS);
		//客户端的自定义出拦截器
		List<Interceptor<? extends Message>> outInterceptors = client.getOutInterceptors();
		outInterceptors.add(new AddUserInterceptor("xfzhang", "1234564"));
		
		String result = hellWS.sayHello("BOB");
		System.out.println("client "+result);
	}
}


 


    3. 说明:用户名和密码是以xml片断的形式存放在soap消息中的
     <soap:Envelope xmlns:soap="
http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Header>
       <atguigu>
        <name>xfzhang</name>
        <password>amomo163</password>
       </atguigu>
      </soap:Header>
      <soap:Body>
       <ns2:getStudentById xmlns:ns2="
http://server.ws.java.atguigu.net/">
        <arg0>1</arg0>
       </ns2:getStudentById>
      </soap:Body>
     </soap:Envelope>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值