栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java如何避免恶意连续点击

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java如何避免恶意连续点击

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.CopyOnWriteArrayList;

import java.util.concurrent.Executors;

import java.util.concurrent.TimeUnit;

public class RequestLimitCache {

private static final ConcurrentHashMap> map = new ConcurrentHashMap>();

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 list = map.get(key);

if (list == null) {

map.put(key, new CopyOnWriteArrayList());

}

map.get(key).add(System.currentTimeMillis());

}

// 是否到达指定数量

public boolean isUpCount(String key) {

CopyOnWriteArrayList 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 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;

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> map = new ConcurrentHashMap>();

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 list = map.get(key);

if (list == null) {

map.put(key, new CopyOnWriteArrayList());

}

map.get(key).add(new Long(System.currentTimeMillis()));

}

// 是否到达指定数量

public boolean isUpCount(String key) {

CopyOnWriteArrayList 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 list = map.get(key);

for (Long date : list) {

if ((System.currentTimeMillis() - date) > Cache.EXPIRE_TIME) {

list.remove(date);

}

}

}

}

}

}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985194.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号