|
|
@@ -3,40 +3,26 @@ package com.sckw.core.aspect;
|
|
|
import com.sckw.core.annotation.RepeatSubmit;
|
|
|
import com.sckw.core.exception.SystemException;
|
|
|
import com.sckw.core.model.constant.Global;
|
|
|
-import com.sckw.core.model.page.PageRes;
|
|
|
-import com.sckw.core.utils.EncryUtil;
|
|
|
-import com.sckw.core.utils.NumberUtils;
|
|
|
import com.sckw.core.utils.StringUtils;
|
|
|
+import com.sckw.core.web.context.LoginUserHolder;
|
|
|
import com.sckw.redis.utils.RedissonUtils;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
-import jakarta.servlet.http.HttpServletResponse;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
-import org.aspectj.lang.JoinPoint;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
-import org.aspectj.lang.annotation.Before;
|
|
|
-import org.aspectj.lang.annotation.Pointcut;
|
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.context.request.RequestAttributes;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
import java.lang.reflect.Method;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author czh
|
|
|
- * @desc TODO
|
|
|
+ * @desc 防重复提交
|
|
|
* @date 2023/9/1
|
|
|
*/
|
|
|
@Aspect
|
|
|
@@ -45,22 +31,23 @@ public class NoRepeatSubmitAspect {
|
|
|
|
|
|
@Around("@annotation(com.sckw.core.annotation.RepeatSubmit)")
|
|
|
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
|
|
|
- // 获取request
|
|
|
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
|
|
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
|
|
|
HttpServletRequest request = servletRequestAttributes.getRequest();
|
|
|
- String requestURI = request.getRequestURI();
|
|
|
- String token = request.getHeader(Global.ACCESS_TOKEN);
|
|
|
- Map<String, Object> tokenMap = EncryUtil.descryV2(Global.PRI_KEY, token);
|
|
|
- Long userId = NumberUtils.parseLong(tokenMap.get("userId"));
|
|
|
- String key = Global.getRepeatSubmitKey(userId, requestURI);
|
|
|
- String string = RedissonUtils.getString(key);
|
|
|
+ String url = request.getRequestURI();
|
|
|
+ Long userId = LoginUserHolder.getUserId();
|
|
|
+ if (Objects.isNull(userId)) {
|
|
|
+ return pjp.proceed();
|
|
|
+ }
|
|
|
+
|
|
|
+ String key = Global.getRepeatSubmitKey(userId, url);
|
|
|
+ String res = RedissonUtils.getString(key);
|
|
|
MethodSignature signature = (MethodSignature) pjp.getSignature();
|
|
|
Method method = signature.getMethod();
|
|
|
RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
|
|
|
long interval = annotation.interval();
|
|
|
- if (StringUtils.isNotBlank(string)) {
|
|
|
- if (System.currentTimeMillis() - Long.parseLong(string) < interval) {
|
|
|
+ if (StringUtils.isNotBlank(res)) {
|
|
|
+ if (System.currentTimeMillis() - Long.parseLong(res) < interval) {
|
|
|
throw new SystemException(annotation.message());
|
|
|
}
|
|
|
return pjp.proceed();
|