首页>代码>spring + cxf 的webservice服务端和客户端功能>/cxf_pro/src/com/hgq/cxf/ip/interceptor/CxfIpInterceptor.java
package com.hgq.cxf.ip.interceptor; import java.util.List; import javax.servlet.http.HttpServletRequest; 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.Level; import org.apache.log4j.Logger; public class CxfIpInterceptor extends AbstractPhaseInterceptor<Message> { private static final Logger log = Logger.getLogger(CxfIpInterceptor.class); public CxfIpInterceptor() { super(Phase.RECEIVE); } // 允许访问的IP private List<String> allowIpList; // 拒绝访问的IP private List<String> deniedList; public List<String> getAllowIpList() { return allowIpList; } public List<String> getDeniedList() { return deniedList; } public void setAllowIpList(List<String> allowIpList) { this.allowIpList = allowIpList; } public void setDeniedList(List<String> deniedList) { this.deniedList = deniedList; } @Override public void handleMessage(Message msg) throws Fault { // 获取WS请求中的IP HttpServletRequest request = (HttpServletRequest) msg.get(AbstractHTTPDestination.HTTP_REQUEST); String ip = request.getRemoteAddr(); // 判断是否在拒绝列表中 if(null != deniedList && !deniedList.isEmpty()) { for(String deniedIp : deniedList) { if(ip.equals(deniedIp)) { if(log.isEnabledFor(Level.WARN)) { log.warn("IP:" + ip + "在拒绝访问列表中"); } throw new Fault(new IllegalAccessException("IP:[" + ip + "]拒绝访问,请联系管理员")); } } } // 判断是否在允许访问列表中 if(null != allowIpList && !allowIpList.isEmpty()) { for(String allowIp : allowIpList) { if(ip.equals(allowIp)) { if(log.isEnabledFor(Level.WARN)) { log.warn("IP:" + ip + "在允许列表中,允许访问"); } break; } else { if(log.isEnabledFor(Level.WARN)) { log.warn("IP:" + ip + "不在允许访问列表中"); } throw new Fault(new IllegalAccessException("IP:[" + ip + "]拒绝访问,请联系管理员")); } } } } }

15947813008 LV52020年12月28日
夏同学 LV12020年6月9日
far_away LV12020年5月5日
14327211789 LV12020年4月1日
15398544947 LV92020年3月27日
dengjunjun LV132019年12月11日
sssssssyy LV102019年8月14日
zjhhanghang LV92019年7月24日
人工智能4708 LV82019年5月7日
heart426 LV92019年2月28日

15947813008 LV52020年12月28日
cljelly2020年11月26日
暂无贡献等级
kiss_ss2020年8月19日
暂无贡献等级
wcy071213 LV262020年8月8日
夏同学 LV12020年6月9日
luolukka LV72020年5月31日
liuyilin9608 LV152020年5月17日
hehe264 LV192020年5月11日
far_away LV12020年5月5日
126415894154 LV22020年4月29日