CXF添加拦截器和自定义拦截器
前面讲了如何采用CXF开发webservice,现在来讲如何添加拦截器和自定义拦截器。
服务端代码:
HelloWorld implementor=new HelloWorldImpl();
String address="http://192.xxx.15.117:8089/helloWorld";
// Endpoint.publish(address, implementor); // jdk实现 暴露webservice接口
JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
factoryBean.setAddress(address); // 设置暴露地址
factoryBean.setServiceClass(HelloWorld.class); // 接口类
factoryBean.setServiceBean(implementor); // 设置实现类
factoryBean.getInInterceptors().add(new LoggingInInterceptor()); // 添加in拦截器 日志拦截器
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); // 添加out拦截器 日志拦截器
factoryBean.getInInterceptors().add(new MyInterceptor()); //自定义拦截器
factoryBean.create(); // 创建webservice接口
自定义拦截器类:
public class MyInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
public MyInterceptor() {
super(Phase.PRE_INVOKE); // 在调用方法之前调用自定拦截器
}
@SuppressWarnings("null")
public void handleMessage(SoapMessage message) throws Fault {
List<Header> headers=message.getHeaders();
if(headers==null && headers.size()==0){
throw new Fault(new IllegalArgumentException("没有Header,拦截器实施拦截"));
}
Header firstHeader=headers.get(0);
Element ele=(Element) firstHeader.getObject();
NodeList uList=ele.getElementsByTagName("userName");
NodeList pList=ele.getElementsByTagName("password");
if(uList.getLength()!=1){
throw new Fault(new IllegalArgumentException("用户名格式不对"));
}
if(pList.getLength()!=1){
throw new Fault(new IllegalArgumentException("密码格式不对"));
}
String userName=uList.item(0).getTextContent();
String password=pList.item(0).getTextContent();
if(!userName.equals("java1234")||!password.equals("123456")){
throw new Fault(new IllegalArgumentException("用户名或者密码错误!"));
}
}
}
客户端代码:
先通过输入命令: wsdl2java http://192.xxx.15.117:8089/helloWorld?wsdl 生成需要的代码,谈话编写调用方法。
HelloWorldImplService service=new HelloWorldImplService();
HelloWorld helloWorld=service.getHelloWorldImplPort();
org.apache.cxf.endpoint.Client client=ClientProxy.getClient(helloWorld); client.getOutInterceptors().add(new AddHeaderInterceptor("java1234","123")); // 添加自定义拦截器
client.getInInterceptors().add(new LoggingInInterceptor()); // 添加In拦截器 日志拦截器
client.getOutInterceptors().add(new LoggingOutInterceptor()); // 添加Out拦截器 日志拦截器
自定义拦截器:
public class AddHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
private String userName;
private String password;
public AddHeaderInterceptor(String userName,String password) {
super(Phase.PREPARE_SEND); // 准备发送SOAP消息的时候调用拦截器
this.userName=userName;
this.password=password;
}
public void handleMessage(SoapMessage message) throws Fault {
List<Header> headerList=message.getHeaders();
Document doc=DOMUtils.createDocument();
Element ele=doc.createElement("authHeader");
Element uElement=doc.createElement("userName");
uElement.setTextContent(userName);
Element pElement=doc.createElement("password");
pElement.setTextContent(password);
ele.appendChild(uElement);
ele.appendChild(pElement);
headerList.add(new Header(new QName("java1234"),ele));
}
}
完成!
CXF添加拦截器和自定义拦截器的更多相关文章
- flask之web网关、三件套、配置、路由(参数、转化器及自定义转化器)、cbv、模板语言、session
目录 1.wsgiref.py 2.werzeug.py 3.三件套 4.配置文件 5.路由本质 6.cbv.py 7.路由转化器 8.自定义转化器 9.模板语言 10.session原理 11.te ...
- Elasticsearch修改分词器以及自定义分词器
Elasticsearch修改分词器以及自定义分词器 参考博客:https://blog.csdn.net/shuimofengyang/article/details/88973597
- WebService -- Java 实现之 CXF ( 添加系统预定义的拦截器)
1. 概述 CXF允许我们在webservice的in/out位置添加拦截器.拦截器有两大分类,一类是系统预定义的:另一类是自定义拦截器. 2. 在server端添加拦截器. JaxWsServerF ...
- struts2内置拦截器和自定义拦截器详解(附源码)
一.Struts2内置拦截器 Struts2中内置类许多的拦截器,它们提供了许多Struts2的核心功能和可选的高级特 性.这些内置的拦截器在struts-default.xml中配置.只有配置了拦截 ...
- 使用struts2中默认的拦截器以及自定义拦截器
转自:http://blog.sina.com.cn/s/blog_82f01d350101echs.html 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Acti ...
- 从struts2拦截器到自定义拦截器
拦截器可谓struts2的核心了,最基本的bean的注入就是通过默认的拦截器实现的,一般在struts2.xml的配置中,package内直接或间接继承了struts-default.xml,这样st ...
- Struts2第七篇【介绍拦截器、自定义拦截器、执行流程、应用】
什么是拦截器 拦截器Interceptor-..拦截器是Struts的概念,它与过滤器是类似的-可以近似于看作是过滤器 为什么我们要使用拦截器 前面在介绍Struts的时候已经讲解过了,Struts为 ...
- 初学者易上手的SSH-struts2 05拦截器与自定义拦截器
因为自己对于struts2也不是很了解,这章将是struts2的最后一章了.那么这一章主要介绍的是拦截器以及怎么样来自定义一个拦截器. struts2的拦截器位于struts2-core(核心包)-& ...
- struts2(五)之struts2拦截器与自定义拦截器
前言 前面介绍了struts2的输入验证,如果让我自己选的话,肯定是选择xml配置校验的方法,因为,能使用struts2中的一些校验规则,就无需自己编写了, 不过到后面应该都有其他更方便的校验方法,而 ...
随机推荐
- asp.net core 一个中小型项目实战的起手式——项目搭建与仓储模式下的持久层创建(1)
常规的中小型项目搭建方式一般是三层架构加上mvc与webapi作为一个主要框架,再加上一些第三方库,例如orm框架(EF.SqlSugar.Dapper等),API文档工具(Swagger)这些的应用 ...
- python random 模块及验证码功能
random模块 import random random.random() random.randint(1,3) # 1-3的整数包括3 import random print(random.ra ...
- 使用re.split 按标点+空格的一种分割办法
import re import string t1 = re.split("["+string.punctuation+" ]","(555) 12 ...
- (三十三)c#Winform自定义控件-日期控件
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- 入门MySQL——查询语法练习
前言: 前面几篇文章为大家介绍了DML以及DDL语句的使用方法,本篇文章将主要讲述常用的查询语法.其实MySQL官网给出了多个示例数据库供大家实用查询,下面我们以最常用的员工示例数据库为准,详细介绍各 ...
- laya2d 与 cad 之间的坐标转换
坐标系基本概念 直角坐标系可分为左手坐标系与右手坐标系,cad 中用到的是右手坐标系, Laya2D 中用到的是左手坐标系, Laya3D 中使用右手坐标系. 那么如何判断二维直角坐标系是左手还是右手 ...
- Python|队列Queue
一 前言 本文算是一次队列的学习笔记,Queue 模块实现了三种类型的队列,它们的区别仅仅是队列中元素被取回的顺序.在 FIFO 队列中,先添加的任务先取回.在 LIFO 队列中,最近被添加的元素先取 ...
- Selenium3 + Python3自动化测试系列十——调用JavaScript代码
调用JavaScript代码 一.调用JavaScript代码方法 Selenium在对浏览器操作时会有自动化代码中不稳定的部分,经常出错的部分,可以将这部分对网页元素进行操作的代码换成对应的Java ...
- unity_实用小技巧(const)
const:声明某个常量字段或常量局部变量. 注意:常量字段和常量局部变量不是变量并且不能修改 利用const管理游戏标签 例如: //管理所有标签 public const string Pl ...
- Android进阶之路(1)-详解MVC
最近因为换工作的原因没有写博客,现在慢慢稳定了,我准备写一些关于Android 进阶的文章,也是为了督促自己学习,大家一起进步! 今天详细的分析一下Android APP架构之一:MVC ### MV ...