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

自己定义权限认证拦截器

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

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

client加入授权拦截器

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element; /**
* 加入授权拦截器
* 用于在client发请求时加入授权
* @author accountwcx@qq.com
*
*/
public class AuthAddInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
public AuthAddInterceptor(){
//准备发送阶段
super(Phase.PREPARE_SEND);
} @Override
public void handleMessage(SoapMessage message) throws Fault {
List<Header> headers = message.getHeaders(); Document doc = DOMUtils.createDocument(); //Element auth = doc.createElement("auth");
Element auth = doc.createElementNS("http://www.tmp.com/auth", "auth"); Element name = doc.createElement("name");
name.setTextContent("admin"); Element password = doc.createElement("password");
password.setTextContent("admin"); auth.appendChild(name);
auth.appendChild(password); headers.add(new Header(new QName(""), auth));
}
}

服务端授权验证拦截器

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList; /**
* 服务端输入拦截器
* 拦截请求有没有授权信息
* @author accountwcx@qq.com
*
*/
public class AuthValidateInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
public AuthValidateInterceptor(){
super(Phase.PRE_INVOKE);
} @Override
public void handleMessage(SoapMessage message) throws Fault {
List<Header> headers = message.getHeaders();
if(headers == null || headers.size() < 1) {
throw new Fault(new Exception("无授权信息。"));
} Element auth = null;
//获取授权信息元素
for(Header header : headers){
QName qname = header.getName();
String ns = qname.getNamespaceURI();
String tagName = qname.getLocalPart();
if(ns != null && ns.equals("http://www.tmp.com/auth") && tagName != null && tagName.equals("auth")){
auth = (Element)header.getObject();
break;
}
} //假设授权信息元素不存在。提示错误
if(auth == null){
throw new Fault(new Exception("无授权信息!"));
} NodeList nameList = auth.getElementsByTagName("name");
NodeList pwdList = auth.getElementsByTagName("password");
if(nameList.getLength() != 1 || pwdList.getLength() != 1){
throw new Fault(new Exception("授权信息错误! "));
} String name = nameList.item(0).getTextContent();
String password = pwdList.item(0).getTextContent();
if(!"admin".equals(name) || !"admin".equals(password)){
throw new Fault(new Exception("授权信息错误。"));
}
}
}

服务端拦截器配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:endpoint id="helloWSEndpoint" implementor="#helloWS" address="/hello">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
<bean class="com.rvho.cxfserver.interceptor.AuthValidateInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>
</beans>

client请求

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

CXF日志拦截器

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

服务端日志内容

七月 30, 2015 10:51:37 上午 org.apache.cxf.services.HelloWSService.HelloWSPort.HelloWS
信息: Inbound Message
----------------------------
ID: 1
Address: http://localhost:8280/cxfserver/services/hello
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
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]}
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>
-------------------------------------- 七月 30, 2015 10:51:37 上午 org.apache.cxf.services.HelloWSService.HelloWSPort.HelloWS
信息: Outbound Message
---------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
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>
--------------------------------------

client日志内容

七月 30, 2015 10:51:37 上午 org.apache.cxf.services.HelloWSService.HelloWSPort.HelloWS
信息: Outbound Message
---------------------------
ID: 1
Address: http://localhost:8280/cxfserver/services/hello
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:welcome xmlns:ns2="http://www.tmp.com/services/hello"><name>accountwcx@qq.com</name></ns2:welcome></soap:Body></soap:Envelope>
--------------------------------------
七月 30, 2015 10:51:37 上午 org.apache.cxf.services.HelloWSService.HelloWSPort.HelloWS
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
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]}
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>
--------------------------------------
欢迎使用CXF! 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. poj 3468 线段树成段更新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 54012   ...

  2. 古代猪文 BZOJ 1951

    古代猪文 [问题描述] “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心……” ——选自猪王国民歌 很久很久以前,在山的那边 ...

  3. 模仿锤子手机的bigbang效果

    <!DOCTYPE html> <html style="height: 100%"> <head> <meta http-equiv=& ...

  4. The disk contains an unclean file system

    Ubuntu : Status 14: The disk contains an unclean file system By mkyong | July 23, 2014 | Viewed : 10 ...

  5. toUpperCase与toLowerCase

    public String toLowerCase()此方法返回的字符串转换为小写. public String toUpperCase()此方法返回的字符串转换为大写. 注:支队英文字符有效,其他字 ...

  6. MySQL的一个麻烦事

    1. 开启一个MySQL连接,在这个连接中发起一个事务,进行一些操作但不提交 2. 拔网线 3. 重连网线,再开启一个MySQL连接,执行delete操作,发现stpe 1中占用的资源没有被释放 4. ...

  7. java布局(每个名字都是有意义的)

    一.FlowLayout 1.流水布局:从左至右,排满换行 2.构造函数有三种: (1)FlowLayout() (2)FlowLayout(align) (3)FlowLayout(align, h ...

  8. Golang协程与通道整理

    协程goroutine        不由OS调度,而是用户层自行释放CPU,从而在执行体之间切换.Go在底层进行协助实现      涉及系统调用的地方由Go标准库协助释放CPU      总之,不通 ...

  9. mac 安装scrapy

    https://jingyan.baidu.com/article/14bd256e748346bb6d2612be.html 1.安装Python 安装完了记得配置环境,将python目录和pyth ...

  10. 从头写一个Cucumber测试(一) Selenium Test

    转载:https://yaowenjie.github.io/%E7%BC%96%E7%A8%8B%E7%9B%B8%E5%85%B3/cucumber-test, 背景(废话不读系列)   前段时间 ...