update 优化 RepeatSubmitAspect 逻辑避免并发请求问题
This commit is contained in:
parent
36b5051cbd
commit
11485c2538
@ -2,6 +2,7 @@ package com.ruoyi.framework.aspectj;
|
|||||||
|
|
||||||
import cn.hutool.core.lang.Dict;
|
import cn.hutool.core.lang.Dict;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.ruoyi.common.annotation.Log;
|
import com.ruoyi.common.annotation.Log;
|
||||||
import com.ruoyi.common.core.domain.event.OperLogEvent;
|
import com.ruoyi.common.core.domain.event.OperLogEvent;
|
||||||
@ -25,6 +26,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 操作日志记录处理
|
* 操作日志记录处理
|
||||||
@ -144,26 +146,23 @@ public class LogAspect {
|
|||||||
* 参数拼装
|
* 参数拼装
|
||||||
*/
|
*/
|
||||||
private String argsArrayToString(Object[] paramsArray, String[] excludeParamNames) {
|
private String argsArrayToString(Object[] paramsArray, String[] excludeParamNames) {
|
||||||
StringBuilder params = new StringBuilder();
|
StringJoiner params = new StringJoiner(" ");
|
||||||
if (paramsArray != null && paramsArray.length > 0) {
|
if (ArrayUtil.isEmpty(paramsArray)) {
|
||||||
for (Object o : paramsArray) {
|
return params.toString();
|
||||||
if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) {
|
}
|
||||||
try {
|
for (Object o : paramsArray) {
|
||||||
String str = JsonUtils.toJsonString(o);
|
if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) {
|
||||||
Dict dict = JsonUtils.parseMap(str);
|
String str = JsonUtils.toJsonString(o);
|
||||||
if (MapUtil.isNotEmpty(dict)) {
|
Dict dict = JsonUtils.parseMap(str);
|
||||||
MapUtil.removeAny(dict, EXCLUDE_PROPERTIES);
|
if (MapUtil.isNotEmpty(dict)) {
|
||||||
MapUtil.removeAny(dict, excludeParamNames);
|
MapUtil.removeAny(dict, EXCLUDE_PROPERTIES);
|
||||||
str = JsonUtils.toJsonString(dict);
|
MapUtil.removeAny(dict, excludeParamNames);
|
||||||
}
|
str = JsonUtils.toJsonString(dict);
|
||||||
params.append(str).append(" ");
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
params.add(str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return params.toString().trim();
|
return params.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -184,9 +183,8 @@ public class LogAspect {
|
|||||||
}
|
}
|
||||||
} else if (Map.class.isAssignableFrom(clazz)) {
|
} else if (Map.class.isAssignableFrom(clazz)) {
|
||||||
Map map = (Map) o;
|
Map map = (Map) o;
|
||||||
for (Object value : map.entrySet()) {
|
for (Object value : map.values()) {
|
||||||
Map.Entry entry = (Map.Entry) value;
|
return value instanceof MultipartFile;
|
||||||
return entry.getValue() instanceof MultipartFile;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
|
return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.ruoyi.framework.aspectj;
|
package com.ruoyi.framework.aspectj;
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
import com.ruoyi.common.annotation.RepeatSubmit;
|
import com.ruoyi.common.annotation.RepeatSubmit;
|
||||||
@ -12,14 +13,11 @@ import com.ruoyi.common.utils.MessageUtils;
|
|||||||
import com.ruoyi.common.utils.ServletUtils;
|
import com.ruoyi.common.utils.ServletUtils;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
import com.ruoyi.common.utils.redis.RedisUtils;
|
import com.ruoyi.common.utils.redis.RedisUtils;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.AfterReturning;
|
import org.aspectj.lang.annotation.AfterReturning;
|
||||||
import org.aspectj.lang.annotation.AfterThrowing;
|
import org.aspectj.lang.annotation.AfterThrowing;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Before;
|
import org.aspectj.lang.annotation.Before;
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@ -28,16 +26,14 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 防止重复提交(参考美团GTIS防重系统)
|
* 防止重复提交(参考美团GTIS防重系统)
|
||||||
*
|
*
|
||||||
* @author Lion Li
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
@Aspect
|
@Aspect
|
||||||
@Component
|
|
||||||
public class RepeatSubmitAspect {
|
public class RepeatSubmitAspect {
|
||||||
|
|
||||||
private static final ThreadLocal<String> KEY_CACHE = new ThreadLocal<>();
|
private static final ThreadLocal<String> KEY_CACHE = new ThreadLocal<>();
|
||||||
@ -45,10 +41,8 @@ public class RepeatSubmitAspect {
|
|||||||
@Before("@annotation(repeatSubmit)")
|
@Before("@annotation(repeatSubmit)")
|
||||||
public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable {
|
public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable {
|
||||||
// 如果注解不为0 则使用注解数值
|
// 如果注解不为0 则使用注解数值
|
||||||
long interval = 0;
|
long interval = repeatSubmit.timeUnit().toMillis(repeatSubmit.interval());
|
||||||
if (repeatSubmit.interval() > 0) {
|
|
||||||
interval = repeatSubmit.timeUnit().toMillis(repeatSubmit.interval());
|
|
||||||
}
|
|
||||||
if (interval < 1000) {
|
if (interval < 1000) {
|
||||||
throw new ServiceException("重复提交间隔时间不能小于'1'秒");
|
throw new ServiceException("重复提交间隔时间不能小于'1'秒");
|
||||||
}
|
}
|
||||||
@ -64,9 +58,7 @@ public class RepeatSubmitAspect {
|
|||||||
submitKey = SecureUtil.md5(submitKey + ":" + nowParams);
|
submitKey = SecureUtil.md5(submitKey + ":" + nowParams);
|
||||||
// 唯一标识(指定key + url + 消息头)
|
// 唯一标识(指定key + url + 消息头)
|
||||||
String cacheRepeatKey = CacheConstants.REPEAT_SUBMIT_KEY + url + submitKey;
|
String cacheRepeatKey = CacheConstants.REPEAT_SUBMIT_KEY + url + submitKey;
|
||||||
String key = RedisUtils.getCacheObject(cacheRepeatKey);
|
if (RedisUtils.setObjectIfAbsent(cacheRepeatKey, "", Duration.ofMillis(interval))) {
|
||||||
if (key == null) {
|
|
||||||
RedisUtils.setCacheObject(cacheRepeatKey, "", Duration.ofMillis(interval));
|
|
||||||
KEY_CACHE.set(cacheRepeatKey);
|
KEY_CACHE.set(cacheRepeatKey);
|
||||||
} else {
|
} else {
|
||||||
String message = repeatSubmit.message();
|
String message = repeatSubmit.message();
|
||||||
@ -114,19 +106,16 @@ public class RepeatSubmitAspect {
|
|||||||
* 参数拼装
|
* 参数拼装
|
||||||
*/
|
*/
|
||||||
private String argsArrayToString(Object[] paramsArray) {
|
private String argsArrayToString(Object[] paramsArray) {
|
||||||
StringBuilder params = new StringBuilder();
|
StringJoiner params = new StringJoiner(" ");
|
||||||
if (paramsArray != null && paramsArray.length > 0) {
|
if (ArrayUtil.isEmpty(paramsArray)) {
|
||||||
for (Object o : paramsArray) {
|
return params.toString();
|
||||||
if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) {
|
}
|
||||||
try {
|
for (Object o : paramsArray) {
|
||||||
params.append(JsonUtils.toJsonString(o)).append(" ");
|
if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) {
|
||||||
} catch (Exception e) {
|
params.add(JsonUtils.toJsonString(o));
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return params.toString().trim();
|
return params.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -147,9 +136,8 @@ public class RepeatSubmitAspect {
|
|||||||
}
|
}
|
||||||
} else if (Map.class.isAssignableFrom(clazz)) {
|
} else if (Map.class.isAssignableFrom(clazz)) {
|
||||||
Map map = (Map) o;
|
Map map = (Map) o;
|
||||||
for (Object value : map.entrySet()) {
|
for (Object value : map.values()) {
|
||||||
Map.Entry entry = (Map.Entry) value;
|
return value instanceof MultipartFile;
|
||||||
return entry.getValue() instanceof MultipartFile;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
|
return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
|
||||||
|
Loading…
x
Reference in New Issue
Block a user