当前位置: 首页 > news >正文

通过Redis实现防止接口重复提交功能

本功能是在切面执行链基础上实现的功能,如果不知道切面执行链的同学,请看一下我之前专门介绍切面执行链的文章。

在SpringBoot项目中实现切面执行链功能-CSDN博客

1.定义防重复提交handler

/*** 重复提交handler**/
@AspectHandlerOrder
public class ResubmitAspectHandler implements AspectHandler {private StringRedisTemplate stringRedisTemplate;public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate = stringRedisTemplate;}@Overridepublic boolean execute(ProceedingJoinPoint pjp) throws Exception {Method method = getMethod(pjp);if (!method.isAnnotationPresent(Resubmit.class)) {return true;}Resubmit annotation = method.getAnnotation(Resubmit.class);long ttl = annotation.ttl();String key = getKey();String value = "1";if (lock(key, value, ttl)) {return true;} throw new BaseRuntimeException(ExceptionEnums.ERROR_10012.getCode(), "操作频率过高,请稍后再试!");}@Overridepublic void afterCompletion(ProceedingJoinPoint pjp, Object response, Exception exception) {Method method = getMethod(pjp);if (method.isAnnotationPresent(Resubmit.class)) {unlock(getKey());}}/*** redis原子操作:如果key不存在就设置key:value** @param key* @param value* @return true:设置成功拿到锁,false:设置失败未拿到锁*/private boolean lock(final String key, final String value, final long ttl) {Boolean result = stringRedisTemplate.boundValueOps(key).setIfAbsent(value, Duration.ofSeconds(ttl));return result != null ? result : false;}/*** 解锁:删除key** @param key*/private void unlock(String key) {if (StringUtils.isNotBlank(key)) {stringRedisTemplate.delete(key);}}/*** 获取方法** @param pjp* @return*/private Method getMethod(ProceedingJoinPoint pjp) {MethodSignature signature = (MethodSignature) pjp.getSignature();Method method = signature.getMethod();return method;}/*** 获取key** @return*/private String getKey() {ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = requestAttributes.getRequest();String url = request.getRequestURI();String httpMethod = request.getMethod();HttpHeader httpHeader = WebContext.getHttpHeader();String deviceId = httpHeader.getDevice_id();String key = RedisConstants.REDIS_RESUBMIT_KEY + httpMethod + url + ":" + deviceId;return key;}
}

2.定义防重复提交注解

/*** 防止重复提交*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Resubmit {/*** 存活时间(秒),当意外情况(例如锁定之后重启服务)* 未能执行解锁功能,redis将在${ttl}秒之后自动删除锁标志* 默认 10秒* @return*/long ttl() default 10;}

3.在配置类中注入防重复提交切面类

@Bean
public List<AspectHandler> apiAspectHandlers() {ResubmitAspectHandler resubmitAspectHandler = new ResubmitAspectHandler();resubmitAspectHandler.setStringRedisTemplate(stringRedisTemplate);return Arrays.asList(resubmitAspectHandler);
}

4.controller中应用防重复提交注解

@PostMapping("/release")
@Resubmit
public ApiResponse<?> insert(@RequestBody @Valid InsertAppRequestDTO req) {// 处理业务逻辑
}

http://www.lryc.cn/news/359108.html

相关文章:

  • 如何构建最小堆?
  • 基于Netty实现安全认证的WebSocket(wss)客户端
  • 代码随想录算法训练营第四十四天 | 01背包问题 二维、 01背包问题 一维、416. 分割等和子集
  • redis常见使用场景
  • 模糊C均值(FCM)算法更新公式推导
  • 金融创新浪潮下的拆分盘投资探索
  • 一份不知道哪里来的第十五届国赛模拟题
  • 机器人动力学模型与MATLAB仿真
  • SAPUI5基础知识3 - 引导过程(Bootstrap)
  • ABAP 借助公司封装的钉钉URL,封装的RFC给钉钉发送消息
  • 登录校验及全局异常处理器
  • 计算机视觉与模式识别实验1-2 图像的形态学操作
  • 【前端每日基础】day31——uni-app
  • 云动态摘要 2024-05-31
  • Oracle数据块如何存储真实数据
  • 【WEB前端2024】开源智体世界:乔布斯3D纪念馆-第30课-门的移动动画
  • 智能化改造给企业带来的实际效果
  • 深度学习-语言模型
  • 微型导轨在自动化制造中有哪些优势?
  • 探索气象数据的多维度三维可视化:PM2.5、风速与高度分析
  • 【传知代码】双深度学习模型实现结直肠癌检测(论文复现)
  • 平衡二叉树的应用举例
  • 一键安装 HaloDB 之 Ansible for Halo
  • el-table的上下筛选功能
  • 【手撕面试题】Vue(高频知识点一)
  • LabVIEW车轮动平衡检测系统
  • 【Python爬虫--scrapy+selenium框架】超详细的Python爬虫scrapy+selenium框架学习笔记(保姆级别的,非常详细)
  • 【Linux】Linux环境基础开发工具_3
  • 数字水印 | 图像噪声攻击(高斯/椒盐/泊松/斑点)
  • LeetCode-47 全排列Ⅱ