今天我们来说一下CXF的Interceptor,其主要功能就是在每个请求响应之前或响应之后,做一些事情。这里的Interceptor就和Filter、Struts的Interceptor很类似,提供它的主要作用就是为了很好的降低代码的耦合性,提供代码的内聚性。下面我们就看看CXF的Interceptor是怎么样工作的。
1、我们就用上篇博客中的HelloWorldService,客户端的调用代码重新写一份,代码如下:
package com.iflytek.client; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.iflytek.service.IHelloWorldService; /** * @author xdwang * * @create Apr 25, 2013 10:35:50 PM * * @email:xdwangiflytek@gmail.com * * @description CXF WebService客户端调用代码 * */ public class ServiceMessageInterceperClient { public static void main(String[] args) { // 调用WebService JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(IHelloWorldService.class); factory.setAddress("http://localhost:8000/helloWorld"); factory.getInInterceptors().add(new LoggingInInterceptor()); factory.getOutInterceptors().add(new LoggingOutInterceptor()); IHelloWorldService service = (IHelloWorldService) factory.create(); System.out.println("[result]" + service.sayHello("xdwang")); } }
上面的CXF的拦截器是添加在客户端,同样在服务器端也是可以添加拦截器Interceptor的。运行后结果如下:
2013-7-25 19:22:42 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://service.iflytek.com/}IHelloWorldServiceService from class com.iflytek.service.IHelloWorldService 2013-7-25 19:22:43 org.apache.cxf.services.IHelloWorldServiceService.IHelloWorldServicePort.IHelloWorldService 信息: Outbound Message --------------------------- ID: 1 Address: http://localhost:8000/helloWorld Encoding: UTF-8 Http-Method: POST 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://service.iflytek.com/"><name>xdwang</name></ns2:sayHello></soap:Body></soap:Envelope> -------------------------------------- 2013-7-25 19:22:43 org.apache.cxf.services.IHelloWorldServiceService.IHelloWorldServicePort.IHelloWorldService 信息: Inbound Message ---------------------------- ID: 1 Response-Code: 200 Encoding: UTF-8 Content-Type: text/xml;charset=UTF-8 Headers: {Content-Length=[235], content-type=[text/xml;charset=UTF-8], Server=[Jetty(8.1.7.v20120910)]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayHelloResponse xmlns:ns1="http://service.iflytek.com/"><return>xdwang say: Hello World </return></ns1:sayHelloResponse></soap:Body></soap:Envelope> -------------------------------------- [result]xdwang say: Hello World
上面的部分信息是LoggingInterceptor输出的日志信息,分别在请求和响应的时候输出日志信息,还有输出请求的时候参数的信息以及响应的时候返回值的信息。
2、刚才是客户端添加Interceptor,现在我们自己编写一个Interceptor,这个Interceptor需要继承AbstractPhaseInterceptor,实现handleMessage和一个带参数的构造函数。然后在服务器端添加这个Interceptor。
Interceptor代码如下:
package com.iflytek.interceptor; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor; /** * @author xdwang * * @create 2013-7-25 下午7:24:12 * * @email:xdwangiflytek@gmail.com * * @description 自定义消息拦截器 * */ public class MessageInterceptor extends AbstractPhaseInterceptor<Message> { // 至少要一个带参的构造函数 public MessageInterceptor(String phase) { super(phase); } public void handleMessage(Message message) throws Fault { System.out.println("============handleMessage=========="); System.out.println(message); if (message.getDestination() != null) { System.out.println(message.getId() + "=" + message.getDestination().getMessageObserver()); } if (message.getExchange() != null) { System.out.println(message.getExchange().getInMessage() + "=" + message.getExchange().getInFaultMessage()); System.out .println(message.getExchange().getOutMessage() + "=" + message.getExchange().getOutFaultMessage()); } } }
下面看看发布服务和添加自定义拦截器的代码:
package com.iflytek.service.deploy; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import org.apache.cxf.phase.Phase; import com.iflytek.interceptor.MessageInterceptor; import com.iflytek.service.HelloWorldService; /** * @author xdwang * * @create 2013-7-25 下午7:25:55 * * @email:xdwangiflytek@gmail.com * * @description 在服务器发布自定义的Interceptor * */ public class DeployInterceptorService { public static void main(String[] args) throws InterruptedException { // 发布WebService JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); // 设置Service Class factory.setServiceClass(HelloWorldService.class); factory.setAddress("http://localhost:9000/helloWorld"); // 设置ServiceBean对象 factory.setServiceBean(new HelloWorldService()); // 添加请求和响应的拦截器,Phase.RECEIVE只对In有效,Phase.SEND只对Out有效 factory.getInInterceptors().add(new MessageInterceptor(Phase.RECEIVE)); factory.getOutInterceptors().add(new MessageInterceptor(Phase.SEND)); factory.create(); System.out.println("Server start ......"); Thread.sleep(1000 * 60); System.exit(0); System.out.println("Server exit "); } }
值得说的是,以前发布WebService是用Endpoint的push方法。这里用的是JaxWsServerFactoryBean和客户端调用的代码JaxWsProxyFactoryBean有点不同。
package com.iflytek.client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.iflytek.service.IHelloWorldService; /** * @author xdwang * * @create 2013-7-25 下午7:28:18 * * @email:xdwangiflytek@gmail.com * * @description CXF WebService客户端调用代码 * */ public class HelloWorldServiceClient { public static void main(String[] args) { // 调用WebService JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(IHelloWorldService.class); factory.setAddress("http://localhost:9000/helloWorld"); IHelloWorldService service = (IHelloWorldService) factory.create(); System.out.println("[result]" + service.sayHello("hoojo")); } }
运行结果:
2013-7-25 19:28:52 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://service.iflytek.com/}HelloWorldServiceService from class com.iflytek.service.HelloWorldService 2013-7-25 19:28:52 org.apache.cxf.endpoint.ServerImpl initDestination 信息: Setting the server's publish address to be http://localhost:9000/helloWorld 2013-7-25 19:28:52 org.eclipse.jetty.server.Server doStart 信息: jetty-8.1.7.v20120910 2013-7-25 19:28:53 org.eclipse.jetty.server.AbstractConnector doStart 信息: Started SelectChannelConnector@localhost:9000 2013-7-25 19:28:53 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL 信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl 2013-7-25 19:28:53 org.apache.cxf.endpoint.ServerImpl initDestination 信息: Setting the server's publish address to be soap.udp://239.255.255.250:3702 2013-7-25 19:28:53 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImpl Server start ...... ============handleMessage========== {org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[196], content-type=[text/xml; charset=UTF-8], Host=[localhost:9000], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.7.4]}, HTTP_CONTEXT_MATCH_STRATEGY=stem, org.apache.cxf.request.url=http://localhost:9000/helloWorld, org.apache.cxf.request.uri=/helloWorld, HTTP.REQUEST=(POST /helloWorld)@1344650582 org.eclipse.jetty.server.Request@5025bd56, HTTP.CONFIG=null, org.apache.cxf.transport.https.CertConstraints=null, Accept=*/*, org.apache.cxf.message.Message.PATH_INFO=/helloWorld, org.apache.cxf.message.Message.BASE_PATH=/helloWorld, org.apache.cxf.continuations.ContinuationProvider=org.apache.cxf.transport.http_jetty.continuations.JettyContinuationProvider@5db18235, org.apache.cxf.message.Message.IN_INTERCEPTORS=[org.apache.cxf.transport.https.CertConstraintsInterceptor@30db95a1], org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@203c9616, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.message.Message.QUERY_STRING=null, HTTP.RESPONSE=HTTP/1.1 200 , org.apache.cxf.security.SecurityContext=org.apache.cxf.transport.http.AbstractHTTPDestination$2@69a18ee2, org.apache.cxf.request.method=POST, org.apache.cxf.async.post.response.dispatch=true, org.apache.cxf.configuration.security.AuthorizationPolicy=null, org.apache.cxf.message.MessageFIXED_PARAMETER_ORDER=false, org.apache.cxf.transport.Destination=org.apache.cxf.transport.http_jetty.JettyHTTPDestination@6dabcd9b, http.base.path=http://localhost:9000, Content-Type=text/xml; charset=UTF-8, HTTP.CONTEXT=ServletContext@o.e.j.s.h.ContextHandler{,null}} null=org.apache.cxf.transport.ChainInitiationObserver@49198ff2 {org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[196], content-type=[text/xml; charset=UTF-8], Host=[localhost:9000], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.7.4]}, HTTP_CONTEXT_MATCH_STRATEGY=stem, org.apache.cxf.request.url=http://localhost:9000/helloWorld, org.apache.cxf.request.uri=/helloWorld, HTTP.REQUEST=(POST /helloWorld)@1344650582 org.eclipse.jetty.server.Request@5025bd56, HTTP.CONFIG=null, org.apache.cxf.transport.https.CertConstraints=null, Accept=*/*, org.apache.cxf.message.Message.PATH_INFO=/helloWorld, org.apache.cxf.message.Message.BASE_PATH=/helloWorld, org.apache.cxf.continuations.ContinuationProvider=org.apache.cxf.transport.http_jetty.continuations.JettyContinuationProvider@5db18235, org.apache.cxf.message.Message.IN_INTERCEPTORS=[org.apache.cxf.transport.https.CertConstraintsInterceptor@30db95a1], org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@203c9616, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.message.Message.QUERY_STRING=null, HTTP.RESPONSE=HTTP/1.1 200 , org.apache.cxf.security.SecurityContext=org.apache.cxf.transport.http.AbstractHTTPDestination$2@69a18ee2, org.apache.cxf.request.method=POST, org.apache.cxf.async.post.response.dispatch=true, org.apache.cxf.configuration.security.AuthorizationPolicy=null, org.apache.cxf.message.MessageFIXED_PARAMETER_ORDER=false, org.apache.cxf.transport.Destination=org.apache.cxf.transport.http_jetty.JettyHTTPDestination@6dabcd9b, http.base.path=http://localhost:9000, Content-Type=text/xml; charset=UTF-8, HTTP.CONTEXT=ServletContext@o.e.j.s.h.ContextHandler{,null}}=null null=null ============handleMessage========== {javax.xml.ws.wsdl.port={http://service.iflytek.com/}HelloWorldServicePort, org.apache.cxf.ws.policy.EffectivePolicy=org.apache.cxf.ws.policy.EffectivePolicyImpl@4cd297c0, org.apache.cxf.service.model.MessageInfo=[MessageInfo OUTPUT: {http://service.iflytek.com/}sayHelloResponse], http.headers.copied=true, wrote.envelope.start=true, org.apache.cxf.message.Message.PROTOCOL_HEADERS={}, javax.xml.ws.wsdl.service={http://service.iflytek.com/}HelloWorldServiceService, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@203c9616, HTTP.RESPONSE=HTTP/1.1 200 Content-Type: text/xml;charset=UTF-8 , javax.xml.ws.wsdl.interface={http://service.iflytek.com/}HelloWorldService, org.apache.cxf.mime.headers={}, javax.xml.ws.wsdl.operation={http://service.iflytek.com/}sayHello, javax.xml.ws.wsdl.description=http://localhost:9000/helloWorld?wsdl, org.apache.cxf.service.model.BindingMessageInfo=org.apache.cxf.service.model.BindingMessageInfo@15e8e5a7, Content-Type=text/xml, org.apache.cxf.headers.Header.list=[]} {javax.xml.ws.wsdl.port={http://service.iflytek.com/}HelloWorldServicePort, org.apache.cxf.service.model.MessageInfo=[MessageInfo INPUT: {http://service.iflytek.com/}sayHello], org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[196], content-type=[text/xml; charset=UTF-8], Host=[localhost:9000], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.7.4]}, HTTP_CONTEXT_MATCH_STRATEGY=stem, org.apache.cxf.request.url=http://localhost:9000/helloWorld, javax.xml.ws.wsdl.interface={http://service.iflytek.com/}HelloWorldService, org.apache.cxf.request.uri=/helloWorld, HTTP.REQUEST=(POST /helloWorld)@1344650582 org.eclipse.jetty.server.Request@5025bd56, HTTP.CONFIG=null, org.apache.cxf.transport.https.CertConstraints=null, Accept=*/*, org.apache.cxf.headers.Header.list=[], org.apache.cxf.message.Message.PATH_INFO=/helloWorld, org.apache.cxf.message.Message.BASE_PATH=/helloWorld, org.apache.cxf.continuations.ContinuationProvider=org.apache.cxf.transport.http_jetty.continuations.JettyContinuationProvider@5db18235, javax.xml.ws.wsdl.service={http://service.iflytek.com/}HelloWorldServiceService, org.apache.cxf.message.Message.IN_INTERCEPTORS=[org.apache.cxf.transport.https.CertConstraintsInterceptor@30db95a1], org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@203c9616, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.message.Message.QUERY_STRING=null, HTTP.RESPONSE=HTTP/1.1 200 Content-Type: text/xml;charset=UTF-8 , org.apache.cxf.security.SecurityContext=org.apache.cxf.transport.http.AbstractHTTPDestination$2@69a18ee2, org.apache.cxf.request.method=POST, org.apache.cxf.async.post.response.dispatch=true, org.apache.cxf.configuration.security.AuthorizationPolicy=null, javax.xml.ws.wsdl.operation={http://service.iflytek.com/}sayHello, org.apache.cxf.message.MessageFIXED_PARAMETER_ORDER=false, org.apache.cxf.transport.Destination=org.apache.cxf.transport.http_jetty.JettyHTTPDestination@6dabcd9b, org.apache.cxf.jaxws.context.WrappedMessageContext.SCOPES={}, javax.xml.ws.wsdl.description=http://localhost:9000/helloWorld?wsdl, http.base.path=http://localhost:9000, Content-Type=text/xml; charset=UTF-8, HTTP.CONTEXT=ServletContext@o.e.j.s.h.ContextHandler{,null}}=null {javax.xml.ws.wsdl.port={http://service.iflytek.com/}HelloWorldServicePort, org.apache.cxf.ws.policy.EffectivePolicy=org.apache.cxf.ws.policy.EffectivePolicyImpl@4cd297c0, org.apache.cxf.service.model.MessageInfo=[MessageInfo OUTPUT: {http://service.iflytek.com/}sayHelloResponse], http.headers.copied=true, wrote.envelope.start=true, org.apache.cxf.message.Message.PROTOCOL_HEADERS={}, javax.xml.ws.wsdl.service={http://service.iflytek.com/}HelloWorldServiceService, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@203c9616, HTTP.RESPONSE=HTTP/1.1 200 Content-Type: text/xml;charset=UTF-8 , javax.xml.ws.wsdl.interface={http://service.iflytek.com/}HelloWorldService, org.apache.cxf.mime.headers={}, javax.xml.ws.wsdl.operation={http://service.iflytek.com/}sayHello, javax.xml.ws.wsdl.description=http://localhost:9000/helloWorld?wsdl, org.apache.cxf.service.model.BindingMessageInfo=org.apache.cxf.service.model.BindingMessageInfo@15e8e5a7, Content-Type=text/xml, org.apache.cxf.headers.Header.list=[]}=null
相关推荐
接下来是CXF对Interceptor(拦截器)的支持。拦截器是一种强大的机制,可以在消息的发送和接收过程中插入自定义逻辑。它们可以用于日志记录、安全检查、性能监控等。在2.1.4版本中,你可以学习如何定义和配置拦截器...
**标题:“CXF和Spring整合,并且添加拦截器”** 在Java世界中,Apache CXF是一个流行的开源服务框架,用于创建和消费Web服务。它支持多种Web服务规范,包括SOAP、RESTful API以及WS-*标准。另一方面,Spring框架是...
本文将深入探讨"CXF拦截器-权限控制-登录校验"这一主题。 首先,我们需要了解什么是拦截器。在CXF框架中,拦截器是实现业务逻辑和基础架构之间解耦的一种方式。它们可以插入到服务调用的生命周期中,在消息发送前或...
ssh2-interceptor拦截器(权限管理).
标题:“CXF拦截器(Interceptor)的使用” 描述:“讲解了cxf实现拦截器的原因、核心API及使用方法” 在深入探讨CXF拦截器的使用之前,我们首先需要理解其在CXF框架中的核心作用与价值。Apache CXF是一个开源框架,...
Apache CXF的拦截器机制提供了一种灵活的方式,使得开发者能够对Web服务的调用过程进行深度定制。了解并熟练掌握拦截器的使用,对于提升Web服务的质量和效率具有重要意义。通过上述介绍,我们了解了拦截器的理论基础...
2. **自定义拦截器实现**:你可以通过实现`org.apache.cxf.interceptor.Fault`和`org.apache.cxf.phase.Phase`接口来自定义拦截器。`Phase`接口定义了拦截器执行的阶段,如PRE_INVOKE、POST_INVOCATION等。每个拦截...
1. 创建拦截器:首先,我们需要创建自定义拦截器类,实现`org.apache.cxf.interceptor.Interceptor`接口或者其子接口,如`org.apache.cxf.interceptor.ClientInterceptor`或`org.apache.cxf.interceptor....
本篇文章将深入探讨如何使用CXF来开发具有权限控制功能的Web Service,并通过拦截器实现这一目标。 首先,我们需要理解Web Service拦截器的概念。在CXF中,拦截器是处理消息生命周期中的关键组件,它们可以在消息...
**三、CXF 对 Interceptor 拦截器的支持** CXF支持拦截器(Interceptor)机制,允许在服务处理流程中插入自定义逻辑。拦截器可以用于日志记录、安全检查、性能监控等场景。通过实现`org.apache.cxf.interceptor....
当我们需要在CXF和Spring整合的基础上发布Web服务,并对权限进行控制时,可以利用拦截器来实现这一目标。本文将详细介绍如何为CXF与Spring整合发布WebService添加拦截器进行权限控制。 首先,我们需要理解CXF拦截器...
提供的压缩包文件名"WebService_CXF_Interceptor_Client_2"和"Webservice_CXF_Interceptor_Server_2"可能包含与CXF Web服务客户端和服务器端拦截器相关的示例代码或配置。通过查看这些文件,你可以更深入地理解如何...
如果权限检查失败,拦截器应该抛出一个适当的异常,例如`org.apache.cxf.interceptor.Fault`,并附带错误信息。这样,CXF会捕获这个异常,并根据配置的策略返回相应的错误响应给客户端。 5. **测试与调试**: ...
本篇将深入探讨CXF框架中的拦截器(Interceptor)及其在"webservice的cxf框架拦截器demo"中的应用。 拦截器在CXF中是一种强大的工具,它允许开发者在消息发送或接收的过程中插入自定义的行为。这在处理认证、日志...
CXF是一个流行的开源框架,它支持SOAP和RESTful两种Web Service风格,并提供了丰富的功能,包括安全、拦截器等。本节我们将深入探讨如何为CXF客户端添加自定义拦截器以实现权限控制。 首先,理解拦截器的概念是至关...
在CXF服务端,你可以通过实现`org.apache.cxf.interceptor.Fault`和`org.apache.cxf.phase.PhaseInterceptorChain`接口的类来创建自定义拦截器,并将它们添加到服务的拦截器链中。 4. **“inter_client”**: 这...
其中,拦截器(Interceptor)是CXF框架中的一个重要概念,允许我们在消息传递的过程中插入自定义逻辑。在这个"CxfInterceptor_CS"压缩包中,我们很可能会找到客户端和服务器端的拦截器实现示例。 拦截器是CXF中一种...
本篇文章将深入探讨如何利用CXF的Interceptor(拦截器)和Feature(特性)功能,提升我们的服务开发体验。 首先,Interceptor是CXF框架中的一个核心组件,它允许我们在服务调用的生命周期中插入自定义的行为。拦截...