预防高频访问 (在x秒内访问达到一定的次数转跳到防刷页面)


背景:预防高频访问 (在10秒内访问达到一定的次数转跳到防刷页面),10秒内访问超过1w次存入内存。项目用的strus2+memcache,java代码和配置如下


package com.woyaou.base.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.woyaou.base.util.CommonCache;
import com.woyaou.base.util.ConfigReaderUtils;
import com.woyaou.base.util.DateUtil;
import com.woyaou.base.util.IpUtils;
import com.woyaou.base.util.LogUtils;

/**
 * 异常拦截器
 *@Description: 预防高频访问 (在10秒内访问达到一定的次数转跳到防刷页面) 
 *@author Wupeng 
 *@date 2013-09-11 
 *
 */
@SuppressWarnings("serial")
public class ChallengeCollapsarInterceptor extends AbstractInterceptor {
	//对于高频访问者的阻塞时间
	private static int BLOCK_CACHE_TIME = 60;
	//对于访问计数缓存时间
	private static int COUNT_CACHE_TIME = 10;
	//10秒内访问访问限制次数
	private static int BLACK_TIME_LIMIT = 1000000;
	
	@Autowired
	private CommonCache commonCache;
	
	public String intercept(ActionInvocation invocation) throws Exception {
		final HttpServletRequest request = ServletActionContext.getRequest();
		String clientIp =IpUtils.getIpAddr(request);
		String blackKey = "black-"+clientIp;
		//检查用户是否在阻塞队列中。
		String value = (String)commonCache.get(blackKey); 
		if(value != null){
			return "blackpage";
		}

		String requestURI = request.getRequestURI();
		String strTime = DateUtil.getTimeLong().substring(0,9); //10秒时间, 9位是10秒级。
		String countKey = "count-"+strTime+"-"+clientIp+"-"+requestURI;
		int iTimes = 0;
		//取出10内的访问次数。
		value = (String)commonCache.get(countKey);
		if(value != null){
			iTimes = Integer.valueOf(value)+1;
			if(iTimes > BLACK_TIME_LIMIT){ //10秒内访问次数大于限定次数
				commonCache.add(blackKey, BLOCK_CACHE_TIME, "1");//加入阻塞缓存
				LogUtils.info("blacked:"+blackKey);
			}
		}
		//加入计数队列
		commonCache.set(countKey, COUNT_CACHE_TIME, String.valueOf(iTimes));

		return invocation.invoke();
	}
	
	public void init(){
		LogUtils.info("ChallengeCollapsarInterceptor:init()");
		String limits = ConfigReaderUtils.getValue("black_refresh_limit");
		if(limits != null){
			BLACK_TIME_LIMIT = Integer.valueOf(limits).intValue();
		}
	}
	
	public void destroy(){
		LogUtils.info("ChallengeCollapsarInterceptor:destroy()");
	}
}

struts.xml配置





    
	
	
	
	
	
 	
 	
 	
 	
 	
 	
 	 	
 	
 	

struts-common.xml 




	
		
			
			
			
			
			
			
				
				
				
				 
				  
			
		
		
		 
		
		
			/404.html
			/otherError.html
			/index.jsp
			/refreshBlack.html
		
		
			/404.html
		
		
		
			
			
			    301
			    ${forwardDomain}/${fromStationPy}/
			
			
			
			    301
			    ${forwardDomain}/${fromStationPy}-${toStationPy}.html
								
		
	
	
		
			
			
			ChallengeCollapsarInterceptor" />
			
			
				
				
				
				
				
			
		
		
		  
		
		
			/404.html
			/other.html
			/index.jsp
			/refreshBlack.html
		
		
			/404.html
		
	
	



你可能感兴趣的:(预防高频访问 (在x秒内访问达到一定的次数转跳到防刷页面))