From 790c2ba4a0b46edf191e3bac84931f796bd42b8f Mon Sep 17 00:00:00 2001 From: zhangf <1603559716@qq.com> Date: 星期三, 24 七月 2024 09:02:49 +0800 Subject: [PATCH] 三方对接接口优化 --- ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatedClickAspect.java | 64 ++++++++++++++++++++++++++++++++ 1 files changed, 64 insertions(+), 0 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatedClickAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatedClickAspect.java new file mode 100644 index 0000000..7eb680f --- /dev/null +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatedClickAspect.java @@ -0,0 +1,64 @@ +package com.ruoyi.framework.aspectj; + + + +import com.ruoyi.common.annotation.RepeatedClick; +import com.ruoyi.common.constant.ResultConstants; +import com.ruoyi.common.core.domain.model.InstitutionUser; +import com.ruoyi.common.core.redis.RedisCache; +import com.ruoyi.common.exception.BusinessException; +import com.ruoyi.common.exception.ServiceException; +import com.ruoyi.framework.security.context.ThreeInContextHolder; +import com.ruoyi.framework.web.service.TokenService; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.util.concurrent.TimeUnit; + +@Component +@Aspect +public class RepeatedClickAspect { + + + @Autowired + private RedisCache redisCache; + + @Before("@annotation(com.ruoyi.common.annotation.RepeatedClick)") + @ResponseBody + public void beforeRepeatedClick(JoinPoint joinPoint){ + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest arg = requestAttributes.getRequest(); + MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); + RepeatedClick annotation = methodSignature.getMethod().getAnnotation(RepeatedClick.class); + if (annotation != null){ + int clickTime = annotation.clickTime(); + String errorMessage = annotation.errorMessage(); + ThreeInContextHolder threeInContextHolder = new ThreeInContextHolder(); + InstitutionUser institutionUser = threeInContextHolder.getContext(); + if (institutionUser != null && institutionUser.getId() != null) { + try { + Long uid = institutionUser.getId(); + String key = "institution_user_id:"+uid+"_"+ arg.getRequestURI() + "_" + arg.getMethod(); + if (redisCache.hasKey(key)){ + throw new ServiceException(errorMessage+" 频繁访问接口: "+ arg.getRequestURI()); + }else { + redisCache.setCacheObject(key, institutionUser, clickTime, TimeUnit.SECONDS); + } + } catch (NumberFormatException e) { + throw new BusinessException(this.getClass(),ResultConstants.PARAM_ERROR.getCode(),"数据参数异常"); + } + } + } + } +} -- Gitblit v1.9.2