mirror of https://gitee.com/xiaonuobase/snowy
【修复】修复 #I6YA2U 节流防抖注解失效的bug
parent
8df3411ef9
commit
3ff79b2c40
|
@ -58,19 +58,20 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
import vip.xiaonuo.auth.core.util.StpClientUtil;
|
import vip.xiaonuo.auth.core.util.StpClientUtil;
|
||||||
import vip.xiaonuo.common.annotation.CommonNoRepeat;
|
import vip.xiaonuo.common.annotation.CommonNoRepeat;
|
||||||
import vip.xiaonuo.common.annotation.CommonWrapper;
|
import vip.xiaonuo.common.annotation.CommonWrapper;
|
||||||
|
import vip.xiaonuo.common.cache.CommonCacheOperator;
|
||||||
import vip.xiaonuo.common.enums.CommonDeleteFlagEnum;
|
import vip.xiaonuo.common.enums.CommonDeleteFlagEnum;
|
||||||
import vip.xiaonuo.common.exception.CommonException;
|
import vip.xiaonuo.common.exception.CommonException;
|
||||||
import vip.xiaonuo.common.listener.CommonDataChangeEventCenter;
|
import vip.xiaonuo.common.listener.CommonDataChangeEventCenter;
|
||||||
import vip.xiaonuo.common.listener.CommonDataChangeListener;
|
import vip.xiaonuo.common.listener.CommonDataChangeListener;
|
||||||
import vip.xiaonuo.common.pojo.CommonResult;
|
import vip.xiaonuo.common.pojo.CommonResult;
|
||||||
import vip.xiaonuo.common.pojo.CommonWrapperInterface;
|
import vip.xiaonuo.common.pojo.CommonWrapperInterface;
|
||||||
|
import vip.xiaonuo.common.util.CommonTimeFormatUtil;
|
||||||
import vip.xiaonuo.core.handler.GlobalExceptionUtil;
|
import vip.xiaonuo.core.handler.GlobalExceptionUtil;
|
||||||
import vip.xiaonuo.sys.core.enums.SysBuildInEnum;
|
import vip.xiaonuo.sys.core.enums.SysBuildInEnum;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.servlet.http.HttpSession;
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
@ -87,6 +88,11 @@ import java.util.*;
|
||||||
@MapperScan(basePackages = {"vip.xiaonuo.**.mapper"})
|
@MapperScan(basePackages = {"vip.xiaonuo.**.mapper"})
|
||||||
public class GlobalConfigure implements WebMvcConfigurer {
|
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();
|
Method method = handlerMethod.getMethod();
|
||||||
CommonNoRepeat annotation = method.getAnnotation(CommonNoRepeat.class);
|
CommonNoRepeat annotation = method.getAnnotation(CommonNoRepeat.class);
|
||||||
if (ObjectUtil.isNotEmpty(annotation)) {
|
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.setCharacterEncoding(CharsetUtil.UTF_8);
|
||||||
response.setContentType(ContentType.JSON.toString());
|
response.setContentType(ContentType.JSON.toString());
|
||||||
response.getWriter().write(JSONUtil.toJsonStr(CommonResult.error("请求过于频繁,请稍后再试")));
|
response.getWriter().write(JSONUtil.toJsonStr(CommonResult.error("请求过于频繁,请" + repeatSubmitJsonObject.getStr("time") + "后再试")));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -323,25 +330,34 @@ public class GlobalConfigure implements WebMvcConfigurer {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isRepeatSubmit(HttpServletRequest request, CommonNoRepeat annotation) {
|
public JSONObject isRepeatSubmit(HttpServletRequest request, CommonNoRepeat annotation) {
|
||||||
JSONObject jsonObject = JSONUtil.createObj();
|
JSONObject jsonObject = JSONUtil.createObj();
|
||||||
jsonObject.set("repeatParam", JSONUtil.toJsonStr(request.getParameterMap()));
|
jsonObject.set("repeatParam", JSONUtil.toJsonStr(request.getParameterMap()));
|
||||||
jsonObject.set("repeatTime", DateUtil.current());
|
jsonObject.set("repeatTime", DateUtil.current());
|
||||||
String url = request.getRequestURI();
|
String url = request.getRequestURI();
|
||||||
HttpSession session = request.getSession();
|
// 获取该接口缓存的限流数据
|
||||||
Object sessionObj = session.getAttribute("repeatData");
|
Object cacheObj = commonCacheOperator.get(COMMON_REPEAT_SUBMIT_CACHE_KEY + url);
|
||||||
if (ObjectUtil.isNotEmpty(sessionObj)) {
|
if (ObjectUtil.isNotEmpty(cacheObj)) {
|
||||||
JSONObject sessionJsonObject = JSONUtil.parseObj(sessionObj);
|
JSONObject cacheJsonObject = JSONUtil.parseObj(cacheObj);
|
||||||
if(sessionJsonObject.containsKey(url)) {
|
if(cacheJsonObject.containsKey(url)) {
|
||||||
JSONObject existRepeatJsonObject = sessionJsonObject.getJSONObject(url);
|
JSONObject existRepeatJsonObject = cacheJsonObject.getJSONObject(url);
|
||||||
if (jsonObject.getStr("repeatParam").equals(existRepeatJsonObject.getStr("repeatParam")) &&
|
// 如果与上次参数一致,且时间间隔小于要求的限流时长,则判定为重复提交
|
||||||
jsonObject.getLong("repeatTime") - existRepeatJsonObject.getLong("repeatTime") < annotation.interval()) {
|
if (jsonObject.getStr("repeatParam").equals(existRepeatJsonObject.getStr("repeatParam"))) {
|
||||||
return true;
|
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));
|
// 缓存最新的该接口的限流数据,为防止缓存的数据过多,缓存时效为1小时
|
||||||
return false;
|
commonCacheOperator.put(COMMON_REPEAT_SUBMIT_CACHE_KEY + url, JSONUtil.createObj().set(url, jsonObject), 60 * 60);
|
||||||
|
return JSONUtil.createObj().set("repeat", false);
|
||||||
}
|
}
|
||||||
}).addPathPatterns("/**");
|
}).addPathPatterns("/**");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue