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 ...
随机推荐
- JS中变量名作为if条件的 true/flase
在Javascript中,可以直接将变量名放到if条件中, var a;//甚至不定义 if (a){ //... } 以下情况被认为是flase: 1.''空的字符串 2.数字0 3.对象null ...
- Spark standalone HA
配置Spark standalone HA 主机:node1,node2,node3 master: node1,node2 slave:node2,node3 修改配置文件: node1,node3 ...
- 通过js获得html标签的值
js获取html元素的值并赋值 1).input文本框 <input type="text" value="时间" placeholder="姓 ...
- Linux默认权限的计算公式(个人理解性的笔记~)
先记下Linux下的权限可以分为 常见的 r(Read,读取):对文件,读取文件内容的权限:目录来说,具有浏览目 录的权限.权限值=4 w(Write,写入):对文件而言,具有新增.修改文件内容的权限 ...
- MVC拦截器记录操作用户日志
主要是用于记录用户操作动态, public class OperationAttribute:ActionFilterAttribute { /// <summary> /// 方法名称 ...
- mysql 简单优化方法
优化步骤:1.查看SQL是否可以优化.2.查看索引是否可以优化.3.查看表结构是否可以优化. show table status from databases like 'tablename%'; / ...
- javascript学习面向对象(二)
主要内容: prototype扩展应用示例: 对比如下: 数组中forEach用法示例: 从上面示例可以看出,forEach只适合遍历一维数组: 应用prototype扩展实现全部元素遍历如下: 简单 ...
- C#高级--通过类来理解学习委托
namespace classanddelegate { class Program { static void Main(string[] args) { //这是类的实例化 Test test = ...
- (转)__cdecl __fastcall与 __stdcall
原帖 http://blog.sina.com.cn/s/blog_6b7c56870100l8rf.html __cdecl __fastcall与 __stdcall 调用约定: __c ...
- zabbix微信告警(虚拟机脚本测试成功,zabbix上收不到信息)
前言: 使用zabbix直接运行脚本又可以正常接收.但是登录zabbix web界面,测试! 动作显示已送达,但是微信并没有收到信息! 解决: 添加脚本参数,因为不添加脚本参数,调用不了你这个脚本 ...