自定义注解实现接口幂等性处理

新建注解

@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiIdempotent {
    String type() default "head";
}

新建注解切面

@Aspect
@Component
public class ApiIdempotentAspect {
    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private BaseConstant baseConstant;

    @Pointcut("@annotation(com.exing.annotation.ApiIdempotent)")
    public void ApiIdempotent() {
    }


    // 环绕通知验证参数
    @Around("ApiIdempotent()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        ApiIdempotent apiIdempotent = signature.getMethod().getDeclaredAnnotation(ApiIdempotent.class);
        if (apiIdempotent != null) {
            return apiIdempotent(proceedingJoinPoint, signature);
        }
        // 放行
        Object proceed = proceedingJoinPoint.proceed();
        return proceed;
    }

    // 验证Token
    public Object apiIdempotent(ProceedingJoinPoint proceedingJoinPoint, MethodSignature signature)
            throws Throwable {
        ApiIdempotent apiIdempotent = signature.getMethod().getDeclaredAnnotation(ApiIdempotent.class);
        if (apiIdempotent == null) {
            // 直接执行程序
            Object proceed = proceedingJoinPoint.proceed();
            return proceed;
        }
        // 代码步骤:
        // 1.获取令牌 存放在请求头中
        HttpServletRequest request = getRequest();
        String valueType = apiIdempotent.type();
        if (StringUtils.isEmpty(valueType)) {
            throw new BadRequestException("注解参数错误!");
        }
        String token = null;
        if (valueType.equals("head")) {
            token = request.getHeader("token");
        } else {
            token = request.getParameter("token");
        }
        if (StringUtils.isEmpty(token)) {
            throw new BadRequestException("缺少幂等性校验token!");
        }
        if (Objects.isNull(redisUtils.get(baseConstant.getIdempotentKey().concat(token)))) {
            throw new BadRequestException("请勿重复提交!");
        }
        Object proceed = proceedingJoinPoint.proceed();
        redisUtils.del(baseConstant.getIdempotentKey().concat(token));
        return proceed;
    }

    public HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        return request;
    }
}

获取幂等性token 放在接口请求的header里面 key为token

@ApiOperation(value = "幂等性验证token", response = String.class)
    @GetMapping("/getToken")
    @ApiLog("幂等性验证token")
    @AnonymousAccess
    public ResponseEntity getToken(HttpServletRequest request) {
        String token = UUID.randomUUID().toString();
        redisUtils.set(baseConstant.getIdempotentKey().concat(token), token, 30000);
        request.setAttribute("token", token);
        return new ResponseEntity(token, HttpStatus.OK);
    }

测试

 @ApiIdempotent
    public ResponseEntity createOrder(@Validated(SaveApplyCarReqDto.Create.class) @RequestBody SaveApplyCarReqDto reqDto) {
        orderService.createOrder(reqDto);
        return new ResponseEntity(HttpStatus.OK);
    }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳十三

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值