首先介绍下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. J2SE知识点摘记(十四)

    1.        字符流 Reader是定义java的流式字符输入模式的抽象类,该类所有方法在出错的情况下都将引发IOException异常. Int read(char buffer[])     ...

  2. iOS 视图跳转

    //跳转 - ( void)present:( id )sender { NSLog ( @"the button,is clicked …" ); // 创建准备跳转的 UIVi ...

  3. Netfilter-packet-flow.svg

    调试网络的方法:(Debugging the kernel using Ftrace)  $ watch -n1 -d sudo cat /proc/net/snmp$ watch -n1 -d su ...

  4. c++游戏编程书籍

    如果要自学游戏程序开发的话,可以看看下面的,呵呵. 游戏开发资料(PDF书都是中文版的,非英文,很多是本人自己扫描制作,从未网上发布过,所以独家啦):  1.Gamebryo 2.2游戏引擎(盛大.腾 ...

  5. 米兰站热卖:奢侈品电商困局已破?-搜狐IT

    米兰站热卖:奢侈品电商困局已破?-搜狐IT 米兰站热卖:奢侈品电商困局已破?

  6. 面向对象程序设计-C++_课时21引用

    数据类型 & 别名=对象名; #include <iostream> using namespace std; int * f(int * x) { (*x)++; return ...

  7. c#读取xml文件配置文件Winform及WebForm-Demo具体解释

    我这里用Winform和WebForm两种为例说明怎样操作xml文档来作为配置文件进行读取操作. 1.新建一个类,命名为"SystemConfig.cs".代码例如以下: < ...

  8. English - 定冠词和不定冠词(a an the) 的区别

    不定冠词表示泛指,定冠词表示特指. 不定冠词a (an)与数词one 同源,是"一个"的意思.a用于辅音音素前,一般读作[e],而an则用于元音音素前,一般读做[en]. 1) 表 ...

  9. oracle基础代码使用

    create or replace procedure pr_test1 is v_case ) :;--定义变量 begin -- /*判断语句 then dbms_output.put_line( ...

  10. vs vsvim viemu vax 备忘

    使用gt和gT往返标签 gd:到达光标所在处函数或者变量的定义处. *:读取光标处的字符串,并且移动光标到它再次出现的地方. #:和上面的类似,但是是往反方向寻找. /text:从当前光标处开始搜索字 ...