赞
踩
通过AOP+注解实现
-
-
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- /**
- * @Author wyy
- * @creare 2022-08-05-9:42
- * 防止重复点击注解
- */
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface LimitAccessTimes {
- /**
- * 指定时间内不可重复提交,单位:s
- *
- * @return
- */
- long timeout() default 5;
-
- }
AOP的切入
- package org.springblade.common.aspect;
-
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.toolkit.StringUtils;
- import lombok.extern.slf4j.Slf4j;
- import me.zhyd.oauth.utils.IpUtils;
- 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.springblade.common.redis.RedisKeyUtil;
- import org.springblade.core.log.exception.ServiceException;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Component;
-
- import java.lang.reflect.Method;
- import java.text.MessageFormat;
- import java.util.UUID;
- import java.util.concurrent.TimeUnit;
-
- /**
- * @Author wyy
- * @creare 2022-08-05-9:44
- */
-
- @Aspect
- @Component
- @Slf4j
- public class LimitAccessTimesAspect {
- @Autowired
- private RedisTemplate<String, String> redisTemplate;
-
-
- @Before("@annotation(LimitAccessTimes)")
- public void repeatSumbitIntercept(JoinPoint joinPoint){
-
- String localIp = IpUtils.getLocalIp();
-
-
- //获取当前切面所设置的方法的类名、方法名
- String className = joinPoint.getTarget().getClass().getName();
- MethodSignature signature = (MethodSignature) joinPoint.getSignature();
- Method method = signature.getMethod();
- String methodName = method.getName();
- //获取方法参数
- //Object[] args = joinPoint.getArgs();
- //log.info("方法参数:" + JSONObject.toJSONString(args));
- // 获取配置的过期时间
- LimitAccessTimes annotation = method.getAnnotation(LimitAccessTimes.class);
- long timeout = annotation.timeout();
-
- //String key = className + ":" + methodName + ":" + localIp;
- String key = RedisKeyUtil.limitAccessKey(methodName, localIp);
- log.info(" --- >> 防重提交:key -- {}", key);
- // 判断是否已经超过重复提交的限制时间
- //说明redis设置的禁止访问时间未过期,抛出异常,禁止访问
- if (redisTemplate.hasKey(key)) {
- String messge = MessageFormat.format("请勿在{0}s内重复提交", timeout);
- throw new ServiceException(messge);
- }
- redisTemplate.opsForValue().set(key, key, timeout, TimeUnit.SECONDS);
- log.info("缓存数据,key:" + key +",时间:"+timeout);
- }
-
-
-
- }
对于RedisTemplate的工具类
-
-
- import org.springblade.core.secure.utils.AuthUtil;
-
- /**
- * 获取redisKey的工具类
- * @date: 2022/5/2 21:32
- * @version: 1.0
- */
- public class RedisKeyUtil {
-
- /**
- * 防止重复点击
- * @param methodName 方法名
- * @param ip ip地址
- * @author: wyy
- * @date: 2022/8/5 13:58
- */
- public static String limitAccessKey(String methodName,String ip){
- return AuthUtil.getTenantId() + ":" + methodName + ":" + ip;
- }
- }
具体的使用:
- @PostMapping("/cancel/exchange")
- @LimitAccessTimes(timeout = 5)
- public R cancelExchange(@ApiParam(value = "订单id", required = true) @RequestBody GlMallOrderRefundVO mallOrderRefund) {
-
- return R.status(this.pointMallOrderService.cancelExchange(mallOrderRefund));
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。