CXF已经内置了一些拦截器,这些拦截器大部分默认加入到拦截器链中,有些拦截器也能够手动加入,如手动加入CXF提供的日志拦截器。也能够自己定义拦截器。CXF中实现自己定义拦截器非常easy。仅仅要继承AbstractPhaseInterceptor或者AbstractPhaseInterceptor的子类(如AbstractSoapInterceptor)就可以。

自己定义权限认证拦截器

权限认证拦截器处理SOAPHeader中的认证信息,client在发起请求时在SOAPHeader中加入认证信息,服务端在接收到请求后,校验认证信息,校验通过则继续运行,校验不通过则返回错误。

  1. <!-- 认证信息格式例如以下 -->
  2. <auth xmlns="http://www.tmp.com/auth">
  3. <name>admin</name>
  4. <password>admin</password>
  5. </auth>

client加入授权拦截器

  1. import java.util.List;
  2. import javax.xml.namespace.QName;
  3. import org.apache.cxf.binding.soap.SoapMessage;
  4. import org.apache.cxf.headers.Header;
  5. import org.apache.cxf.helpers.DOMUtils;
  6. import org.apache.cxf.interceptor.Fault;
  7. import org.apache.cxf.phase.AbstractPhaseInterceptor;
  8. import org.apache.cxf.phase.Phase;
  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.Element;
  11. /**
  12. * 加入授权拦截器
  13. * 用于在client发请求时加入授权
  14. * @author accountwcx@qq.com
  15. *
  16. */
  17. public class AuthAddInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
  18. public AuthAddInterceptor(){
  19. //准备发送阶段
  20. super(Phase.PREPARE_SEND);
  21. }
  22. @Override
  23. public void handleMessage(SoapMessage message) throws Fault {
  24. List<Header> headers = message.getHeaders();
  25. Document doc = DOMUtils.createDocument();
  26. //Element auth = doc.createElement("auth");
  27. Element auth = doc.createElementNS("http://www.tmp.com/auth", "auth");
  28. Element name = doc.createElement("name");
  29. name.setTextContent("admin");
  30. Element password = doc.createElement("password");
  31. password.setTextContent("admin");
  32. auth.appendChild(name);
  33. auth.appendChild(password);
  34. headers.add(new Header(new QName(""), auth));
  35. }
  36. }

服务端授权验证拦截器

  1. import java.util.List;
  2. import javax.xml.namespace.QName;
  3. import org.apache.cxf.binding.soap.SoapMessage;
  4. import org.apache.cxf.headers.Header;
  5. import org.apache.cxf.interceptor.Fault;
  6. import org.apache.cxf.phase.AbstractPhaseInterceptor;
  7. import org.apache.cxf.phase.Phase;
  8. import org.w3c.dom.Element;
  9. import org.w3c.dom.NodeList;
  10. /**
  11. * 服务端输入拦截器
  12. * 拦截请求有没有授权信息
  13. * @author accountwcx@qq.com
  14. *
  15. */
  16. public class AuthValidateInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
  17. public AuthValidateInterceptor(){
  18. super(Phase.PRE_INVOKE);
  19. }
  20. @Override
  21. public void handleMessage(SoapMessage message) throws Fault {
  22. List<Header> headers = message.getHeaders();
  23. if(headers == null || headers.size() < 1) {
  24. throw new Fault(new Exception("无授权信息。"));
  25. }
  26. Element auth = null;
  27. //获取授权信息元素
  28. for(Header header : headers){
  29. QName qname = header.getName();
  30. String ns = qname.getNamespaceURI();
  31. String tagName = qname.getLocalPart();
  32. if(ns != null && ns.equals("http://www.tmp.com/auth") && tagName != null && tagName.equals("auth")){
  33. auth = (Element)header.getObject();
  34. break;
  35. }
  36. }
  37. //假设授权信息元素不存在。提示错误
  38. if(auth == null){
  39. throw new Fault(new Exception("无授权信息!"));
  40. }
  41. NodeList nameList = auth.getElementsByTagName("name");
  42. NodeList pwdList = auth.getElementsByTagName("password");
  43. if(nameList.getLength() != 1 || pwdList.getLength() != 1){
  44. throw new Fault(new Exception("授权信息错误!
  45. "));
  46. }
  47. String name = nameList.item(0).getTextContent();
  48. String password = pwdList.item(0).getTextContent();
  49. if(!"admin".equals(name) || !"admin".equals(password)){
  50. throw new Fault(new Exception("授权信息错误。"));
  51. }
  52. }
  53. }

服务端拦截器配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jaxws="http://cxf.apache.org/jaxws"
  5. xmlns:soap="http://cxf.apache.org/bindings/soap"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
  9. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  10. <jaxws:endpoint id="helloWSEndpoint" implementor="#helloWS" address="/hello">
  11. <jaxws:inInterceptors>
  12. <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
  13. <bean class="com.rvho.cxfserver.interceptor.AuthValidateInterceptor"></bean>
  14. </jaxws:inInterceptors>
  15. <jaxws:outInterceptors>
  16. <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
  17. </jaxws:outInterceptors>
  18. </jaxws:endpoint>
  19. </beans>

client请求

  1. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  2. factory.setServiceClass(HelloWS.class);
  3. factory.setAddress("http://localhost:8280/cxfserver/services/hello");
  4. factory.getInInterceptors().add(new org.apache.cxf.interceptor.LoggingInInterceptor());
  5. //client授权拦截器
  6. factory.getOutInterceptors().add(new com.rvho.cxfclient.interceptor.AuthAddInterceptor());
  7. factory.getOutInterceptors().add(new org.apache.cxf.interceptor.LoggingOutInterceptor());
  8. HelloWS helloWS = factory.create(HelloWS.class);
  9. String welcome = helloWS.welcome("accountwcx@qq.com");

CXF日志拦截器

CXF提供了输入日志拦截器LoggingInInterceptor和输出日志拦截器LoggingOutInterceptor,日志拦截器能够用在服务端也能够用在client。在測试或者调试的时候。能够用日志拦截器输出服务端、client请求和接收到的信息。

服务端日志内容

  1. 七月 30, 2015 10:51:37 上午 org.apache.cxf.services.HelloWSService.HelloWSPort.HelloWS
  2. 信息: Inbound Message
  3. ----------------------------
  4. ID: 1
  5. Address: http://localhost:8280/cxfserver/services/hello
  6. Encoding: UTF-8
  7. Http-Method: POST
  8. Content-Type: text/xml; charset=UTF-8
  9. Headers: {Accept=[*/*], cache-control=[no-cache], connection=[keep-alive], Content-Length=[212], content-type=[text/xml; charset=UTF-8], host=[localhost:8280], pragma=[no-cache], SOAPAction=[""], user-agent=[Apache CXF 3.1.1]}
  10. Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:welcome xmlns:ns2="http://www.tmp.com/services/hello"><name>accountwcx@qq.com</name></ns2:welcome></soap:Body></soap:Envelope>
  11. --------------------------------------
  12. 七月 30, 2015 10:51:37 上午 org.apache.cxf.services.HelloWSService.HelloWSPort.HelloWS
  13. 信息: Outbound Message
  14. ---------------------------
  15. ID: 1
  16. Response-Code: 200
  17. Encoding: UTF-8
  18. Content-Type: text/xml
  19. Headers: {}
  20. Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:welcomeResponse xmlns:ns2="http://www.tmp.com/services/hello"><return>欢迎使用CXF!
  21. accountwcx@qq.com</return></ns2:welcomeResponse></soap:Body></soap:Envelope>
  22. --------------------------------------

client日志内容

  1. 七月 30, 2015 10:51:37 上午 org.apache.cxf.services.HelloWSService.HelloWSPort.HelloWS
  2. 信息: Outbound Message
  3. ---------------------------
  4. ID: 1
  5. Address: http://localhost:8280/cxfserver/services/hello
  6. Encoding: UTF-8
  7. Http-Method: POST
  8. Content-Type: text/xml
  9. Headers: {Accept=[*/*], SOAPAction=[""]}
  10. Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:welcome xmlns:ns2="http://www.tmp.com/services/hello"><name>accountwcx@qq.com</name></ns2:welcome></soap:Body></soap:Envelope>
  11. --------------------------------------
  12. 七月 30, 2015 10:51:37 上午 org.apache.cxf.services.HelloWSService.HelloWSPort.HelloWS
  13. 信息: Inbound Message
  14. ----------------------------
  15. ID: 1
  16. Response-Code: 200
  17. Encoding: UTF-8
  18. Content-Type: text/xml;charset=UTF-8
  19. Headers: {content-type=[text/xml;charset=UTF-8], Date=[Thu, 30 Jul 2015 02:51:37 GMT], Server=[Apache-Coyote/1.1], transfer-encoding=[chunked]}
  20. Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:welcomeResponse xmlns:ns2="http://www.tmp.com/services/hello"><return>欢迎使用CXF!accountwcx@qq.com</return></ns2:welcomeResponse></soap:Body></soap:Envelope>
  21. --------------------------------------
  22. 欢迎使用CXF!
  23. accountwcx@qq.com

CXF实战之自己定义拦截器(五)的更多相关文章

  1. Struct2_定义拦截器并使用注解方式作用在Action的方法中

    一.目的:通过在方法上加注解控制哪些方法需要登陆后才能访问   二.方式:利用拦截器判断用户是否登陆   三.实现步骤 定义配置文件struts.xml添加节点 1 2 3 4 5 6 7 8 9 1 ...

  2. struts自己定义拦截器--登录权限控制

    说明:该自己定义的拦截器实现用户登录的权限控制. login.jsp--->LoginAction--重定向-->MainAction--->main.jsp 一.1.整体的步骤: ...

  3. struts2学习笔记(5)---自己定义拦截器

    什么是拦截器? struts2中拦截器分为Struts2定义好的拦截器和自己定义的拦截器. 其作用是在一个Action运行之前进行拦截,在Action运行之后又增加某些操作. 实现原理 当请求一个Ac ...

  4. Struts2通过自己定义拦截器实现登录之后跳转到原页面

    这个功能对用户体验来说是非常重要的.实现起来事实上非常easy. 拦截器的代码例如以下: package go.derek.advice; import go.derek.entity.User; i ...

  5. jquery.ajax与axios及定义拦截器

    首先导入jquery和axios包 jquery.ajax function reg(){ var username = $("#username").val(); var pas ...

  6. springmvc自己定义拦截器

    Spring MVC也能够使用拦截器对请求进行拦截处理,用户能够自己定义拦截器来实现特定的功能,自己定义的拦截器必须实现HandlerInterceptor接口. 直接看下样例: package co ...

  7. Struts2自己定义拦截器实例—登陆权限验证

    版本号:struts2.1.6 此实例实现功能:用户须要指定username登陆,登陆成功进入对应页面运行操作,否则返回到登陆页面进行登陆,当直接訪问操作页面(登陆后才干訪问的页面)时则不同意,须返回 ...

  8. Dora.Interception,为.NET Core度身打造的AOP框架 [2]:以约定的方式定义拦截器

    上一篇<更加简练的编程体验>提供了最新版本的Dora.Interception代码的AOP编程体验,接下来我们会这AOP框架的编程模式进行详细介绍,本篇文章着重关注的是拦截器的定义.采用“ ...

  9. springboot-12-自定义拦截器的配置interceptor

    springmvc中拦截器的概念已经被弱化了, springboot中使用的也不甚广泛, 通常在用户登录等方面仍有用处 创建拦截器步骤: , 创建拦截器类继承HandlerInterceptor , ...

随机推荐

  1. 开源编辑器ueditor

    http://ueditor.baidu.com/website/onlinedemo.html

  2. one day php. alomost all;

    <? namespace Test; use \PhpProject\PhpApp as Other; $u=new Other("ns test"); echo $u-&g ...

  3. 呕心沥血之作:完美解决Informix的中文乱码问题

    Informix是IBM旗下的一款数据库,要不是这个项目需要,估计这辈子我都不知道居然还有这么一款数据库.想来公司的项目遍布全国各地,各种部署环境各种应用场景应有尽有,七七八八的问了一大堆的各项目组兄 ...

  4. AC日记——爱改名的小融 codevs 2967

    2967 爱改名的小融  时间限制: 1 s  空间限制: 16000 KB  题目等级 : 白银 Silver 题解       题目描述 Description Wikioi上有个人叫小融,他喜欢 ...

  5. 洛谷—— P3807 【模板】卢卡斯定理

    https://www.luogu.org/problemnew/show/3807 题目背景 这是一道模板题. 题目描述 给定n,m,p(1\le n,m,p\le 10^51≤n,m,p≤105) ...

  6. 第2章 Spring Boot 文档

    Spring Boot 文档 本节简要介绍了Spring Boot文档,是整个文档的参考指南. 您可以完整阅读本参考指南,或者如果您不感兴趣的话可以跳过该部分. 1. 关于文档 Spring Boot ...

  7. TCP11种状态

    2.全部11种状态 2.1.客户端独有的:(1)SYN_SENT (2)FIN_WAIT1 (3)FIN_WAIT2 (4)CLOSING (5)TIME_WAIT . 2.2.服务器独有的:(1)L ...

  8. extern “C”的使用

    2016-12-11   22:40:48 VS编译的时候,可以指定编译为C代码或者C++代码.c/c++->高级.而当你新建一个cpp文件时,VS很有可能自动会把编译方式由C变成C++编译.然 ...

  9. Gvim 和 Opencv编译

    好奇看了一下Gvim编译器:在官网上也有介绍,github上有源码,安装在电脑上,感觉需求不大,记那些命令也太多了.然后编译opencv过程中cmake成功了,但是VS下编译报了很多错,准备不搞这些了 ...

  10. 更改已经签名的app中的内容

    转载请说明出处http://blog.csdn.net/andywuchuanlong 记得上次在南昌中兴的一个项目中遇到过一个这种需求:一个app能够给多个渠道商去运营,渠道商推广出去能够获得对应的 ...