【修复】修复 #I6YA2U 节流防抖注解失效的bug

pull/114/head
xuyuxiang 2023-04-25 15:21:43 +08:00
parent 8df3411ef9
commit 3ff79b2c40
1 changed files with 31 additions and 15 deletions

View File

@ -58,19 +58,20 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import vip.xiaonuo.auth.core.util.StpClientUtil;
import vip.xiaonuo.common.annotation.CommonNoRepeat;
import vip.xiaonuo.common.annotation.CommonWrapper;
import vip.xiaonuo.common.cache.CommonCacheOperator;
import vip.xiaonuo.common.enums.CommonDeleteFlagEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.listener.CommonDataChangeEventCenter;
import vip.xiaonuo.common.listener.CommonDataChangeListener;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.pojo.CommonWrapperInterface;
import vip.xiaonuo.common.util.CommonTimeFormatUtil;
import vip.xiaonuo.core.handler.GlobalExceptionUtil;
import vip.xiaonuo.sys.core.enums.SysBuildInEnum;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
@ -87,6 +88,11 @@ import java.util.*;
@MapperScan(basePackages = {"vip.xiaonuo.**.mapper"})
public class GlobalConfigure implements WebMvcConfigurer {
private static final String COMMON_REPEAT_SUBMIT_CACHE_KEY = "common-repeatSubmit:";
@Resource
private CommonCacheOperator commonCacheOperator;
/**
*
*/
@ -312,10 +318,11 @@ public class GlobalConfigure implements WebMvcConfigurer {
Method method = handlerMethod.getMethod();
CommonNoRepeat annotation = method.getAnnotation(CommonNoRepeat.class);
if (ObjectUtil.isNotEmpty(annotation)) {
if (this.isRepeatSubmit(request, annotation)) {
JSONObject repeatSubmitJsonObject = this.isRepeatSubmit(request, annotation);
if (repeatSubmitJsonObject.getBool("repeat")) {
response.setCharacterEncoding(CharsetUtil.UTF_8);
response.setContentType(ContentType.JSON.toString());
response.getWriter().write(JSONUtil.toJsonStr(CommonResult.error("请求过于频繁,请后再试")));
response.getWriter().write(JSONUtil.toJsonStr(CommonResult.error("请求过于频繁,请" + repeatSubmitJsonObject.getStr("time") + "后再试")));
return false;
}
}
@ -323,25 +330,34 @@ public class GlobalConfigure implements WebMvcConfigurer {
return true;
}
public boolean isRepeatSubmit(HttpServletRequest request, CommonNoRepeat annotation) {
public JSONObject isRepeatSubmit(HttpServletRequest request, CommonNoRepeat annotation) {
JSONObject jsonObject = JSONUtil.createObj();
jsonObject.set("repeatParam", JSONUtil.toJsonStr(request.getParameterMap()));
jsonObject.set("repeatTime", DateUtil.current());
String url = request.getRequestURI();
HttpSession session = request.getSession();
Object sessionObj = session.getAttribute("repeatData");
if (ObjectUtil.isNotEmpty(sessionObj)) {
JSONObject sessionJsonObject = JSONUtil.parseObj(sessionObj);
if(sessionJsonObject.containsKey(url)) {
JSONObject existRepeatJsonObject = sessionJsonObject.getJSONObject(url);
if (jsonObject.getStr("repeatParam").equals(existRepeatJsonObject.getStr("repeatParam")) &&
jsonObject.getLong("repeatTime") - existRepeatJsonObject.getLong("repeatTime") < annotation.interval()) {
return true;
// 获取该接口缓存的限流数据
Object cacheObj = commonCacheOperator.get(COMMON_REPEAT_SUBMIT_CACHE_KEY + url);
if (ObjectUtil.isNotEmpty(cacheObj)) {
JSONObject cacheJsonObject = JSONUtil.parseObj(cacheObj);
if(cacheJsonObject.containsKey(url)) {
JSONObject existRepeatJsonObject = cacheJsonObject.getJSONObject(url);
// 如果与上次参数一致,且时间间隔小于要求的限流时长,则判定为重复提交
if (jsonObject.getStr("repeatParam").equals(existRepeatJsonObject.getStr("repeatParam"))) {
long interval = jsonObject.getLong("repeatTime") - existRepeatJsonObject.getLong("repeatTime");
if(interval < annotation.interval()) {
long secondsParam = (annotation.interval() - interval) / 1000;
if(secondsParam == 0) {
return JSONUtil.createObj().set("repeat", false);
} else {
return JSONUtil.createObj().set("repeat", true).set("time", CommonTimeFormatUtil.formatSeconds(secondsParam));
}
}
}
}
}
session.setAttribute("repeatData", JSONUtil.createObj().set(url, jsonObject));
return false;
// 缓存最新的该接口的限流数据为防止缓存的数据过多缓存时效为1小时
commonCacheOperator.put(COMMON_REPEAT_SUBMIT_CACHE_KEY + url, JSONUtil.createObj().set(url, jsonObject), 60 * 60);
return JSONUtil.createObj().set("repeat", false);
}
}).addPathPatterns("/**");
}