CXF为什么要设计拦截器?

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

拦截器分类

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

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

  

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

  3.1系统拦截器:

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

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

***server***
//SEI
@WebService
public interface HelloWS { @WebMethod
public String sayHello(String name);
} //SEI implement
@WebService
public class HelloWSImpl implements HelloWS { @Override
public String sayHello(String name) {
return "hello: "+name;
}
} //publish
public class RealeaseWS { public static void main(String[] args) {
String address = "http://localhost:8989/WebService_Server";
Endpoint endpoint = Endpoint.publish(address, new HelloWSImpl());
EndpointImpl endpointImpl = (EndpointImpl)endpoint;
//服务器的的入拦截器
List<Interceptor<? extends Message>> inInterceptors = endpointImpl.getInInterceptors();
//将系统日志入拦截器类添加到入拦截器集合中
inInterceptors.add(new LoggingInInterceptor()); //服务器的的出拦截器
List<Interceptor<? extends Message>> outInterceptors = endpointImpl.getOutInterceptors();
//将系统日志出拦截器类添加到出拦截器集合中
outInterceptors.add(new LoggingOutInterceptor());
System.out.println("webService发布成功!");
}
}
***client***
//生成客户端代码步骤省略。。。
//客户端调用服务器方法测试程序
public class TestWebService { public static void main(String[] args) {
HelloWSImplService factory = new HelloWSImplService();
HelloWSImpl helloWSImpl = factory.getHelloWSImplPort(); //在调用服务器方法前配置拦截器
//获取发送请求的客户端对象
Client client = ClientProxy.getClient(helloWSImpl); //客户端的系统日志出拦截器
List<Interceptor<? extends Message>> outInterceptors = client.getOutInterceptors();
outInterceptors.add(new LoggingOutInterceptor());
//客户端的系统日志入拦截器
List<Interceptor<? extends Message>> inInterceptors = client.getInInterceptors();
inInterceptors.add(new LoggingInInterceptor()); String result = helloWSImpl.sayHello("webService");
System.out.println(result);
}
}

执行程序后日志:

1.首先是从客户端发送请求给服务器,先是发出请求给服务器被拦截(客户端出拦截器),服务器返回响应数据后又被客户端拦截器拦截(客户端入拦截器),拦截信息如下:
一月 26, 2016 10:32:44 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
INFO: Creating Service {http://server.webService.com/}HelloWSImplService from WSDL: http://localhost:8989/WebService_Server?wsdl
一月 26, 2016 10:32:45 下午 org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS
INFO: Outbound Message
---------------------------
ID: 1
Address: http://localhost:8989/WebService_Server
Encoding: UTF-8
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
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>
--------------------------------------
一月 26, 2016 10:32:45 下午 org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS
INFO: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[230], content-type=[text/xml;charset=UTF-8], Server=[Jetty(7.5.4.v20111024)]}
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>
--------------------------------------
hello: webService
2.服务器接收到客户端请求后,该请求先是被拦截(服务器入拦截器),接着服务器接收请求并执行后,将数据发回给客户端又被拦截(服务器出拦截器),该拦截信息为:
一月 26, 2016 10:20:35 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://server.webService.com/}HelloWSImplService from class com.webService.server.HelloWS
一月 26, 2016 10:20:38 下午 org.apache.cxf.endpoint.ServerImpl initDestination
INFO: Setting the server's publish address to be http://localhost:8989/WebService_Server
一月 26, 2016 10:20:38 下午 org.eclipse.jetty.server.Server doStart
INFO: jetty-7.5.4.v20111024
一月 26, 2016 10:20:38 下午 org.eclipse.jetty.server.AbstractConnector doStart
INFO: Started SelectChannelConnector@localhost:8989 STARTING
一月 26, 2016 10:20:38 下午 org.eclipse.jetty.server.handler.ContextHandler startContext
INFO: started o.e.j.s.h.ContextHandler{,null}
webService发布成功!
一月 26, 2016 10:32:44 下午 org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS
INFO: Inbound Message
----------------------------
ID: 1
Address: http://localhost:8989/WebService_Server?wsdl
Encoding: UTF-8
Http-Method: GET
Content-Type: text/xml
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]}
--------------------------------------
一月 26, 2016 10:32:45 下午 org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS
INFO: Inbound Message
----------------------------
ID: 2
Address: http://localhost:8989/WebService_Server
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
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]}
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>
--------------------------------------

  3.2自定义拦截器

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

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

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

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

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

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

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

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

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

运行程序:

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

客户端拦截了
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: 用户名或者密码错误!
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:156)
at com.sun.proxy.$Proxy27.sayHello(Unknown Source)
at com.test.TestWebService.main(TestWebService.java:34)
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. win10使用小技巧以及常见问题处理方案

    1.win10开机一直处于黑屏状态或者反复重启怎么处理? 处理小方法:开机按win+X组合件进入高级修复模式---选择修复开启模式---f4进入安全模式开机状态---管理控制面板---禁用或者卸载显卡 ...

  2. 转一下大牛的嵌入web页播放视频方法(转)

    来自:http://www.cnblogs.com/bandry/archive/2006/10/11/526229.html 在Web页中嵌入Media Player的方法比较简单,只要用HTML中 ...

  3. APP如何实现推送功能

    一.推送功能的集成 (1)在Umeng开发者中心,申请新应用,开通推送功能.此时需要上传开发推送证书和生产推送证书的p12文件. 申请证书的流程如下: >>1 创建开发推送证书 >& ...

  4. ThinkPad L440 FN键设置

    刚入手了ThinkPad L440,用起来相当不错,嘿嘿! L440系统默认(F1-F12)键盘为系统默认功能键,主要控制音量.亮度.连接投影仪等. 因为编写程序需要调试,经常用到F10,F11等键, ...

  5. SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  6. B-tree

    2-3 Tree 二叉搜索树的每个节点只带有一个值,这个值将数据区间划分成两部分,值左边的部分(也就是小于这个值的数据)保存到节点的左子树,值右边的部分保存到节点的右子树.因此,每个非叶子节最多能够拥 ...

  7. C#时间戳转成php的time()

    DateTime timeStamp = new DateTime(1970,1,1);  //得到1970年的时间戳 long a = (DateTime.UtcNow.Ticks - timeSt ...

  8. Python 变量范围

    1.本地变量,全局变量 Python 中有2种变量作用范围本地变量,全局变量. 变量搜索路径是:本地变量->全局变量 它们简而言之就是本地变量的值只在本地作用范围有效.而全局变量的作用范围是全局 ...

  9. maven 三个基本插件 clean dependency compiler

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. Httpoxy远程代理感染漏洞 [转]

    Httpoxy是一个最新曝出的一个CGI程序漏洞,它主要可能威胁到运行在CGI上的PHP,Go,Python和其他代码程序语言. 0×01  什么是httpoxy? 具体来说, httpoxy是一组影 ...