Java Spring 注解annotation+AOP实现防止重复提交请求

背景介绍

用户开发票功能,数据链路较长,并且是异步返回开票最终结果的;为了防止用户重复提交开票申请,需要做防止重复提交的业务逻辑处理。
我们的服务是分布式微服务架构,开票服务有多个节点。当时已有的实现是通过redis key 有效期来实现的。为了将做成一个公共的工具,我试图把重复提交判断的逻辑与业务代码解耦,希望达到的效果是:在需要验证重复提交的接口上添加自定义注解@NoRepeatSubmit(expire = 60, handkey = NoRepeatSubmitHandleFactory实现类.class),
注解使用者需要通过实现NoRepeatSubmitHandleFactory接口,并重写handleKey方法去处理自己特殊的重复提交判断。

关键类代码

1、注解 NoRepeatSubmit
/**
 * @Description 防止重复提交请求注解
 * @Date 2022/8/30 14:50
 * @Created by liuxianglin
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {
    //key的过期时间 单位s
    int expire();
    //处理key的方法
    Class<? extends NoRepeatSubmitHandleFactory> handleKey();
}
2、切面拦截器 NoRepeatSubmitAspect
/**
 * @Description 切面拦截器处理NoRepeatSubmit注解
 * @Date 2022/8/30 15:07
 * @Created by liuxianglin
 */
@Aspect
@Component
public class NoRepeatSubmitAspect {
    private static final Logger logger = LoggerFactory.getLogger(NoRepeatSubmitAspect.class);

    @Pointcut("@annotation(com.xxxx.config.repeatsubmit.NoRepeatSubmit)")
    public void point() {}

    @Around("point()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        //获取NoRepeatSubmit注解的key处理类
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        NoRepeatSubmit annotation = method.getAnnotation(NoRepeatSubmit.class);
        Class<? extends NoRepeatSubmitHandleFactory> aClass1 = annotation.handleKey();
        NoRepeatSubmitHandleFactory handleFactory = SpringUtil.getBean(aClass1);
        //调用处理key的方法
        List<String> keys;
        try {
            keys = handleFactory.handleKey(pjp);
        } catch (Exception e) {
            return Results.failure(null, e.getMessage());
        }
        //执行请求
        Results result = (Results) pjp.proceed();
        return result;
    }

}
3、处理重复提交具体业务逻辑的工厂类 NoRepeatSubmitHandleFactory
/**
 * @Description
 * @Date 2022/8/31 16:53
 * @Created by liuxianglin
 */
public interface  NoRepeatSubmitHandleFactory {
    List<String> handleKey(ProceedingJoinPoint pjp) throws Exception;
}
4、我的重复提交具体业务逻辑的实现类 MyNoRepeatSubmitHandleFactory
import org.springframework.data.redis.core.RedisTemplate;
/**
 * @Description
 * @Date 2022/8/31 16:55
 * @Created by liuxianglin
 */
@Component
public class MyNoRepeatSubmitHandle implements NoRepeatSubmitHandleFactory {
    @Autowired
    private RedisTemplate redisRepository;

    @Override
    public List<String> handleKey(ProceedingJoinPoint pjp) throws Exception {
    	//获取request
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //获取response
        HttpServletResponse response = requestAttributes.getResponse();
        //获取请求的body参数
        Object[] args = pjp.getArgs();
        MyCommand cmd = (MyCommand) args[0];
        String key = cmd.toString();
       //获取注解NoRepeatSubmit注解
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        NoRepeatSubmit annotation = method.getAnnotation(NoRepeatSubmit.class);
        Object value = redisRepository.get(key);
        if (value != null) {
            throw new Exception("请勿重复提交");
        }
        redisRepository.opsForValue().set(key, key, annotation.expire(), TimeUnit.SECONDS);
    }
}
5、使用注解 NoRepeatSubmit的示例
@NoRepeatSubmit(expire = 60, handleKey = MyNoRepeatSubmitHandleFactory.class)
@GetMapping("/testNoRepeatSubmit")
public Results testNoRepeatSubmit(MyCommand cmd) {
   	return null;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值