springboot实现防重复提交和防重复点击的示例

背景

同一条数据被用户点击了多次,导致数据冗余,需要防止弱网络等环境下的重复点击

目标

通过在指定的接口处添加注解,实现根据指定的接口参数来防重复点击

说明

这里的重复点击是指在指定的时间段内多次点击按钮

技术方案

springboot + redis锁 + 注解

使用 feign client 进行请求测试

最终的使用实例

1、根据接口收到 PathVariable 参数判断唯一

/**
  * 根据请求参数里的 PathVariable 里获取的变量进行接口级别防重复点击
  *
  * @param testId 测试id
  * @param requestVo 请求参数
  * @return
  * @author daleyzou
  */
 @PostMapping("/test/{testId}")
 @NoRepeatSubmit(location = "thisIsTestLocation", seconds = 6)
 public RsVo thisIsTestLocation(@PathVariable Integer testId, @RequestBody RequestVo requestVo) throws Throwable {
  // 睡眠 5 秒,模拟业务逻辑
  Thread.sleep(5);
  return RsVo.success("test is return success");
 }

2、根据接口收到的 RequestBody 中指定变量名的值判断唯一

/**
  * 根据请求参数里的 RequestBody 里获取指定名称的变量param5的值进行接口级别防重复点击
  *
  * @param testId 测试id
  * @param requestVo 请求参数
  * @return
  * @author daleyzou
  */
 @PostMapping("/test/{testId}")
 @NoRepeatSubmit(location = "thisIsTestBody", seconds = 6, argIndex = 1, name = "param5")
 public RsVo thisIsTestBody(@PathVariable Integer testId, @RequestBody RequestVo requestVo) throws Throwable {
  // 睡眠 5 秒,模拟业务逻辑
  Thread.sleep(5);
  return RsVo.success("test is return success");
 }

ps: jedis 2.9 和 springboot有各种兼容问题,无奈只有降低springboot的版本了

运行结果

收到响应:{"succeeded":true,"code":500,"msg":"操作过于频繁,请稍后重试","data":null}
收到响应:{"succeeded":true,"code":500,"msg":"操作过于频繁,请稍后重试","data":null}
收到响应:{"succeeded":true,"code":500,"msg":"操作过于频繁,请稍后重试","data":null}
收到响应:{"succeeded":true,"code":200,"msg":"success","data":"test is return success"}

测试用例

package com.dalelyzou.preventrepeatsubmit.controller;

import com.dalelyzou.preventrepeatsubmit.PreventrepeatsubmitApplicationTests;
import com.dalelyzou.preventrepeatsubmit.service.AsyncFeginService;
import com.dalelyzou.preventrepeatsubmit.vo.RequestVo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * TestControllerTest
 * @description 防重复点击测试类
 * @author daleyzou
 * @date 2020年09月28日 17:13
 * @version 1.3.1
 */
class TestControllerTest extends PreventrepeatsubmitApplicationTests {
 @Autowired
 AsyncFeginService asyncFeginService;

 @Test
 public void thisIsTestLocation() throws IOException {
  RequestVo requestVo = new RequestVo();
  requestVo.setParam5("random");
  ExecutorService executorService = Executors.newFixedThreadPool(4);
  for (int i = 0; i <= 3; i++) {
   executorService.execute(() -> {
    String kl = asyncFeginService.thisIsTestLocation(requestVo);
    System.err.println("收到响应:" + kl);
   });
  }
  System.in.read();
 }

 @Test
 public void thisIsTestBody() throws IOException {
  RequestVo requestVo = new RequestVo();
  requestVo.setParam5("special");
  ExecutorService executorService = Executors.newFixedThreadPool(4);
  for (int i = 0; i <= 3; i++) {
   executorService.execute(() -> {
    String kl = asyncFeginService.thisIsTestBody(requestVo);
    System.err.println("收到响应:" + kl);
   });
  }
  System.in.read();
 }
}

定义一个注解

package com.dalelyzou.preventrepeatsubmit.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * NoRepeatSubmit
 * @description 重复点击的切面
 * @author daleyzou
 * @date 2020年09月23日 14:35
 * @version 1.4.8
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {
 /**
  * 锁过期的时间
  * */
 int seconds() default 5;
 /**
  * 锁的位置
  * */
 String location() default "NoRepeatSubmit";
 /**
  * 要扫描的参数位置
  * */
 int argIndex() default 0;
 /**
  * 参数名称
  * */
 String name() default "";
}

根据指定的注解定义一个切面,根据参数中的指定值来判断请求是否重复

package com.dalelyzou.preventrepeatsubmit.aspect;

import com.dalelyzou.preventrepeatsubmit.constant.RedisKey;
import com.dalelyzou.preventrepeatsubmit.service.LockService;
import com.dalelyzou.preventrepeatsubmit.vo.RsVo;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.Map;

@Aspect
@Component
public class NoRepeatSubmitAspect {
 private static final Logger logger = LoggerFactory.getLogger(NoRepeatSubmitAspect.class);

 private static Gson gson = new Gson();

 private static final String SUFFIX = "SUFFIX";

 @Autowired
 LockService lockService;

 /**
  * 横切点
  */
 @Pointcut("@annotation(noRepeatSubmit)")
 public void repeatPoint(NoRepeatSubmit noRepeatSubmit) {
 }

 /**
  * 接收请求,并记录数据
  */
 @Around(value = "repeatPoint(noRepeatSubmit)")
 public Object doBefore(ProceedingJoinPoint joinPoint, NoRepeatSubmit noRepeatSubmit) {
  String key = RedisKey.NO_REPEAT_LOCK_PREFIX + noRepeatSubmit.location();
  Object[] args = joinPoint.getArgs();
  String name = noRepeatSubmit.name();
  int argIndex = noRepeatSubmit.argIndex();
  String suffix;
  if (StringUtils.isEmpty(name)) {
   suffix = String.valueOf(args[argIndex]);
  } else {
   Map<String, Object> keyAndValue = getKeyAndValue(args[argIndex]);
   Object valueObj = keyAndValue.get(name);
   if (valueObj == null) {
    suffix = SUFFIX;
   } else {
    suffix = String.valueOf(valueObj);
   }
  }
  key = key + ":" + suffix;
  logger.info("==================================================");
  for (Object arg : args) {
   logger.info(gson.toJson(arg));
  }
  logger.info("==================================================");
  int seconds = noRepeatSubmit.seconds();
  logger.info("lock key : " + key);
  if (!lockService.isLock(key, seconds)) {
   return RsVo.fail("操作过于频繁,请稍后重试");
  }
  try {
   Object proceed = joinPoint.proceed();
   return proceed;
  } catch (Throwable throwable) {
   logger.error("运行业务代码出错", throwable);
   throw new RuntimeException(throwable.getMessage());
  } finally {
   lockService.unLock(key);
  }
 }

 public static Map<String, Object> getKeyAndValue(Object obj) {
  Map<String, Object> map = Maps.newHashMap();
  // 得到类对象
  Class userCla = (Class) obj.getClass();
  /* 得到类中的所有属性集合 */
  Field[] fs = userCla.getDeclaredFields();
  for (int i = 0; i < fs.length; i++) {
   Field f = fs[i];
   // 设置些属性是可以访问的
   f.setAccessible(true);
   Object val = new Object();
   try {
    val = f.get(obj);
    // 得到此属性的值
    // 设置键值
    map.put(f.getName(), val);
   } catch (IllegalArgumentException e) {
    logger.error("getKeyAndValue IllegalArgumentException", e);
   } catch (IllegalAccessException e) {
    logger.error("getKeyAndValue IllegalAccessException", e);
   }

  }
  logger.info("扫描结果:" + gson.toJson(map));
  return map;
 }
}

项目完整代码

https://github.com/daleyzou/PreventRepeatSubmit

以上就是springboot实现防重复提交和防重复点击的示例的详细内容,更多关于springboot实现防重复提交和防重复点击的资料请关注我们其它相关文章!

(0)

相关推荐

  • spring boot 防止重复提交实现方法详解

    本文实例讲述了spring boot 防止重复提交实现方法.分享给大家供大家参考,具体如下: 服务器端实现方案:同一客户端在2秒内对同一URL的提交视为重复提交 上代码吧 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.or

  • Spring Boot使用AOP防止重复提交的方法示例

    在传统的web项目中,防止重复提交,通常做法是:后端生成一个唯一的提交令牌(uuid),并存储在服务端.页面提交请求携带这个提交令牌,后端验证并在第一次验证后删除该令牌,保证提交请求的唯一性. 上述的思路其实没有问题的,但是需要前后端都稍加改动,如果在业务开发完在加这个的话,改动量未免有些大了,本节的实现方案无需前端配合,纯后端处理. 思路 自定义注解 @NoRepeatSubmit 标记所有Controller中的提交请求 通过AOP 对所有标记了 @NoRepeatSubmit 的方法拦截

  • Spring boot通过AOP防止API重复请求代码实例

    这篇文章主要介绍了Spring boot通过AOP防止API重复请求代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 实现思路 基于Spring Boot 2.x 自定义注解,用来标记是哪些API是需要监控是否重复请求 通过Spring AOP来切入到Controller层,进行监控 检验重复请求的Key:Token + ServletPath + SHA1RequestParas Token:用户登录时,生成的Token Servlet

  • Spring Boot如何防止重复提交

    场景:同一个用户在2秒内对同一URL的提交视为重复提交. 思考逻辑: 1.从数据库方面考虑,数据设计的时候,某些数据有没有唯一性,如果有唯一性,要考虑设置唯一索引,可以避免脏数据. 2.从应用层面考虑,首先判断是单机服务还是分布式服务,则此时需要考虑一些缓存,利用缓存,来保证数据的重复提交. 假设是分布式应用,则可以将用户的信息,例如token和请求的url进行组装在一起,存储到缓存存,例如redis,并设置超时时间为2秒,如此来保证数据的唯一性. 以下是代码实现: Application.ja

  • springboot实现防重复提交和防重复点击的示例

    背景 同一条数据被用户点击了多次,导致数据冗余,需要防止弱网络等环境下的重复点击 目标 通过在指定的接口处添加注解,实现根据指定的接口参数来防重复点击 说明 这里的重复点击是指在指定的时间段内多次点击按钮 技术方案 springboot + redis锁 + 注解 使用 feign client 进行请求测试 最终的使用实例 1.根据接口收到 PathVariable 参数判断唯一 /** * 根据请求参数里的 PathVariable 里获取的变量进行接口级别防重复点击 * * @param

  • javascript下阻止表单重复提交、防刷新、防后退

    1 服务器端的解决方法.这是我最为推荐的方法.优点是判断准确,兼容性最大. 做法:a页面显示表单,然后提交b页面处理,处理完后重定向到c页面显示结果. 1.0 在访问a页面时在session里生成一个标志ID,例如 //伪代码  session("submitID")=random()  然后把这个值写到表单的一个hidden的input里 //伪代码  <%response.write("<input name=submitID2 type=hidden val

  • JSP防止网页刷新重复提交数据的几种方法

    本篇文章主要介绍了网页如何防止刷新重复提交与如何防止后退的解决方法,具体如下: 提交后禁用提交按钮(大部分人都是这样做的) 如果客户提交后,按F5刷新怎么办? 使用Session 在提交的页面也就是数据库处理之前: if session("ok")=true then response.write "错误,正在提交" response.end end if 数据处理完后,修改session("ok")=false. 数据处理成功马上Redirec

  • JavaWeb中HttpSession中表单的重复提交示例

    表单的重复提交 重复提交的情况: ①. 在表单提交到一个 Servlet,而 Servlet 又通过请求转发的方式响应了一个 JSP(HTML)页面,此时地址栏还保留着 Servlet 的那个路径,在响应页面点击 "刷新". ②. 在响应页面没有到达时,重复点击 "提交按钮" ③. 点击返回,再点击提交 不是重复提交的情况:点击 "返回","刷新" 原表单页面,再点击提交. 如何避免表单的重复提交:在表单中做一个标记,提交到

  • SpringBoot+Redis实现后端接口防重复提交校验的示例

    目录 1 Maven依赖 2 RepeatedlyRequestWrapper 3 RepeatableFilter 4 RepeatSubmit 5 RepeatSubmitInterceptor 6 RepeatSubmitConfig 7 RepeatSubmitController 1 Maven依赖 <!--redis缓存--> <dependency> <groupId>org.springframework.boot</groupId> <

  • SpringBoot基于redis自定义注解实现后端接口防重复提交校验

    目录 一.添加依赖 二.代码实现 三.测试 一.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.4.RELEASE</version> </dependency> <dependency> <

  • resubmit渐进式防重复提交框架示例

    目录 resubmit 创作目的 特性 maven 引入 编码 自定义 spring 整合使用 maven 引入 @EnableResubmit 注解说明 测试代码 整合 spring-boot maven 引入 代码实现 测试代码 自定义策略 自定义缓存 cache core 中指定使用 spring 中指定使用 resubmit resubmit 是一款为 java 设计的渐进式防止重复提交框架. 推荐阅读: 面试官:你们的项目中是怎么做防止重复提交的? 创作目的 有时候手动加防止重复提交很

  • 浅谈C#在网络波动时防重复提交的方法

    前几天,公司数据库出现了两条相同的数据,而且时间相同(毫秒也相同).排查原因,发现是网络波动造成了重复提交. 由于网络波动而重复提交的例子也比较多: 网络上,防重复提交的方法也很多,使用redis锁,代码层面使用lock. 但是,我没有发现一个符合我心意的解决方案.因为网上的解决方案,第一次提交返回成功,第二次提交返回失败.由于两次返回信息不一致,一次成功一次失败,我们不确定客户端是以哪个返回信息为准,虽然我们希望客户端以第一次返回成功的信息为准,但客户端也可能以第二次失败信息运行,这是一个不确

  • Spring MVC接口防数据篡改和重复提交

    本文实例为大家分享了Spring MVC接口防数据篡改和重复提交的具体代码,供大家参考,具体内容如下 一.自定义一个注解,此注解可以使用在方法上或类上 使用在方法上,表示此方法需要数据校验 使用在类上,表示此类下的所有方法需要数据校验 此注解对无参数方法不起作用 import org.springframework.stereotype.Component; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionP

  • Java结合redis实现接口防重复提交

    redis 接口防重 技术点:redis/aop 说明: 简易版本实现防止重复提交,适用范围为所有接口适用,采用注解方式,在需要防重的接口上使用注解,可以设置防重时效. 场景: 在系统中,经常会有一些接口会莫名其妙的被调用两次,可能在幂等接口中不会存在太大的问题,但是非幂等接口的处理就会导致出现脏数据,甚至影响系统的正确性. 选型参考: 在常见的防重处理分为多种,粗分为前端处理,后端处理 前端处理分为: 在按钮触发后便将按钮置灰,设置为不可用,在接口调用成功后回调处理,将按钮恢复 发送请求时,设

随机推荐