使用CXF开发WebService程序的总结(六):结合拦截器使用
1. 使用CXF提供的拦截器
package com.lonely.server.impl; import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.lonely.server.HelloWs; public class ReleaseClient { public static void main(String[] args) {
System.out.println("WS 服务端 start~~~~~~");
String address = "http://localhost:8090/sayhello";
HelloWs helloWs = new HelloWsImpl();
JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
// 设置地址
jaxWsServerFactoryBean.setAddress(address);
// 设置接口
jaxWsServerFactoryBean.setServiceClass(HelloWs.class);
// 设置实现类
jaxWsServerFactoryBean.setServiceBean(helloWs); // 添加 in 日志拦截器
jaxWsServerFactoryBean.getInInterceptors().add(new LoggingInInterceptor());
// 添加 out 日志拦截器
jaxWsServerFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); jaxWsServerFactoryBean.create();
System.out.println("WS 服务端 started~~~~~~~");
}
}
1.2 启动发布
package com.lonely.client; import java.util.List; import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor; import com.lonely.server.HelloWs;
import com.lonely.server.HelloWsService;
import com.lonely.server.MyClass;
import com.lonely.server.MyClassArray;
import com.lonely.server.User; public class HelloInvoking { public static void main(String[] args) {
HelloWsService helloWsService = new HelloWsService();
HelloWs helloWs = helloWsService.getHelloWsPort();
// System.out.println(helloWs.sayHelloWs("dugu")); /*
* Clazz clazz = new Clazz(); clazz.setClassId(1); List<User> list =
* helloWs.findUsersByClassId(clazz); for (User user : list) {
* System.out.println(user.getUsername() + ":" + user.getClassId()); }
*/ // 获取 Client对象,来获取拦截器
Client client = ClientProxy.getClient(helloWs);
// 添加 in 日志拦截器
client.getInInterceptors().add(new LoggingInInterceptor());
// 添加 out 日志拦截器
client.getOutInterceptors().add(new LoggingOutInterceptor()); MyClassArray myClassArray = helloWs.findAllUsers();
List<MyClass> list = myClassArray.getItem();
for (MyClass myClass : list) {
System.out.println("班级:" + myClass.getKey());
List<User> users = myClass.getValue();
for (User user : users) {
System.out.println("\t学生:" + user.getUsername());
}
}
}
}
1.4 运行看效果,至此,cxf提供的拦截器 配置完成,下面是 自定义拦截器配置
2. 自定义拦截器的配置使用
package com.lonely.intercepter; import java.util.List; 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.Node;
import org.w3c.dom.NodeList; public class MyIntercepter extends AbstractPhaseInterceptor<SoapMessage> { public MyIntercepter() {
// 在调用方法之前拦截
super(Phase.PRE_INVOKE); } @Override
public void handleMessage(SoapMessage message) throws Fault {
// 1. 获取拦截器,这里就假设只有一个拦截器
List<Header> headers = message.getHeaders();
if (headers == null || headers.size() == 0) {
throw new IllegalArgumentException("没有获取到表头信息");
}
Header header = headers.get(0); // 2. 获取元素
Element element = (Element) header.getObject();
NodeList uNodeList = element.getElementsByTagName("userName");
NodeList cNodeList = element.getElementsByTagName("classId"); Node uNode = uNodeList.item(0);
String userName = uNode.getTextContent();
Node cNode = cNodeList.item(0);
String classId = cNode.getTextContent(); // 3.验证
if (!userName.equals("aa") || !classId.equals("1")) {
throw new IllegalArgumentException("用户名或密码错误");
} } }
2.2 在 发布类中 添加该 拦截器
// 在调用方法之前验证身份,创建一个身份验证的 in 拦截器
jaxWsServerFactoryBean.getInInterceptors().add(new MyIntercepter());
2.3 启动发布
package com.lonely.intercepter; 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; public class AuthIntercepter extends AbstractPhaseInterceptor<SoapMessage> { private String userName;
private String classId; public AuthIntercepter(String userName, String classId) {
// 发起调用前
super(Phase.PREPARE_SEND);
this.userName = userName;
this.classId = classId;
} /**
* 调用服务端方法前 将身份信息封装到soap中,服务端拦截器拦截获取信息验证
*/
@Override
public void handleMessage(SoapMessage message) throws Fault {
// 获取soap表头信息
List<Header> headers = message.getHeaders(); // 1.创建document和元素
Document document = DOMUtils.createDocument();
Element authEle = document.createElement("authInfo");
Element uEle = document.createElement("userName");
Element cEle = document.createElement("classId"); // 2.赋值
uEle.setTextContent(userName);
cEle.setTextContent(classId);
authEle.appendChild(uEle);
authEle.appendChild(cEle); // 3. 添加到headers中
headers.add(new Header(new QName("auth"), authEle)); } }
2.5 在客户端的 测试类中,添加 该 out 拦截器
// 添加 out 自定义身份验证拦截器
client.getOutInterceptors().add(new AuthIntercepter("aa", "1"));
2.6 调用测试,分别 正确和错误都演示一边
使用CXF开发WebService程序的总结(六):结合拦截器使用的更多相关文章
- 使用CXF开发WebService程序的总结(七):Spring+CXF+Mybatis+Mysql共同打造的服务端示例
通过该demo,可以 熟悉下 spring+cxf+maven+mybatis+mysql等常见后端技术整合 1. 在前面的 父工程 ws_parent 中 添加依赖 由于原来的项目是使用的cxf依赖 ...
- 使用CXF开发WebService程序的总结(五):基于Map数据类型处理的的客户端和服务端代码的编写
1. 首先我们按照List或数组等处理方式来处理Map,看看效果 1.1 在服务端的接口中添加以下方法 /** * 查询所有班级信息加上对应的学生列表 * * @return */ public Ma ...
- 使用CXF开发WebService程序的总结(三):创建webservice客户端
1.创建一个maven子工程 ws_client,继承父工程 1.1 修改父工程pom配置 <modules> <module>ws_server</module> ...
- 使用CXF开发WebService程序的总结(四):基于bean的客户端和服务端代码的编写
1. 在原服务端项目 ws_server中添加两个bean 1.1 添加两个类 User 和 Clazz package com.lonely.pojo; public class User { ...
- 【WebService】使用CXF开发WebService(四)
CXF简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix ...
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- 使用cxf开发webservice应用时抛出异常
在使用cxf开发webservice应用时,报出了类似下面的错误 JAXB: [javax.xml.bind.UnmarshalException: unexpected element (uri:& ...
- 使用cxf开发webservice接口
项目中经常用到开发webservice接口,及调用webService接口.这里讲解如何使用cxf开发webService接口. 一.webservice介绍及理解 webservice是一种跨平台, ...
- Spring boot+CXF开发WebService
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
随机推荐
- 191107Django的Cookie和Session
Cookie的使用 from django.shortcuts import render,redirect def login(request): print("1",reque ...
- Firefox63以后 禁止自动更新方式
参考:https://bbs.kafan.cn/thread-2135160-1-1.html 63版以后在prefs.js文件末尾加代码来禁止自动更新的方式失效 新方式: 使用DisableAppU ...
- weka数据导入
每一行代表一条数据,用逗号分开属性,最后一列为分类标签 将后缀名改为csv,用excel打开,为每一列加上属性名称,直接导入weka即可
- Windows监控——性能指标详解(转)
http://blog.csdn.net/yiqin3399/article/details/51730106
- Linux常用命令:修改文件权限chmod 754/744
常用命令:chmod 777 文件或目录 chmod 777 /etc/squid 运行命令后,squid文件夹(目录)的权限就被修改为777(可读可写可执行). Linux系统中,每个用户的角色 ...
- python 生成excel,并下载到本地
from django.shortcuts import reverse,redirect,render from operations import models import xlwt impor ...
- 微信小程序的配置详解
1.配置详解: 使用app.json文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. 1>pages 接受一个数组,每一项都是字符串,来指定小 ...
- java:Mybatis框架1(基本配置,log4j,Junit4(单元测试))
1.mybatis01: db.properties: driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test userna ...
- PHP 按照时区获取当前时间
/** * 时间格式化 * @param string $dateformat 时间格式 * @param int $timestamp 时间戳 * @param int $timeoffse ...
- P1596 【[USACO10OCT]湖计数Lake Counting】
可爱的题面君~~ 个人感觉这题还是很简单的,就是一个完全不加工的找联通块个数 个人解题思路是先读入,然后循环一遍,遇到水就dfs,并把这个w所在的联通块“删除”,并在答案上加一 最后输出答案 具体注释 ...