发布时间:2023-06-06 13:00
在项目的使用使用过程中,经常会出现某些操作在短时间内频繁提交。例如:用户鼠标点击过快而重复保存,从而创建了2笔一模一样的单据。针对类似情况,我们就可以全局地控制接口不允许重复提交。
创建拦截器 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次请求,结果如下:
这边只是提供一种简单的方案,还可以有其他扩展,例如: