package com.hx.repeat.check;
|
|
import com.hx.exception.TipsException;
|
import com.hx.redis.RedisUtil;
|
import com.hx.util.DateUtil;
|
import com.hx.util.IPUtils;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.util.Date;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* URL防重复提交
|
* @author CJH
|
*/
|
public class RequestRepeat {
|
|
/**请求重复校验
|
* @param request HttpServletRequest
|
* @param redisUtil redis
|
* @param millisecond 间隔时间,毫秒(默认500毫秒)
|
*/
|
public static void checkRequest(HttpServletRequest request,RedisUtil redisUtil,Long millisecond){
|
//没有设置,默认0.5秒
|
if(millisecond == null){
|
millisecond = 500L;
|
}
|
//获取请求的IP地址
|
String ip = IPUtils.getIpAddr(request);
|
//获取URL
|
String method = request.getServletPath();
|
if(!redisUtil.setIfAbsent(ip+"-"+method, DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"),millisecond, TimeUnit.MILLISECONDS)){
|
throw new TipsException("请勿频繁操作!");
|
}
|
}
|
|
}
|