客户端拦截器:

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拦截器的更多相关文章

  1. 基于Spring MVC 实现拦截器

    Spring MVC 拦截器 一,具体内容: 在所有的开发之中拦截器属于一个重要的组件,可以说几乎所有的项目都会提供的概念应用,不管是Spring MVC,还是Struts 2.x都是提供有拦截器的, ...

  2. 基于Spring和Mybatis拦截器实现数据库操作读写分离

    首先需要配置好数据库的主从同步: 上一篇文章中有写到:https://www.cnblogs.com/xuyiqing/p/10647133.html 为什么要进行读写分离呢? 通常的Web应用大多数 ...

  3. Spring Boot 配置拦截器方式

    其实spring boot拦截器的配置方式和springMVC差不多,只有一些小的改变需要注意下就ok了.下面主要介绍两种常用的拦截器: 一.基于URL实现的拦截器: public class Log ...

  4. 5.webService拦截器

    CXF为什么要设计拦截器? 为了在webservice请求过程中,能动态操作请求和响应数据, CXF设计了拦截器. 拦截器分类 1.按所处的位置分:服务器端拦截器,客户端拦截器 2.按消息的方向分:入 ...

  5. Spring mvc登录拦截器

    自己实现的第一个Spring mvc登录拦截器 题目要求:拒绝未登录用户进入系统,只要发现用户未登录,则将用户请求转发到/login.do要求用户登录 实现步骤: 1.在spring的配置文件中添加登 ...

  6. 玩转spring MVC(七)----拦截器

    继续在前边的基础上来学习spring MVC中拦截器的使用,下面通过一个例子来实现(完整项目在这里下载:http://download.csdn.net/detail/u012116457/84334 ...

  7. Spring Boot配置拦截器及实现跨域访问

    拦截器功能强大,能够深入方法前后,常应用于日志记录.权限检查和性能检测等,几乎是项目中不可或缺的一部分,本文就来实现Spring Boot自定义拦截器的配置. 理论指导 问:Spring Boot怎么 ...

  8. (转)spring中的拦截器(HandlerInterceptor+MethodInterceptor)

    1.  过滤器跟拦截器的区别 在说拦截器之前,不得不说一下过滤器,有时候往往被这两个词搞的头大. 其实我们最先接触的就是过滤器,还记得web.xml中配置的<filter>吗~ 你应该知道 ...

  9. spring mvc +cookie+拦截器功能 实现系统自动登陆

    先看看我遇到的问题: @ResponseBody @RequestMapping("/logout") public Json logout(HttpSession session ...

随机推荐

  1. Chrome & Linux font

    1 $ sudo apt-get install texlive-full # 较大 2 $ mkdir -p ~/.fonts 3 下载这个win7字体包解压后放到~/.fonts下 4 $ sud ...

  2. 关于yii2框架活动记录activeRecord添加默认字段的问题

    平时使用sql的时候可以如下添加默认字段flag: "select a.*,0 as flag from user_info a", 对于yii2框架则需要这样: $query = ...

  3. js中的一些容易混淆的方法!

    数组的一些方法: 1.join()和split()方法  与之相反的是split()方法:用于把一个字符串分割成字符串数组.  注意返回的数组中不包括separator本身: 提示和注释注释:如果把空 ...

  4. XUtils——GET请求

    //HttpUtils实例化对象     HttpUtils http = new HttpUtils();       /*                *发送请求send(HttpMethod ...

  5. 3.jenkins 权限认证与密码设置

    1.前言 在用Jenkins过程中忘记管理员密码和开启权限认证后管理员帐号没有任何权限是经常遇到的情况,最近有好多群友被这个问题困扰.但Jenkins没有提供密码找回的功能,经过一翻探索找到了一种变相 ...

  6. SQLMAP 中$与#的区别

    在sql配置中比如in(#rewr#) 与in ($rewr$) 在Ibatis中我们使用SqlMap进行Sql查询时需要引用参数,在参数引用中遇到的符号#和$之间的区分为,#可以进行与编译,进行类型 ...

  7. Thinkphp 学习笔记

    前后台配置: 在根目录文件夹中创建一个Conf文件夹 Conf文件夹下建立一个config.php文件,里面存放公共配置信息,方便前后台调用. 简单定义404页面 伪静态去除.html Config中 ...

  8. IIS7.5 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面

    IIS7.5中将一网站应用程序池托管管道模式改为经典后,网站页面打不开,错误信息: 引用内容 HTTP 错误 404.2 - Not Found由于 Web 服务器上的“ISAPI 和 CGI 限制” ...

  9. HTTP协议-----小白

    HTTP是一个属于应用层的面向对象的协议. ***OSI的7层从上到下分别是 7 应用层 6 表示层 5 会话层 4 传输层 3 网络层 2 数据链路层 1 物理层 HTTP协议的主要特点可概括如下: ...

  10. JSON字符串解析

    有时保存在数据库的数据是一串json字符串,需要进行读取的时候就需要解析操作. 简单介绍两种: 1.net.sf.json.* 2.com.alibaba.fastjson.* 需要的包自行下载. 第 ...