springboot 订单重复提交_springboot AOP实现防止接口重复提交

背景:之前自己写了一个简单的书写文章小项目,测试的时候发现多次点击添加文章按钮系统会出现重复的文章,所以就想着用aop实现防止接口重复提交功能。

个人设计的思路:

1. 自定义一个注解,添加了该注解的接口可以防止重复提交。

2. 定义切面类,对使用了自定义注解的接口做代理。

3. 使用登录的userId+文章标题作为key存入redis,设置有效期,当有请求调用接口时,到redis中查找相应的key,如果能找到,则说明重复提交,如果找不到,则执行文章插入操作。

具体代码如下:

一.导入aop依赖(本文测试使用的springboot)

org.springframework.boot

spring-boot-starter-aop

二.自定义注解

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface ArticleReSubmit {

}

三.定义切面类

切面类需要使用@Aspect和@Component这两个注解做标注。

8c5ab0df11e90662dd5f22b173c1d5f2.png代码图片

@Aspect@Component@Slf4jpublic class ArticleAspect {@Resource private RedisUtil redisUtil; @Value("${user.session.key}") private String userSessionKey; @Pointcut(value = "@annotation(com.mypage.annotation.ArticleReSubmit)") public void annotationPointCut() { } @Around("annotationPointCut()") public Object NoReSubmit(ProceedingJoinPoint joinPoint) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); //获取request HttpServletRequest request = attributes.getRequest(); HttpSession session = request.getSession(); //从session中获取登录的user对象,如果为null,则要求重新登录 Object sessionUser = session.getAttribute(userSessionKey); if (sessionUser == null) { return Response.FAIL("页面超时,请重新登录"); } User user = (User) sessionUser; Integer userId = user.getId(); //获取接口的请求参数,如果时Article类型,则保存为Article对象,使用Article对象里的title属性 Object[] args = joinPoint.getArgs(); Article article = null; for (Object object : args) { if (object instanceof Article) { article = (Article) object; } } if (args == null) { return Response.FAIL("请求参数错误"); } //组装redis key 从redis中获取对应的值 String key = userId + "_" + article.getTitle(); Object flag = redisUtil.getStr(key); //如果redis中不存在对应的值,则执行原有的代码逻辑(插入文章操作) if (flag == null) { //redis设置key,value值为1 redisUtil.setStr(key, "1"); //设置有效期为5分钟 redisUtil.strSetExpireSeconds(key, 5*60L); try { return joinPoint.proceed(); } catch (Throwable throwable) { redisUtil.delStr(key); return Response.FAIL("系统错误,请联系管理员!"); } } else { //如果redis中存在对应的值,则证明重复提交,返回对应的信息 log.info("{}:重复提交", key); return Response.FAIL("重复提交"); } }}

四. 在想要防止重复提交的接口上添加注解

@RequestMapping("/addArticle")@ResponseBody@ArticleReSubmitpublic Response addArticle(Article article, HttpServletRequest request) {User user = (User) request.getSession().getAttribute(userSessionKey); return articleService.addArticle(article, user);}

至此,我们的代码就完成了。

如果您觉得我哪写得不好,或者有什么意见或建议,还请指出,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值