防重提交注解支持配置间隔时间/提示消息

This commit is contained in:
RuoYi 2021-09-20 19:09:25 +08:00
parent ac94242875
commit 26f0737c60
3 changed files with 17 additions and 20 deletions

View File

@ -19,5 +19,13 @@ import java.lang.annotation.Target;
@Documented @Documented
public @interface RepeatSubmit public @interface RepeatSubmit
{ {
/**
* 间隔时间(ms)小于此时间视为重复提交
*/
public int interval() default 5000;
/**
* 提示消息
*/
public String message() default "不允许重复提交,请稍后再试";
} }

View File

@ -29,9 +29,9 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter
RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class); RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
if (annotation != null) if (annotation != null)
{ {
if (this.isRepeatSubmit(request)) if (this.isRepeatSubmit(request, annotation))
{ {
AjaxResult ajaxResult = AjaxResult.error("不允许重复提交,请稍后再试"); AjaxResult ajaxResult = AjaxResult.error(annotation.message());
ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult)); ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult));
return false; return false;
} }
@ -51,5 +51,5 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter
* @return * @return
* @throws Exception * @throws Exception
*/ */
public abstract boolean isRepeatSubmit(HttpServletRequest request); public abstract boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation);
} }

View File

@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.filter.RepeatedlyRequestWrapper; import com.ruoyi.common.filter.RepeatedlyRequestWrapper;
@ -35,21 +36,9 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
@Autowired @Autowired
private RedisCache redisCache; private RedisCache redisCache;
/**
* 间隔时间单位: 默认10秒
*
* 两次相同参数的请求如果间隔时间大于该参数系统不会认定为重复提交的数据
*/
private int intervalTime = 10;
public void setIntervalTime(int intervalTime)
{
this.intervalTime = intervalTime;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public boolean isRepeatSubmit(HttpServletRequest request) public boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation)
{ {
String nowParams = ""; String nowParams = "";
if (request instanceof RepeatedlyRequestWrapper) if (request instanceof RepeatedlyRequestWrapper)
@ -87,7 +76,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
if (sessionMap.containsKey(url)) if (sessionMap.containsKey(url))
{ {
Map<String, Object> preDataMap = (Map<String, Object>) sessionMap.get(url); Map<String, Object> preDataMap = (Map<String, Object>) sessionMap.get(url);
if (compareParams(nowDataMap, preDataMap) && compareTime(nowDataMap, preDataMap)) if (compareParams(nowDataMap, preDataMap) && compareTime(nowDataMap, preDataMap, annotation.interval()))
{ {
return true; return true;
} }
@ -95,7 +84,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
} }
Map<String, Object> cacheMap = new HashMap<String, Object>(); Map<String, Object> cacheMap = new HashMap<String, Object>();
cacheMap.put(url, nowDataMap); cacheMap.put(url, nowDataMap);
redisCache.setCacheObject(cacheRepeatKey, cacheMap, intervalTime, TimeUnit.SECONDS); redisCache.setCacheObject(cacheRepeatKey, cacheMap, annotation.interval(), TimeUnit.MILLISECONDS);
return false; return false;
} }
@ -112,11 +101,11 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
/** /**
* 判断两次间隔时间 * 判断两次间隔时间
*/ */
private boolean compareTime(Map<String, Object> nowMap, Map<String, Object> preMap) private boolean compareTime(Map<String, Object> nowMap, Map<String, Object> preMap, int interval)
{ {
long time1 = (Long) nowMap.get(REPEAT_TIME); long time1 = (Long) nowMap.get(REPEAT_TIME);
long time2 = (Long) preMap.get(REPEAT_TIME); long time2 = (Long) preMap.get(REPEAT_TIME);
if ((time1 - time2) < (this.intervalTime * 1000)) if ((time1 - time2) < interval)
{ {
return true; return true;
} }