CXF 的IP拦截

很久没有写技术文档了,今天 记录下Webserver的Ip限制吧

需求是:webserver接口能在内网访问,但是测试平台的webserver要可以在外网访问,这样就有了一点区别,

这个实现的比较简单配置文件就一个白名单

#可用的IP
whiteList=a;b;

表示一个内网网段都可以访问

用配置文件读取

package com.onepiece.cxf.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 配置文件工具
 *
 * @author JueYue
 * @date 2014年7月3日 下午5:06:35
 */
public class CxfInterceptorProperties {

	private static final Logger logger = LoggerFactory
			.getLogger(CxfInterceptorProperties.class);

	private static boolean isNotIntercept = false;

	private static List<String> whiteList;

	static {
		ResourceBundle bundle;
		try {
			bundle = ResourceBundle.getBundle("cxf-interceptor");
		} catch (Exception e) {
			logger.info("cxf-interceptor没有配置配置文件,使用缺省配置");
			bundle = ResourceBundle
					.getBundle("com/onepiece/cxf/util/cxf-interceptor");
		}
		whiteList = Arrays.asList(bundle.getString("whiteList").split(";"));
		try {
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface
                    .getNetworkInterfaces();
            String ip;
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface nif = netInterfaces.nextElement();
                Enumeration<InetAddress> iparray = nif.getInetAddresses();
                while (iparray.hasMoreElements()) {
                	ip = iparray.nextElement().getHostAddress();
                	if (ip.contains("192.168.0")) {
                		logger.info("发现测试IP地址,取消IP限制");
        				isNotIntercept = true;
        				break;
        			}
                	logger.debug("本机IP为:{}",ip);
                }
            }
        } catch (Exception e) {
        	logger.info("获取IP失败,默认启用拦截");
			e.printStackTrace();
        }
	}

	public static List<String> getWhiteList() {
		return whiteList;
	}

	public static boolean isNotIntercept() {
		return isNotIntercept;
	}

}

自己设置一个默认配置,但是测试平台的话就没有IP限制了,这里获取IP地址复杂点,因为可能存在多个IP所以都获取发现一下自己想要的IP地址

然后是IP拦截

package com.onepiece.cxf.address;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.onepiece.cxf.util.CxfInterceptorProperties;

/**
 * 拦截配置文件的配置
 *
 * @author JueYue
 * @date 2014年7月3日 下午5:05:46
 */
public class IpAddressInInterceptor extends AbstractPhaseInterceptor<Message> {

	private static final Logger logger = LoggerFactory
			.getLogger(IpAddressInInterceptor.class);

	public IpAddressInInterceptor() {
		super(Phase.RECEIVE);
	}

	public void handleMessage(Message message) throws Fault {
		HttpServletRequest request = (HttpServletRequest) message
				.get(AbstractHTTPDestination.HTTP_REQUEST);
		List<String> allowedList = CxfInterceptorProperties.getWhiteList();
		if (logger.isDebugEnabled() && request != null) {
			logger.debug("getRemoteAddr 获取的IP:{},X-Client-Address 获取的IP:{}",
					request.getRemoteAddr(),request.getHeader("X-Client-Address"));
		}
		if (CxfInterceptorProperties.isNotIntercept() || request == null) {
			return;
		}
		String ipAddress = getIP(request);
		// 处理白名单
		boolean contains = false;
		for (String allowIpAddress : allowedList) {
			if (ipAddress.startsWith(allowIpAddress)) {
				contains = true;
				break;
			}
		}
		if (!contains) {
			logger.warn("***********************发现一个外来IP,IP地址:" + ipAddress
					+ "**************");
			throw new Fault(new IllegalAccessException("IP address "
					+ ipAddress + " is not allowed"));
		}
	}

	private String getIP(HttpServletRequest request) {
		String IP = request.getHeader("X-Client-Address"); // 取客户端IP地址
		if (StringUtils.isEmpty(IP)) {
			IP = request.getRemoteAddr();
		}
		return IP;
	}

}

这里获取IP首先是X-Clint-Address 因为我们是用的PortTunnel代理的,这个设置下就可以在X-cliet-Address获取真实IP

这样我们就做到了IP限制.后面就是配置下

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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-servlet.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

	<!-- IP地址输入拦截器 -->
	<bean id="ipAddressInInterceptor"
   		class="com.onepiece.cxf.address.IpAddressInInterceptor" />
   	<!-- 全局Bus(输入拦截器) -->
   	<cxf:bus>
   		<cxf:inInterceptors>
   			<ref bean="ipAddressInInterceptor"/>
   		</cxf:inInterceptors>
   	</cxf:bus>

</beans>

使用的是全局设置,所以不用每个都配置,这里需要注意一点default-lazy-init="true" 如果spring加上这个就会失效,

这里基本就完成了IP限制了

CXF 的IP拦截

时间: 2024-08-03 05:10:23

CXF 的IP拦截的相关文章

WebServices中使用cxf开发日志拦截器以及自定义拦截器

首先下载一个cxf实例,里面包含cxf的jar包.我下的是apache-cxf-2.5.9 1.为什么要设置拦截器? 为了在webservice请求过程中,能动态操作请求和响应数据, CXF设计了拦截器. 2.拦截器分类 1. 按所处的位置分:服务器端拦截器,客户端拦截器 2. 按消息的方向分:入拦截器,出拦截器 3. 按定义者分:系统拦截器,自定义拦截器 3.拦截器API Interceptor(拦截器接口) AbstractPhaseInterceptor(自定义拦截器从此继承) Loggi

WebService框架CXF实战一拦截器Interceptor(四)

拦截器(Interceptor)是CXF功能最主要的扩展点,可以在不对核心模块进行修改的情况下,动态添加很多功能.拦截器和JAX-WS Handler.Filter的功能类似,当服务被调用时,就会创建一个拦截器链(Interceptor Chain),拦截器链在服务输入(IN)或输出(OUT)阶段实现附加功能. 拦截器可以在客户端,也可以在服务端添加.当客户端发起一个WebService请求时,在客户端会创建输出拦截器链,服务端接收到客户端的后,会创建输入拦截器链.当服务端返回响应消息时,响应消

CXF对Interceptor拦截器的支持

前面在Axis中介绍过Axis的Handler,这里CXF的Interceptor就和Handler的功能类似.在每个请求响应之前或响应之后,做一些事情.这里的Interceptor就和Filter.Struts的Interceptor很类似,提供它的主要作用就是为了很好的降低代码的耦合性,提供代码的内聚性.下面我们就看看CXF的Interceptor是怎么样工作的. 1. 我们就用上面的HelloWorldService,客户端的调用代码重新写一份,代码如下: package com.hoo.

Apache CXF自定义拦截器

为什么设计拦截器?1.为了在webservice请求过程中,能动态操作请求和响应数据,CXF设计了拦截器 拦截器分类: 1.按所处的位置分:服务器端拦截器,客户端拦截器. 2.按消息的方向分:入拦截器,出拦截器. 3.按定义者分:系统拦截器,自定义拦截器. 客户端添加日志拦截器 package com.client.interceptor; import java.util.List; import javax.xml.namespace.QName; import org.apache.cxf

构建基于CXF的WebService服务(3)-- 利用拦截器实现权限验证

CXF中的拦截器分为in拦截器和out拦截器,又有客户端拦截器和服务端拦截器. 拦截器使用流程:客户端(out)-> 服务端(in)->处理业务->服务端(out)->客户端(in),并不是每一步都需要拦截器.在这里我们用到的是客户端Out拦截器和服务端in拦截器.服务端in拦截器检查用户级权限,客户端out浏览器发送用户信息给服务端. 1.创建服务端验证 JaxWsServerFactoryBean或Endpoint都可以通过getInInterceptors方法,向WebSer

CXF拦截器(Interceptor)LoggingInInterceptor

Interceptor是CXF架构中一个重要的功能.你可以在不对核心模块进行修改的情况下,动态添加很多功能(你可以想象Struts2拦截器的优点).这对于CXF这个以处理消息为中心的服务框架来说是非常有用的,CXF通过在Interceptor中对消息进行特殊处理,实现了很多重要功能模块,例如:日志记录,Soap消息处理,消息的压缩处理. CXF已经实现了很多种拦截器,很多已经在发布.访问Web 服务时已经默认添加到拦截器链.一般情况下, 我们自己的拦截器只要继承AbstractPhaseInte

cxf添加拦截器应用

项目中有时候也会做一些类似于权限验证的东西,拦截器也是一种实现方式.拦截器主要作用是做一些权限过滤,编码处理等. webService接口也可以上拦截器,我们也可以给webservice请求加权限判断功能: webservice分服务端和客户端,服务端和客户端都是可以加拦截器的,无论是服务端还是客户端,都分进,出(In,Out)拦截器: 可以使用cxf内置拦截器,也可以自定义拦截器,无论是自定义的拦截器,还是CXF自带的拦截器,都必须实现Interceptor接口. 下面分别从这两个方面来讲解:

使用CXF为webservice添加拦截器

拦截器分为Service端和Client端 拦截器是在发送soap消息包的某一个时机拦截soap消息包,对soap消息包的数据进行分析或处理.分为CXF自带的拦截器和自定义的拦截器 1.Service端: 通过将org.apache.cxf.jaxws.EndpointImpl类的方法即可实现   发布Web Service 方法可以通过Endpoint.publish()方法,此方法返为EndpointImpl,所以为Service端添加拦截器很简单. 部分代码: UserService us

cxf整合spring发布rest服务

1.创建maven web项目并添加依赖 pom.xml 1 <properties> 2 <webVersion>3.0</webVersion> 3 <cxf.version>3.2.5</cxf.version> 4 <spring.version>4.3.18.RELEASE</spring.version> 5 <jettison.version>1.4.0</jettison.version&