防止表单重复提交,重复请求。程序幂等性预防

防止表单重复提交,重复请求。程序幂等性预防

需要用到Redis

自定义注解:

	作用于方法上,打了此注解的方法会通过Spring AOP进行幂等性检查:
/**
 * 请求幂等性检查
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface CheckIdempotency {
    //模块描述
    String model() default "";
    //锁失效时间
    int timeOut() default 10;
    //异常信息
    String ex() default "程序正在执行,请勿重复请求";
    //是否手动删除锁
    boolean delLock() default true;
}

AOP切面类:

对CheckIdempotency 自定义注解进行AOP操作,通过Redis的setNX分布式锁的特性进行幂等性检查:
@Aspect
@Component
public class CheckIdempotencyAop {

    @Autowired
    RedisTemplate redisTemplate;


    @Pointcut("@annotation(com.qzsoft.common.annotation.CheckIdempotency)")
    private void method() {
    }

    @Around(value = "method()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        Signature signature = pjp.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        CheckIdempotency checkIdempotency = targetMethod.getAnnotation(CheckIdempotency.class);
        String model = checkIdempotency.model();
        int timeOut = checkIdempotency.timeOut();
        String ex = checkIdempotency.ex();
        Object[] values = pjp.getArgs();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < values.length; i++) {
            stringBuffer.append(values[i]);
        }
        boolean success = setNx(stringBuffer+model,"java",timeOut,TimeUnit.SECONDS);
        if(!success){
            throw BusinessException.buildBiz(ex);
        }
        pjp.proceed(values);
        if(checkIdempotency.delLock())
        redisTemplate.delete(stringBuffer+model);
        return true;
    }

    @AfterThrowing(value = "method()", throwing="throwinfo")
     public void afterThrowing(JoinPoint pjp, Throwable  throwinfo) {
        Signature signature = pjp.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        CheckIdempotency checkIdempotency = targetMethod.getAnnotation(CheckIdempotency.class);
        String model = checkIdempotency.model();
        String ex = checkIdempotency.ex();
        Object[] values = pjp.getArgs();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < values.length; i++) {
            stringBuffer.append(values[i]);
        }
        if(!ex.equals(throwinfo.getMessage()))
            //如果程序逻辑报错,则删除锁。 如果错误信息是自定义的幂等性提示信息,说明出现了幂等性问题(多并发,或多次请求)则不删除锁。
        redisTemplate.delete(stringBuffer+model);
     }

    private boolean setNx(String key, String value, long expires, TimeUnit timeUnit) {
        boolean flag = false;
        try {
            flag = (boolean) redisTemplate.execute((RedisCallback<Boolean>) connection -> connection.set(key.getBytes(), value.getBytes(), Expiration.from(expires, timeUnit), RedisStringCommands.SetOption.ifAbsent()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}

自定义注解调用实例:

在需要幂等性检查的方法上标注自定义注解:
    @CheckIdempotency(model = CREATESAMPMODEL,timeOut = 35,ex = EX,delLock = false)
    public boolean createSamples(String projId){
		........
	}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

易柏州Innovation

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

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

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

打赏作者

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

抵扣说明:

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

余额充值