7.添加基于Spring的WebService拦截器
客户端拦截器:
public class AccountInterceptor extends AbstractPhaseInterceptor<SoapMessage>{ private String name; private String password; public AccountInterceptor(String name,String password) { //Phase值决定了拦截器什么时候拦截到消息 //PRE_PROTOCOL准备请求时拦截 super(Phase.PRE_PROTOCOL); this.name = name; this.password = password; } //一旦被拦截,首先调用此方法 @SuppressWarnings("deprecation") @Override public void handleMessage(SoapMessage msg) throws Fault { List<Header> headers = msg.getHeaders(); //在客户端请求时,会将用户名密码带过去 //怎么带用户名和密码到服务器,将用户名和密码设置在请求头中 org.w3c.dom.Document document = DOMHelper.createDocument(); Element ele = document.createElement("account");//创建标签<account></account> Element eleName = document.createElement("name");//创建标签<name></name> eleName.setTextContent(name);//给<name>设值,值为客户端传进来的用户名 ele.appendChild(eleName); Element elePwd = document.createElement("password");//创建标签<password></password> elePwd.setTextContent(password);//给<password>设值,值为客户端传进来的密码 ele.appendChild(elePwd); //设置标签<account>的account headers.add(new Header(new QName("account"),ele)); //如果拦截了,打印以下信息! System.out.println("客户端拦截了"); } }
客户端client-beans.xml配置:
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- serviceClass:SEI --> <!-- address:server端webService的发布地址 --> <jaxws:client id="weatherClient" serviceClass="com.cxf.dao.WeatherDao" address="http://localhost:8080/cxf_spring_webService_server/weatherws"> <!-- 客户端配置出拦截器 --> <jaxws:outInterceptors> <!-- 客户端拦截器全类名 --> <bean class="com.webservice.server.AccountInterceptor"> <!-- 拦截器构造器参数 --> <constructor-arg name="name" value="xxx"/> <constructor-arg name="password" value="xxx"/> </bean> </jaxws:outInterceptors> </jaxws:client> </beans>
服务器拦截器:
//服务器拦截器 public class CheckAccountInterceptor extends AbstractPhaseInterceptor<SoapMessage>{ public CheckAccountInterceptor() { super(Phase.PRE_PROTOCOL); } @Override public void handleMessage(SoapMessage message) throws Fault { //获取客户端请求头 //account为客户端设置的qname Header header = message.getHeader(new QName("account")); if(header != null){ Element account = (Element) header.getObject(); //通过标签名获取值<name></name> String name = account.getElementsByTagName("name").item(0).getTextContent(); String password = account.getElementsByTagName("password").item(0).getTextContent(); if("webService".equals(name) && "123456".equals(password)){ System.out.println("验证通过......"); } } System.out.println("没有通过拦截器!"); throw new Fault(new RuntimeException("用户名或者密码错误!")); } }
服务器beans.xml配置
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- cxf的一些核心配置(必须引入) --> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- implementor:SEI实现类全类名 --> <!-- address:名字可以任意取; webService部署路径:主机名/工程名/address wsdl文档:通过主机名/工程名/address?wsdl 不再需要手动去发布webService! --> <jaxws:endpoint id="weatherWS" implementor="com.cxf.service.WeatherService" address="/weatherws"> <!-- 服务器配置入拦截器 --> <jaxws:inInterceptors> <!-- 服务器拦截器全类名 --> <bean class="com.webService.server.CheckAccountInterceptor"></bean> </jaxws:inInterceptors> </jaxws:endpoint> </beans>
总结:配置拦截器相当简单,所以不再提供详细例子,参考以上配置即可。
7.添加基于Spring的WebService拦截器的更多相关文章
- 基于Spring MVC 实现拦截器
Spring MVC 拦截器 一,具体内容: 在所有的开发之中拦截器属于一个重要的组件,可以说几乎所有的项目都会提供的概念应用,不管是Spring MVC,还是Struts 2.x都是提供有拦截器的, ...
- 基于Spring和Mybatis拦截器实现数据库操作读写分离
首先需要配置好数据库的主从同步: 上一篇文章中有写到:https://www.cnblogs.com/xuyiqing/p/10647133.html 为什么要进行读写分离呢? 通常的Web应用大多数 ...
- Spring Boot 配置拦截器方式
其实spring boot拦截器的配置方式和springMVC差不多,只有一些小的改变需要注意下就ok了.下面主要介绍两种常用的拦截器: 一.基于URL实现的拦截器: public class Log ...
- 5.webService拦截器
CXF为什么要设计拦截器? 为了在webservice请求过程中,能动态操作请求和响应数据, CXF设计了拦截器. 拦截器分类 1.按所处的位置分:服务器端拦截器,客户端拦截器 2.按消息的方向分:入 ...
- Spring mvc登录拦截器
自己实现的第一个Spring mvc登录拦截器 题目要求:拒绝未登录用户进入系统,只要发现用户未登录,则将用户请求转发到/login.do要求用户登录 实现步骤: 1.在spring的配置文件中添加登 ...
- 玩转spring MVC(七)----拦截器
继续在前边的基础上来学习spring MVC中拦截器的使用,下面通过一个例子来实现(完整项目在这里下载:http://download.csdn.net/detail/u012116457/84334 ...
- Spring Boot配置拦截器及实现跨域访问
拦截器功能强大,能够深入方法前后,常应用于日志记录.权限检查和性能检测等,几乎是项目中不可或缺的一部分,本文就来实现Spring Boot自定义拦截器的配置. 理论指导 问:Spring Boot怎么 ...
- (转)spring中的拦截器(HandlerInterceptor+MethodInterceptor)
1. 过滤器跟拦截器的区别 在说拦截器之前,不得不说一下过滤器,有时候往往被这两个词搞的头大. 其实我们最先接触的就是过滤器,还记得web.xml中配置的<filter>吗~ 你应该知道 ...
- spring mvc +cookie+拦截器功能 实现系统自动登陆
先看看我遇到的问题: @ResponseBody @RequestMapping("/logout") public Json logout(HttpSession session ...
随机推荐
- sugar 自动为DP 加cache (or打表)
// from http://www.csdn.net/article/2015-12-03/2826381 #include <iostream> #include <tuple& ...
- 【Debian】非法关机后无法联网 connect: network is unreachable
某一天,突然发现无法ssh登录虚拟机内的debian系统,一直认为是ssh的问题,然后无意间ping了ping百度,发现原来是debian系统没有联网....囧 首先,是虚拟机的网络设置检查. 打开V ...
- final
final的变量的值不能被改变.(包括形参) final的方法不能被重写. final的类不能被继承.
- tomcat使用线程池配置高并发连接
1:配置executor属性打开/conf/server.xml文件,在Connector之前配置一个线程池:[html] view plain copy<Executor name=" ...
- silverLight--绑定数据dataGrid
后台代码编写 ,为表格绑定数据: using System; using System.Collections.Generic; using System.Linq; using System.Net ...
- Linux下Hadoop2.6.0集群环境的搭建
本文旨在提供最基本的,可以用于在生产环境进行Hadoop.HDFS分布式环境的搭建,对自己是个总结和整理,也能方便新人学习使用. 基础环境 JDK的安装与配置 现在直接到Oracle官网(http:/ ...
- C#结构
namespace STRUCT_TEST{ class Program { struct A { public int X;//不能直接对其进行赋值 public int Y; public sta ...
- jxl 处理 Excel 样式
jxl 能够操作的 excel 样式: 1. Workbook的格式设置(代表一个excel文件) 2. Sheet的格式设置(代表一个表格) 3. Cell的格式设置(代表一个单元格) PS:此jx ...
- MongoDB GridFS 对图片进行增删改
using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; using MongoDB.Driver.GridFS ...
- Linux下VFP NEON浮点编译
http://blog.csdn.net/liujia2100/article/details/27236477 NEON:SIMD(Single Instruction Multiple Data ...