当前位置:   article > 正文

Java 防止多次点击注解封装_limitaccesstimesaspect

limitaccesstimesaspect

通过AOP+注解实现

  1. import java.lang.annotation.ElementType;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.RetentionPolicy;
  4. import java.lang.annotation.Target;
  5. /**
  6. * @Author wyy
  7. * @creare 2022-08-05-9:42
  8. * 防止重复点击注解
  9. */
  10. @Target(ElementType.METHOD)
  11. @Retention(RetentionPolicy.RUNTIME)
  12. public @interface LimitAccessTimes {
  13. /**
  14. * 指定时间内不可重复提交,单位:s
  15. *
  16. * @return
  17. */
  18. long timeout() default 5;
  19. }

AOP的切入

  1. package org.springblade.common.aspect;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  4. import lombok.extern.slf4j.Slf4j;
  5. import me.zhyd.oauth.utils.IpUtils;
  6. import org.aspectj.lang.JoinPoint;
  7. import org.aspectj.lang.annotation.Aspect;
  8. import org.aspectj.lang.annotation.Before;
  9. import org.aspectj.lang.reflect.MethodSignature;
  10. import org.springblade.common.redis.RedisKeyUtil;
  11. import org.springblade.core.log.exception.ServiceException;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.redis.core.RedisTemplate;
  14. import org.springframework.stereotype.Component;
  15. import java.lang.reflect.Method;
  16. import java.text.MessageFormat;
  17. import java.util.UUID;
  18. import java.util.concurrent.TimeUnit;
  19. /**
  20. * @Author wyy
  21. * @creare 2022-08-05-9:44
  22. */
  23. @Aspect
  24. @Component
  25. @Slf4j
  26. public class LimitAccessTimesAspect {
  27. @Autowired
  28. private RedisTemplate<String, String> redisTemplate;
  29. @Before("@annotation(LimitAccessTimes)")
  30. public void repeatSumbitIntercept(JoinPoint joinPoint){
  31. String localIp = IpUtils.getLocalIp();
  32. //获取当前切面所设置的方法的类名、方法名
  33. String className = joinPoint.getTarget().getClass().getName();
  34. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  35. Method method = signature.getMethod();
  36. String methodName = method.getName();
  37. //获取方法参数
  38. //Object[] args = joinPoint.getArgs();
  39. //log.info("方法参数:" + JSONObject.toJSONString(args));
  40. // 获取配置的过期时间
  41. LimitAccessTimes annotation = method.getAnnotation(LimitAccessTimes.class);
  42. long timeout = annotation.timeout();
  43. //String key = className + ":" + methodName + ":" + localIp;
  44. String key = RedisKeyUtil.limitAccessKey(methodName, localIp);
  45. log.info(" --- >> 防重提交:key -- {}", key);
  46. // 判断是否已经超过重复提交的限制时间
  47. //说明redis设置的禁止访问时间未过期,抛出异常,禁止访问
  48. if (redisTemplate.hasKey(key)) {
  49. String messge = MessageFormat.format("请勿在{0}s内重复提交", timeout);
  50. throw new ServiceException(messge);
  51. }
  52. redisTemplate.opsForValue().set(key, key, timeout, TimeUnit.SECONDS);
  53. log.info("缓存数据,key:" + key +",时间:"+timeout);
  54. }
  55. }

对于RedisTemplate的工具类

  1. import org.springblade.core.secure.utils.AuthUtil;
  2. /**
  3. * 获取redisKey的工具类
  4. * @date: 2022/5/2 21:32
  5. * @version: 1.0
  6. */
  7. public class RedisKeyUtil {
  8. /**
  9. * 防止重复点击
  10. * @param methodName 方法名
  11. * @param ip ip地址
  12. * @author: wyy
  13. * @date: 2022/8/5 13:58
  14. */
  15. public static String limitAccessKey(String methodName,String ip){
  16. return AuthUtil.getTenantId() + ":" + methodName + ":" + ip;
  17. }
  18. }

具体的使用:

  1. @PostMapping("/cancel/exchange")
  2. @LimitAccessTimes(timeout = 5)
  3. public R cancelExchange(@ApiParam(value = "订单id", required = true) @RequestBody GlMallOrderRefundVO mallOrderRefund) {
  4. return R.status(this.pointMallOrderService.cancelExchange(mallOrderRefund));
  5. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/694070
推荐阅读
相关标签
  

闽ICP备14008679号

        
cppcmd=keepalive&