防止恶意刷新接口, 控制在5秒内不能被刷新10次以上


import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 防止恶意刷新接口, 控制在5秒内不能被刷新10次以上
 * @author alex.liul
 * @create 2017.11.08
 * @version 1.0
 */
public class RequestLimitCache {
	private static final ConcurrentHashMap<String, CopyOnWriteArrayList<Long>> map = new ConcurrentHashMap<String, CopyOnWriteArrayList<Long>>();
	private static final long EXPIRE_TIME = 1000 * 5L;
	private static final int MAX_REFRESH_COUNT = 10;

	private static final RequestLimitCache cache = new RequestLimitCache();

	private RequestLimitCache() {
		Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(new ClearCacheRunnable(), 1000L, 500L, TimeUnit.MILLISECONDS);
	}

	public static RequestLimitCache getInstance() {
		return cache;
	}

	// 增加指定key的访问次数
	public void increment(String key) {
		CopyOnWriteArrayList<Long> list = map.get(key);
		if (list == null) {
			map.put(key, new CopyOnWriteArrayList<Long>());
		}
		map.get(key).add(System.currentTimeMillis());
	}

	// 是否到达指定数量
	public boolean isUpCount(String key) {
		CopyOnWriteArrayList<Long> list = map.get(key);
		if (list == null) {
			return false;
		}
		return list.size() > MAX_REFRESH_COUNT;
	}

	// 清理过期数据线程
	private static class ClearCacheRunnable implements Runnable {
		@Override
		public void run() {
			try {
				clear();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		private void clear() {
			for (String key : map.keySet()) {
				CopyOnWriteArrayList<Long> list = map.get(key);
				for (Long date : list) {
					if ((System.currentTimeMillis() - date) > RequestLimitCache.EXPIRE_TIME) {
						list.remove(date);
					}
				}
			}
		}
	}
}
import java.io.IOException;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 防止恶意刷新网站 控制网站的所有网页在5秒内不能被刷新10次以上
 * 
 * @author alex.liul
 * @create 2013.3.21
 * @version 1.0
 */
public class RefreshFilter implements Filter {

	public void init(FilterConfig config) throws ServletException {

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		String ip = req.getRemoteAddr(); //得到客户端IP地址 
		String requestURI = req.getRequestURI();//得到客户请求的uri
		String cache_key=ip+requestURI;
		Cache cache = Cache.getInstance();
		if (cache.isUpCount(cache_key)) {
			res.setContentType("text/html; charset=UTF-8");// 响应类型
			res.sendRedirect(req.getContextPath() + "/error.html");
		} else {
			cache.increment(cache_key);
			chain.doFilter(request, response);
		}
	}

	public void destroy() {

	}

	// 缓存
	private static class Cache {

		private static final ConcurrentHashMap<String, CopyOnWriteArrayList<Long>> map = new ConcurrentHashMap<String, CopyOnWriteArrayList<Long>>();
		private static final long EXPIRE_TIME = 1000 * 5L;
		private static final long CLEAR_TIME = 500L;
		private static final int MAX_REFRESH_COUNT = 10;

		private static final Cache cache = new Cache();

		private Cache() {
			new Thread(new ClearCacheRunnable()).start();
		}

		public static Cache getInstance() {
			return cache;
		}

		// 增长指定url的点击次数
		public void increment(String key) {
			CopyOnWriteArrayList<Long> list = map.get(key);
			if (list == null) {
				map.put(key, new CopyOnWriteArrayList<Long>());
			}
			map.get(key).add(new Long(System.currentTimeMillis()));
		}

		// 是否到达指定数量
		public boolean isUpCount(String key) {
			CopyOnWriteArrayList<Long> list = map.get(key);
			if (list == null) {
				return false;
			}
			return list.size() > MAX_REFRESH_COUNT;
		}

		// 清理过期数据线程
		private static class ClearCacheRunnable implements Runnable {
			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(Cache.CLEAR_TIME);
						clear();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}

			private void clear() {
				for (String key : map.keySet()) {
					CopyOnWriteArrayList<Long> list = map.get(key);
					for (Long date : list) {
						if ((System.currentTimeMillis() - date) > Cache.EXPIRE_TIME) {
							list.remove(date);
						}
					}
				}
			}
		}
	}

}

 

转载于:https://my.oschina.net/u/2353992/blog/1568113

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值