CXF为什么要设计拦截器?

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

拦截器分类

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

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

  

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

  3.1系统拦截器:

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

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

  1. ***server***
  2. //SEI
  3. @WebService
  4. public interface HelloWS {
  5.  
  6. @WebMethod
  7. public String sayHello(String name);
  8. }
  9.  
  10. //SEI implement
  11. @WebService
  12. public class HelloWSImpl implements HelloWS {
  13.  
  14. @Override
  15. public String sayHello(String name) {
  16. return "hello: "+name;
  17. }
  18. }
  19.  
  20. //publish
  21. public class RealeaseWS {
  22.  
  23. public static void main(String[] args) {
  24. String address = "http://localhost:8989/WebService_Server";
  25. Endpoint endpoint = Endpoint.publish(address, new HelloWSImpl());
  26. EndpointImpl endpointImpl = (EndpointImpl)endpoint;
  27. //服务器的的入拦截器
  28. List<Interceptor<? extends Message>> inInterceptors = endpointImpl.getInInterceptors();
  29. //将系统日志入拦截器类添加到入拦截器集合中
  30. inInterceptors.add(new LoggingInInterceptor());
  31.  
  32. //服务器的的出拦截器
  33. List<Interceptor<? extends Message>> outInterceptors = endpointImpl.getOutInterceptors();
  34. //将系统日志出拦截器类添加到出拦截器集合中
  35. outInterceptors.add(new LoggingOutInterceptor());
  36. System.out.println("webService发布成功!");
  37. }
  38. }
  1. ***client***
  2. //生成客户端代码步骤省略。。。
  3. //客户端调用服务器方法测试程序
  4. public class TestWebService {
  5.  
  6. public static void main(String[] args) {
  7. HelloWSImplService factory = new HelloWSImplService();
  8. HelloWSImpl helloWSImpl = factory.getHelloWSImplPort();
  9.  
  10. //在调用服务器方法前配置拦截器
  11. //获取发送请求的客户端对象
  12. Client client = ClientProxy.getClient(helloWSImpl);
  13.  
  14. //客户端的系统日志出拦截器
  15. List<Interceptor<? extends Message>> outInterceptors = client.getOutInterceptors();
  16. outInterceptors.add(new LoggingOutInterceptor());
  17. //客户端的系统日志入拦截器
  18. List<Interceptor<? extends Message>> inInterceptors = client.getInInterceptors();
  19. inInterceptors.add(new LoggingInInterceptor());
  20.  
  21. String result = helloWSImpl.sayHello("webService");
  22. System.out.println(result);
  23. }
  24. }

执行程序后日志:

  1. 1.首先是从客户端发送请求给服务器,先是发出请求给服务器被拦截(客户端出拦截器),服务器返回响应数据后又被客户端拦截器拦截(客户端入拦截器),拦截信息如下:
  2. 一月 26, 2016 10:32:44 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
  3. INFO: Creating Service {http://server.webService.com/}HelloWSImplService from WSDL: http://localhost:8989/WebService_Server?wsdl
  4. 一月 26, 2016 10:32:45 下午 org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS
  5. INFO: Outbound Message
  6. ---------------------------
  7. ID: 1
  8. Address: http://localhost:8989/WebService_Server
  9. Encoding: UTF-8
  10. Content-Type: text/xml
  11. Headers: {Accept=[*/*], SOAPAction=[""]}
  12. Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://server.webService.com/"><arg0>webService</arg0></ns2:sayHello></soap:Body></soap:Envelope>
  13. --------------------------------------
  14. 一月 26, 2016 10:32:45 下午 org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS
  15. INFO: Inbound Message
  16. ----------------------------
  17. ID: 1
  18. Response-Code: 200
  19. Encoding: UTF-8
  20. Content-Type: text/xml;charset=UTF-8
  21. Headers: {Content-Length=[230], content-type=[text/xml;charset=UTF-8], Server=[Jetty(7.5.4.v20111024)]}
  22. Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://server.webService.com/"><return>hello: webService</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
  23. --------------------------------------
  24. hello: webService
  1. 2.服务器接收到客户端请求后,该请求先是被拦截(服务器入拦截器),接着服务器接收请求并执行后,将数据发回给客户端又被拦截(服务器出拦截器),该拦截信息为:
  2. 一月 26, 2016 10:20:35 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
  3. INFO: Creating Service {http://server.webService.com/}HelloWSImplService from class com.webService.server.HelloWS
  4. 一月 26, 2016 10:20:38 下午 org.apache.cxf.endpoint.ServerImpl initDestination
  5. INFO: Setting the server's publish address to be http://localhost:8989/WebService_Server
  6. 一月 26, 2016 10:20:38 下午 org.eclipse.jetty.server.Server doStart
  7. INFO: jetty-7.5.4.v20111024
  8. 一月 26, 2016 10:20:38 下午 org.eclipse.jetty.server.AbstractConnector doStart
  9. INFO: Started SelectChannelConnector@localhost:8989 STARTING
  10. 一月 26, 2016 10:20:38 下午 org.eclipse.jetty.server.handler.ContextHandler startContext
  11. INFO: started o.e.j.s.h.ContextHandler{,null}
  12. webService发布成功!
  13. 一月 26, 2016 10:32:44 下午 org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS
  14. INFO: Inbound Message
  15. ----------------------------
  16. ID: 1
  17. Address: http://localhost:8989/WebService_Server?wsdl
  18. Encoding: UTF-8
  19. Http-Method: GET
  20. Content-Type: text/xml
  21. Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], content-type=[text/xml], Host=[localhost:8989], Pragma=[no-cache], User-Agent=[Apache CXF 2.5.9]}
  22. --------------------------------------
  23. 一月 26, 2016 10:32:45 下午 org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS
  24. INFO: Inbound Message
  25. ----------------------------
  26. ID: 2
  27. Address: http://localhost:8989/WebService_Server
  28. Encoding: UTF-8
  29. Http-Method: POST
  30. Content-Type: text/xml; charset=UTF-8
  31. Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[203], content-type=[text/xml; charset=UTF-8], Host=[localhost:8989], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.5.9]}
  32. Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://server.webService.com/"><arg0>webService</arg0></ns2:sayHello></soap:Body></soap:Envelope>
  33. --------------------------------------

  3.2自定义拦截器

  案例:使用自定义拦截器,实现用户名和密码的权限,即访问权限控制!

    需要的拦截器:客户端的出拦截器和服务器的入拦截器

  说明:自定义拦截器都从AbstractPhaseIntercepter继承

  问题:为什么客户端需要设置拦截器?

    因为客户端拦截器能得到客户端的用户名和密码,并且将用户名和密码设置到请求头中,这样服务器接收请求时才能获取到用户名和密码,并且根据得到的用户名和密码来做出验证!

  1. ***客户端拦截器***
  2. public class AccountInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
  3.  
  4. private String name;
  5. private String password;
  6.  
  7. public AccountInterceptor(String name,String password) {
  8. //Phase值决定了拦截器什么时候拦截到消息
  9. //PRE_PROTOCOL准备请求时拦截
  10. super(Phase.PRE_PROTOCOL);
  11. this.name = name;
  12. this.password = password;
  13. }
  14.  
  15. //一旦被拦截,首先调用此方法
  16. @SuppressWarnings("deprecation")
  17. @Override
  18. public void handleMessage(SoapMessage msg) throws Fault {
  19. List<Header> headers = msg.getHeaders();
  20. //在客户端请求时,会将用户名密码带过去
  21. //怎么带用户名和密码到服务器,将用户名和密码设置在请求头中
  22. org.w3c.dom.Document document = DOMHelper.createDocument();
  23. Element ele = document.createElement("account");//创建标签<account></account>
  24.  
  25. Element eleName = document.createElement("name");//创建标签<name></name>
  26. eleName.setTextContent(name);//给<name>设值,值为客户端传进来的用户名
  27. ele.appendChild(eleName);
  28.  
  29. Element elePwd = document.createElement("password");//创建标签<password></password>
  30. elePwd.setTextContent(password);//给<password>设值,值为客户端传进来的密码
  31. ele.appendChild(elePwd);
  32. //设置标签<account>的account
  33. headers.add(new Header(new QName("account"),ele));
  34. //如果拦截了,打印以下信息!
  35. System.out.println("客户端拦截了");
  36. }
  37. }
  1. ***添加自定义拦截器到客户端出拦截器中***
  2. public class TestWebService {
  3.  
  4. public static void main(String[] args) {
  5. HelloWSImplService factory = new HelloWSImplService();
  6. HelloWSImpl helloWSImpl = factory.getHelloWSImplPort();
  7.  
  8. //设置用户名和密码
  9. String name = "webService";
  10. String password = "123456";
  11.  
  12. //在调用服务器方法前配置拦截器
  13. //获取发送请求的客户端对象
  14. Client client = ClientProxy.getClient(helloWSImpl);
  15.  
  16. //添加自定义拦截器到客户端出拦截器中
  17. List<Interceptor<? extends Message>> outInterceptors = client.getOutInterceptors();
  18. outInterceptors.add(new AccountInterceptor(name,password));
  19.  
  20. String result = helloWSImpl.sayHello("webService");
  21. System.out.println(result);
  22. }
  23. }

*****************以上是客户端********************

*****************以上是服务器********************

  1. //服务器拦截器
  2. public class CheckAccountInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
  3.  
  4. public CheckAccountInterceptor() {
  5. super(Phase.PRE_PROTOCOL);
  6.  
  7. }
  8.  
  9. @Override
  10. public void handleMessage(SoapMessage message) throws Fault {
  11. //获取客户端请求头
  12. //account为客户端设置的qname
  13. Header header = message.getHeader(new QName("account"));
  14. if(header != null){
  15. Element account = (Element) header.getObject();
  16. //通过标签名获取值<name></name>
  17. String name = account.getElementsByTagName("name").item(0).getTextContent();
  18. String password = account.getElementsByTagName("password").item(0).getTextContent();
  19. if("webService".equals(name) && "123456".equals(password)){
  20. System.out.println("验证通过......");
  21. }
  22. }
  23. System.out.println("没有通过拦截器!");
  24. throw new Fault(new RuntimeException("用户名或者密码错误!"));
  25. }
  26. }
  1. //将自定义拦截器添加到服务器的入拦截器
  2. public class RealeaseWS {
  3.  
  4. public static void main(String[] args) {
  5. String address = "http://localhost:8989/WebService_Server";
  6. Endpoint endpoint = Endpoint.publish(address, new HelloWSImpl());
  7. EndpointImpl endpointImpl = (EndpointImpl)endpoint;
  8. //将自定义拦截器添加到服务器的入拦截器
  9. List<Interceptor<? extends Message>> inInterceptors = endpointImpl.getInInterceptors();
  10. inInterceptors.add(new CheckAccountInterceptor());
  11. System.out.println("webService发布成功!");
  12. }
  13. }

运行程序:

  如果没有通过,则出现以下信息:

  1. 客户端拦截了
  2. Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: 用户名或者密码错误!
  3. at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:156)
  4. at com.sun.proxy.$Proxy27.sayHello(Unknown Source)
  5. at com.test.TestWebService.main(TestWebService.java:34)
  6. Caused by: org.apache.cxf.binding.soap.SoapFault: 用户名或者密码错误!

5.webService拦截器的更多相关文章

  1. 7.添加基于Spring的WebService拦截器

    客户端拦截器: public class AccountInterceptor extends AbstractPhaseInterceptor<SoapMessage>{ private ...

  2. webservice拦截器 查看消息包(soap)

    服务端: 1.获取EndpointImpl对象 2.调用EndpointImpl对象中的方法获取In拦截器 3.调用EndpointImpl对象中的方法获取out拦截器 4.添加自己的In拦截器与Ou ...

  3. WebService -- Java 实现之 CXF ( 添加系统预定义的拦截器)

    1. 概述 CXF允许我们在webservice的in/out位置添加拦截器.拦截器有两大分类,一类是系统预定义的:另一类是自定义拦截器. 2. 在server端添加拦截器. JaxWsServerF ...

  4. 【WebService】WebService之CXF的拦截器(五)

    CXF拦截器介绍 CXF拦截器是功能的主要实现单元,也是主要的扩展点,可以在不对核心模块进行修改的情况下,动态添加功能.当服务被调用时,会经过多个拦截器链(Interceptor Chain)处理,拦 ...

  5. 为webService添加Interceptor(拦截器)

    今天写一个简单的拦截器,以webService接口为例: 背景:H5的一个项目,只要调用H5webService 接口下面的方法都会触发一个AuthorityInterceptor去验证是否调用类型是 ...

  6. 使用CXF开发WebService程序的总结(六):结合拦截器使用

    1. 使用CXF提供的拦截器 拦截器在我看来分为两端两向,两端分为:客户端和服务端,两向(方向)分为:进(in)和出(out),即大致四类拦截器. 在这里利用cxf提供的 日志拦截器举例 1.1 在服 ...

  7. WebService学习总结(五)--CXF的拦截器

    拦截器是Cxf的基础,Cxf中很多的功能都是由内置的拦截器来实现的,拦截器在Cxf中由Interceptor表示.拦截器的作用类似axis2中handle.Cxf的拦截器包括入拦截器和出拦截器,所有的 ...

  8. WebService cxf 接口中获得拦截器参数

    1. 拦截器中put属性 Message message = PhaseInterceptorChain.getCurrentMessage(); message.put("AuthCode ...

  9. 使用CXF为webservice添加拦截器

      拦截器分为Service端和Client端 拦截器是在发送soap消息包的某一个时机拦截soap消息包,对soap消息包的数据进行分析或处理.分为CXF自带的拦截器和自定义的拦截器 1.Servi ...

随机推荐

  1. 3.使用git提交项目到开源中国(gitosc)

    1.提交地址 使用的是开源中国git仓库 git.oschina.net 在windos环境下使用msysgit. 2.初始化化 username.email初始化 git config --glob ...

  2. 用 python实现简单EXCEL数据统计

    任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...

  3. 查看Oracle中是否有锁表的sql

    1.查看是否有锁表的sql 代码如下: select 'blocker('||lb.sid||':'||sb.username||')-sql:'|| qb.sql_text blockers, 'w ...

  4. android ListView点击item返回后listview滚动位置

    1.Don't work when dynamically loading content Parcelable state; @Override public void onPause() { // ...

  5. HTML超标记语言

     Html超文本标记语言,负责描绘Web世界的骨架. 〇.工具 http;//www.w3cchool.com.cn 一.Tim Bemers Lee 万维网之父: Html设计者: W3C创始人: ...

  6. 【如何快速的开发一个完整的iOS直播app】(原理篇)

    原文转自:袁峥Seemygo    感谢分享.自我学习 目录 [如何快速的开发一个完整的iOS直播app](原理篇) [如何快速的开发一个完整的iOS直播app](播放篇) [如何快速的开发一个完整的 ...

  7. sql 中 in与exists的对比

    1.exists只能用于子查询,可以替代IN,如果查询到结果则退出内部查询,并将条件标记为TRUE,传回全部结果资料 in 不管匹配到匹配不到,都全部匹配 2.根据上面的解释可以得出结论:如果子查询结 ...

  8. Spring 4 官方文档学习 Spring与Java EE技术的集成

    本部分覆盖了以下内容: Chapter 28, Remoting and web services using Spring -- 使用Spring进行远程和web服务 Chapter 29, Ent ...

  9. mysql将一张表中多条记录按联系整合成一条

    现有表如下:id time is_login 3 2012-07-03 11:20:20 13 2012-07-03 11:25:20 04 2012-07-03 12:30:20 14 2012-0 ...

  10. linux bash 笔记

    Bash的简单使用笔记: 1- 命令行参数(调用脚本后面跟的参数) 2- 命令行选项(修改命令行为的单字符串) 3- 获取键盘输入 4- 读文件 5- 函数 1.命令行参数(调用脚本后面跟的参数) x ...