Java实现幂等注解

拦截器

@Aspect
@Component
public class NoRepeatAspect {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Pointcut("@annotation(com.sunline.project.aop.NoRepeat) || @within(com.sunline.project.aop.NoRepeat)")
    public void pointCut(){}


    @Around("pointCut()")
    private Object around(ProceedingJoinPoint point) {
        try {
            // 获取当前用户的token
            Subject currStaff = SecurityUtils.getSubject();
            UserVo user = (UserVo) currStaff.getPrincipal();
            
            String token = user.getToken();
            StaticLog.info("检测是否重复提交");
            StaticLog.info("point:{}", point);

            // 获取当前请求的方法名
            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method = signature.getMethod();
            String name = method.getName();
            StaticLog.info("token:{},======methodName:{}", token, name);
            if (redisTemplate.hasKey(token+name)) {
                StaticLog.error("监测到重复提交>>>>>>>>>>");
                return ResponseData.fail(ResponseCode.FAIL_CODE, "请勿重复提交");
            }
            // 获取注解
            NoRepeat annotation = method.getAnnotation(NoRepeat.class);
            Long timeout = annotation.timeOut();
            // 此处我用token和请求方法名为key存入redis中,有效期为timeout 时间, 也可以使用ip地址做为key
            redisTemplate.opsForValue().set(token+name, token+name, timeout, TimeUnit.SECONDS);
            return point.proceed();
        } catch (Throwable throwable) {
            StaticLog.error("=====>>>>>>操作失败:{}", throwable);
            return ResponseData.fail(ResponseCode.FAIL_CODE, "操作失败");
        }
    }
}

注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NoRepeat {
    // 默认失效时间
    long timeOut() default 10;
}

测试:@NoRepeat(timeOut = 5)
timeOut默认时间为10s

    @NoRepeat(timeOut = 5)
    @GetMapping(value = "/test")
    @ApiOperation(value = "测试幂等注解")
    @SysLogs("测试幂等注解")
    @ApiImplicitParam(paramType = "header", name = "Authorization", value = "身份认证Token")
    public ResponseData<T> testIdempotent() {
        try {
            System.err.println(new Date());
            return ResponseData.ok(ResponseCode.SUCCESS_CODE, "操作成功");
        } catch (Exception e) {
            StaticLog.error("操作失败:{}", e);
            return ResponseData.ok(ResponseCode.FAIL_CODE, "操作失败");
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值