SpringBoot+Redis防止接口重复提交

前言

在项目的使用使用过程中,经常会出现某些操作在短时间内频繁提交。例如:用户鼠标点击过快而重复保存,从而创建了2笔一模一样的单据。针对类似情况,我们就可以全局地控制接口不允许重复提交。

实现思路

  • 创建拦截器 Interceptor,拦截所有API请求
  • 将用户唯一标识(token或者jsessionid)+接口地址进行拼接,作为后续步骤的 redis-key
  • 判断Redis是否存在该key值,存在说明重复提交,不存在就存入Redis,过期时间1秒

SpringBoot+Redis防止接口重复提交_第1张图片

代码示例

创建拦截器 RepeatSubmitInterceptor

@Component
public class RepeatSubmitInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //防重时间间隔(秒)
    private final int duration = 1;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getDispatcherType() == DispatcherType.ERROR){
            return true;
        }
        String token = request.getHeader("token");
        String lockKey = "RepeatSubmit:" + token + ":" + request.getServletPath();
        Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, "", duration, TimeUnit.SECONDS);
        if (!result){
            throw new Exception("请勿重复提交");
        }

        return true;
    }
}

注入拦截器

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(repeatSubmitInterceptor);
    }
}

测试

建个 controller 并创建个简单的测试接口,打开 postman 快速点击2次请求,结果如下:
SpringBoot+Redis防止接口重复提交_第2张图片

总结

这边只是提供一种简单的方案,还可以有其他扩展,例如:

  • 增加参数的校验,只做相同参数的重复判定,参数不同可以重复提交
  • 增加AOP自定义注解,只有注解标识的接口才会重复判定
  • 使用Session替代Redis进行存储和校验(不适用于tomcat集群)

你可能感兴趣的:(Spring,Boot,redis,springboot,重复提交,接口)