¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.framework.aspectj; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import com.baomidou.lock.LockInfo; |
| | | import com.baomidou.lock.LockTemplate; |
| | | import com.ruoyi.common.annotation.RepeatSubmit; |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.properties.TokenProperties; |
| | | import com.ruoyi.common.utils.ServletUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.framework.config.properties.RepeatSubmitProperties; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.aspectj.lang.JoinPoint; |
| | | import org.aspectj.lang.Signature; |
| | | 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.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.lang.reflect.Method; |
| | | |
| | | /** |
| | | * 鲿¢éå¤æäº¤ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @RequiredArgsConstructor(onConstructor_ = @Autowired) |
| | | @Aspect |
| | | @Component |
| | | public class RepeatSubmitAspect { |
| | | |
| | | private final TokenProperties tokenProperties; |
| | | private final RepeatSubmitProperties repeatSubmitProperties; |
| | | private final LockTemplate lockTemplate; |
| | | |
| | | // é
ç½®ç»å
¥ç¹ |
| | | @Pointcut("@annotation(com.ruoyi.common.annotation.RepeatSubmit)") |
| | | public void repeatSubmitPointCut() { |
| | | } |
| | | |
| | | @Before("repeatSubmitPointCut()") |
| | | public void doBefore(JoinPoint point) throws Throwable { |
| | | RepeatSubmit repeatSubmit = getAnnotationRateLimiter(point); |
| | | // å¦ææ³¨è§£ä¸ä¸º0 åä½¿ç¨æ³¨è§£æ°å¼ |
| | | long intervalTime = repeatSubmitProperties.getIntervalTime(); |
| | | if (repeatSubmit.intervalTime() > 0) { |
| | | intervalTime = repeatSubmit.timeUnit().toMillis(repeatSubmit.intervalTime()); |
| | | } |
| | | if (intervalTime < 1000) { |
| | | throw new ServiceException("éå¤æäº¤é´éæ¶é´ä¸è½å°äº'1'ç§"); |
| | | } |
| | | HttpServletRequest request = ServletUtils.getRequest(); |
| | | String nowParams = StrUtil.join(",", point.getArgs()); |
| | | |
| | | // 请æ±å°åï¼ä½ä¸ºåæ¾cacheçkeyå¼ï¼ |
| | | String url = request.getRequestURI(); |
| | | |
| | | // å¯ä¸å¼ï¼æ²¡ææ¶æ¯å¤´å使ç¨è¯·æ±å°åï¼ |
| | | String submitKey = request.getHeader(tokenProperties.getHeader()); |
| | | if (StringUtils.isEmpty(submitKey)) { |
| | | submitKey = url; |
| | | } |
| | | submitKey = SecureUtil.md5(submitKey + ":" + nowParams); |
| | | // å¯ä¸æ è¯ï¼æå®key + æ¶æ¯å¤´ï¼ |
| | | String cacheRepeatKey = Constants.REPEAT_SUBMIT_KEY + submitKey; |
| | | LockInfo lock = lockTemplate.lock(cacheRepeatKey, intervalTime, intervalTime / 2); |
| | | if (lock == null) { |
| | | throw new ServiceException("ä¸å
许éå¤æäº¤ï¼è¯·ç¨ååè¯!"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦å卿³¨è§£ï¼å¦æåå¨å°±è·å |
| | | */ |
| | | private RepeatSubmit getAnnotationRateLimiter(JoinPoint joinPoint) { |
| | | Signature signature = joinPoint.getSignature(); |
| | | MethodSignature methodSignature = (MethodSignature) signature; |
| | | Method method = methodSignature.getMethod(); |
| | | |
| | | if (method != null) { |
| | | return method.getAnnotation(RepeatSubmit.class); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | } |