首先介绍下CXF的拦截器:
简单地说,CXF使用流水线型(或者说总线型)处理机制,它的核心是一个Bus。一个客户端的请求或者一个对客户端桩代码的调用被组织成为一个Message。同时,所有的CXF功能都组织成Interceptor挂接在Bus上,分阶段依次处理Message。Message本质上是一个Map数据结构,既包含系统公共的也包含Interceptor自定义的数据。

AbstractPhaseInterceptor<Message>这个抽象类拦截器类,自定义拦截器类可以继承它实现它其中一个抽象方法public void handleMessage(Message message) throws Fault

如下代码实现:

package com.jd.train.service.webservice.ipinterceptor;

import com.jd.train.service.util.CodesUtil;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: zhangzhaozhao
 * Date: 12-3-26
 * Time: 下午4:06
 * To change this template use File | Settings | File Templates.
 */
public class IpAddressInInterceptor extends AbstractPhaseInterceptor<Message> {
    //这个属性是注入进来的,你也可以从properties,xml文件中去读取,也可以从数据库中去获取;
    private List<String> ipList;

public void setIpList(List<String> ipList) {
        this.ipList = ipList;
    }

private final static Logger logger = LogManager.getLogger(IpAddressInInterceptor.class);
    public IpAddressInInterceptor() {
        super(Phase.RECEIVE);
    }

public void handleMessage(Message message) throws Fault {
        //指定CXF获取客户端的HttpServletRequest : http-request;
        HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
        String ipAddress="";
        boolean flag = false;
        if (null != request) {
            ipAddress = getUserIpAddr(request); // 取客户端IP地址
            logger.info("请求客户端的IP地址:" + ipAddress);
            for (String s : ipList) {
                if (s.equals(ipAddress)) {
                    flag = true;
                    break;
                }
            }
        }
        if(!flag) {
            throw new Fault(new IllegalAccessException("IP address " + ipAddress + " is stint"));
        }
    }

/**
     * 获取IP地址的方法
     * @param request
     * @return
     */
    private String getUserIpAddr(HttpServletRequest request) {
        //获取经过代理的客户端的IP地址; 排除了request.getRemoteAddr() 方法 在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了
        String ip = CodesUtil.getIpAddr(request);
        if (ip != null && ip.indexOf(",") > 0) {
            logger.info("取到客户多个ip1====================" + ip);
            String[] arr = ip.split(",");
            ip = arr[arr.length - 1].trim();//有多个ip时取最后一个ip
            logger.info("取到客户多个ip2====================" + ip);
        }
        return ip;
    }
}

看下Spring 配置文件的信息:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:http-conf="http://cxf.apache.org/transports/http/configuration" xmlns:cxf="http://cxf.apache.org/core"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
             http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
             http://cxf.apache.org/transports/http/configuration
             http://cxf.apache.org/schemas/configuration/http-conf.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"
       default-autowire="byName">

<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"/>

<jaxws:endpoint id="messageNotifyServices" address="/MessageNotifyServices" >
        <jaxws:implementor>
            <bean class="com.jd.train.service.webservice.impl.MessageNotifyServicesImpl"/>
        </jaxws:implementor>
        <jaxws:inInterceptors>
            <ref bean="ipInterceptor"/>
        </jaxws:inInterceptors>
    </jaxws:endpoint>
    <!-- 订单推送WebService接口-->
    <jaxws:endpoint id="orderStatusServices" address="/OrderStatusServices">
        <!--这是需要发布的实现类 -->
        <jaxws:implementor>
            <bean class="com.jd.train.service.webservice.impl.OrderStatusServicesImpl"/>
        </jaxws:implementor>
        <jaxws:inInterceptors>
            <ref bean="ipInterceptor"/>
        </jaxws:inInterceptors>
    </jaxws:endpoint>

<!--毫秒单位 name 为 webservice 的域名 或者地址-->
    <http-conf:conduit name="${train.api.domain.name}.*">
        <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/>
    </http-conf:conduit>

<bean id="logIn" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
    <bean id="logOut" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
     <!-- 自定义拦截器-->
    <bean id="ipInterceptor" class="com.jd.train.service.webservice.ipinterceptor.IpAddressInInterceptor"/>
    <!-- 合法的IP地址,如果第三方IP变动需要修改 -->
    <bean id="ipList" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <value>114.80.202.120</value>
            </list>
        </constructor-arg>
    </bean>

<!-- CXF 全局的拦截器-->
   <cxf:bus>
       <cxf:inInterceptors>
           <ref bean="logIn"/>
       </cxf:inInterceptors>
       <cxf:outInterceptors>
           <ref bean="logOut" />
       </cxf:outInterceptors>
   </cxf:bus>

</beans>

解释下里面东西:
<jaxws:endpoint id="messageNotifyServices" address="/MessageNotifyServices" >
<jaxws:implementor>
    <bean class="com.jd.train.service.webservice.impl.MessageNotifyServicesImpl"/>
</jaxws:implementor>
<jaxws:inInterceptors>
    <ref bean="ipInterceptor"/>
</jaxws:inInterceptors>
</jaxws:endpoint>

<!-- 订单推送WebService接口-->
<jaxws:endpoint id="orderStatusServices" address="/OrderStatusServices">
<!--这是需要发布的实现类 -->
<jaxws:implementor>
    <bean class="com.jd.train.service.webservice.impl.OrderStatusServicesImpl"/>
</jaxws:implementor>
<jaxws:inInterceptors>
    <ref bean="ipInterceptor"/>
</jaxws:inInterceptors>
</jaxws:endpoint>
这两个是我发布出去的WebService 接口(这里需要你自己去实现,我并没有把代码贴出来);
<jaxws:inInterceptors>
    <ref bean="ipInterceptor"/>
</jaxws:inInterceptors> 这个是自定义的拦截器类

说到这里需要注意下了:如果把自定义的拦截器引入到发布出去的接口当中,而不是引入到全局的<cxf:bus>中,这样只对你发布出去的接口起作用
如果配置到全局的<cxf:bus>中对你访问第三方的WebService接口和别人访问你发布出去的WebService接口,都起到拦截作用,我开发过程遇到此问题,我调用第三方的webService接口时候也被拦截了,其中在自定义拦截器的HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
request是null ,并且IP也被过过滤掉了,使其我不能访问第三方的接口了.

第二个问题:就是Spring 配置文件的头信息http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd搞进去!

第三个问题:就是在获取客户IP时候,request.getRemoteAddr()取得客户端的IP地址,但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了。
经过代理以后,由于在客户端和服务之间增加了中间层,因此服务器无法直接拿到客户端的IP,服务器端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息用以跟踪原有的客户端IP地址和原来客户端请求的服务器地址。
给出比较靠谱实现:
 public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串ip值,其中哪个才是真正的用户端的真实IP呢?
答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100,用户真实IP为: 192.168.1.110
如果朋友遇到以上问题,请多加注意.

CXF详细介绍的更多相关文章

  1. [No0000A7]批处理经常用到的变量及批处理>NUL详细介绍

    绝对路径是指调用绝对的程序位置的路径,例如: start C:\Windows\test.exe 相对路径是文件改变路径以后还会按照变量的路径所在位置去调用,例如: start %WINDIR%\te ...

  2. linux配置网卡IP地址命令详细介绍及一些常用网络配置命令

    linux配置网卡IP地址命令详细介绍及一些常用网络配置命令2010-- 个评论 收藏 我要投稿 Linux命令行下配置IP地址不像图形界面下那么方 便,完全需要我们手动配置,下面就给大家介绍几种配置 ...

  3. _MSC_VER详细介绍

    _MSC_VER详细介绍 转自:http://www.cnblogs.com/braver/articles/2064817.html _MSC_VER是微软的预编译控制. _MSC_VER可以分解为 ...

  4. php CGI、Fastcgi、PHP-FPM的详细介绍与之间的关系

    以下PHP CGI.Fastcgi.PHP-FPM的一些信息归纳和汇总----->详细介绍与之间的关系 一:CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的 web ...

  5. RabbitMQ消息队列(一): Detailed Introduction 详细介绍

     http://blog.csdn.net/anzhsoft/article/details/19563091 RabbitMQ消息队列(一): Detailed Introduction 详细介绍 ...

  6. doT.js详细介绍

    doT.js详细介绍   doT.js特点是快,小,无依赖其他插件. 官网:http://olado.github.iodoT.js详细使用介绍 使用方法:{{= }} for interpolati ...

  7. Linux截屏工具scrot用法详细介绍

    Scrot是Linux命令行中使用的截图工具,能够进行全屏.选取等操作,下面小编将针对Scrot截图工具的用法给大家做个详细介绍,通过操作实例来学习Scrot的使用.   在Linux中安装Scrot ...

  8. Oracle Merge into 详细介绍

    Oracle Merge into 详细介绍 /*Merge into 详细介绍MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查 ...

  9. cPage分页详细介绍

    asp.net中各种数据控件,datalist.gridview.Repeater等分页是最常用的功能,几乎任何一个B/S项目,无论是系统还是网站都会用到.分页时,读取整个数据,直接绑定到控件,都可以 ...

随机推荐

  1. sim卡中电话本(ADN)的简要格式

    ADN的格式 ADN存放于sim卡下面3f00/7f10/6f3a,记录文件格式,其最小记录格式为14,最长为255(?),记录个数最大为255(?) 其后数14个字节是必有的,其前12个字节是电话号 ...

  2. 《Linux 设备驱动程序》读后感。 并发,竞态,死锁。

    1. 概念 并发:在操作系统中,是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行. 来源: 1. Linux ...

  3. UberX及以上级别车奖励政策(优步北京第一组)

    优步北京第一组: 定义为2015年6月1日凌晨前(不含6月1日)激活的司机(以优步后台数据显示为准) 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机( ...

  4. lodash中_.set的用法

    _.set(object, path, value) # Ⓢ Ⓣ Ⓝ 设置对象的路径上的属性值.如果路径不存在,则创建它. 参数 1.object (Object): 待扩大的对象. 2.path ( ...

  5. hibernate中文查询时无查询结果

    原因很简单,问题在于我连接mysql用的url时,没有指定字符集,导致查询不到任何数据 问题出在 hibernate.xml配置文件中: 将 <property name="jdbcU ...

  6. 解决mac插入U盘不显示标识问题

    有时候,我们在使用U盘的时候,如果未能正常退出U盘,下次插入U盘可能会不显示U盘. 解决办法如下,打开Finder-->偏好设置,设置 成功解决问题.

  7. lucene定义自己的分词器将其分成单个字符

    问题描写叙述:将一句话拆分成单个字符.而且去掉空格. package com.mylucene; import java.io.IOException; import java.io.Reader; ...

  8. MFC数据类型(data types)

    为便于理解MFC库函数中的各种形参,现将MFC中常见的参数类型总结如下: 下面这些是和Win32程序(SDK程序)共同使用的数据类型: 数据类型 意义 BOOL Boolean值(布尔值,不是TRUE ...

  9. appium locator

    If you want to find out more about the UIAutomator library, then it might be helpful to check out ht ...

  10. You raise me up

    You raise me up, so I can stand on mountains;You raise me up, to walk on stormy seas;I am strong, wh ...