非常久没有写技术文档了,今天 记录下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"); // 取clientIP地址
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拦截的更多相关文章

  1. CXF实战之拦截器Interceptor(四)

    拦截器(Interceptor)是CXF功能最基本的扩展点,能够在不正确核心模块进行改动的情况下.动态加入非常多功能.拦截器和JAX-WS Handler.Filter的功能相似,当服务被调用时.就会 ...

  2. WS之cxf的权限拦截器应用

    一.服务器端: 1.权限判断: package cn.tdtk.ws.interceptor; import java.util.List; import org.apache.cxf.binding ...

  3. CXF对Interceptor拦截器的支持

    前面在Axis中介绍过Axis的Handler,这里CXF的Interceptor就和Handler的功能类似.在每个请求响应之前或响应之后,做一些事情.这里的Interceptor就和Filter. ...

  4. 基于springmvc开发注解式ip拦截器

    一.注解类 @Documented @Target({ElementType.TYPE,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) ...

  5. cxf与struts2拦截器冲突的解决方案

    最近学习接口,学习了下cxf,用tomcat部署访问的时候,发现接口不能访问:百度了很多,最终找到比较好的解决方案: sturts2配置: <!-- 设置strus拦截器 --> < ...

  6. (六)CXF之自定义拦截器

    一.需求分析 客户端在调用服务端的方法时,需要进行用户名和密码验证.此时分为: 客户端请求的时候,要发送用户名密码到服务端 服务端检验用户名密码. 二.案例 前提:本章案例是基于前一章节的例子进一步讲 ...

  7. (五)CXF之添加拦截器

    一.需求分析 webService中的拦截器类似于servlet的Filter过滤器.一般用于调用服务前后先调用拦截器的方法. 二.案例 本章案例是基于上一章节的基础上添加拦截器的 2.1 服务端添加 ...

  8. CXF之六 自定义拦截器

    CXF已经内置了一些拦截器,这些拦截器大部分默认添加到拦截器链中,有些拦截器也可以手动添加,如手动添加CXF提供的日志拦截器.也可以自定义拦截器,CXF中实现自定义拦截器很简单,只要继承Abstrac ...

  9. Apache CXF自定义拦截器

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

随机推荐

  1. RoHS认证简介

    RoHS认证是<电气.电子设备中限制使用某些有害物质指令>(The restriction of the use of certain hazardous substances in el ...

  2. 迁移到MSYS2 与 Qt 工具链注意的几个事情(注意链接顺序,并且人造mingw工具链所没有的局部midl.exe命令)

    Microsoft Visual Studio 2015社区版提供了强大的开发体验,且 Qt 提供了预编译版本.然而,由于客户提出兼容Windows XP ~ Windows 8.1 这样宽泛的环境要 ...

  3. ASP.NET缓存中Cache过期的三种策略

    原文:ASP.NET缓存中Cache过期的三种策略 我们在页面上添加三个按钮并双击按钮创建事件处理方法,三个按钮使用不同的过期策略添加ASP.NET缓存. <asp:Button ID=&quo ...

  4. 不要在公共接口中传递STL容器

    最近的一个项目,是开发一个framework,提供给公司内部不同的产品线使用. 之间遇到的一个问题,就是STL容器的使用, 而结论是不要在公共接口中传递STL容器: 这里说的STL容器,但主要则是指容 ...

  5. [11-3] Gradient Boosting regression

    main idea:用adaboost类似的方法,选出g,然后选出步长 Gredient Boosting for regression: h控制方向,eta控制步长,需要对h的大小进行限制 对(x, ...

  6. java 为pdf添加水印图片

    首先需要引入两个Jar包分别是:iTextAsian.jar .itext-2.1.7.jar  可以去  http://download.csdn.net/detail/work201003/922 ...

  7. 泛型、注解、log4j

    泛型.注解.log4j 泛型:将运行阶段的类型错误提前到编译阶段. 声明泛型必须两端的一致,要么左面有,要么右边有,两边都有的两边必须一致. 泛型方法: static 之后 返回类型之前进行声明 泛型 ...

  8. magento中取不同store中的产品数据

    $products = Mage::getResourceModel('catalog/product_collection')                    ->setStoreId( ...

  9. Docker简单介绍

    Docker简单介绍 Docker是一个能够把开发的应用程序非常方便地部署到容器的开源引擎.由Docker公司团队编写,基于Apache 2.0开源授权协议发行.Docker的主要目的例如以下: 提供 ...

  10. Quartz.NET 2.0 作业调度框架使用

    Quartz.NET是一个开源的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不 ...