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实现防重复提交和防重复点击的更多相关文章
- asp.net网页防刷新重复提交、防后退解决办法!
原文发布时间为:2008-10-14 -- 来源于本人的百度文章 [由搬家工具导入] 1、提交后 禁用提交按钮(像CSDN这样)2、数据处理成功马上跳转到另外一个页面! 操作后刷新的确是个问题,你可以 ...
- javaEE开发中使用session同步和token机制来防止并发重复提交
javaEE开发中使用session同步和token机制来防止并发重复提交 通常在普通的操作当中,我们不需要处理重复提交的,而且有很多方法来防止重复提交.比如在登陆过程中,通过使用redirect,可 ...
- Spring Boot (一) 校验表单重复提交
一.前言 在某些情况下,由于网速慢,用户操作有误(连续点击两下提交按钮),页面卡顿等原因,可能会出现表单数据重复提交造成数据库保存多条重复数据. 存在如上问题可以交给前端解决,判断多长时间内不能再次点 ...
- ASP.NET Web Form和MVC中防止F5刷新引起的重复提交问题
转载 http://www.cnblogs.com/hiteddy/archive/2012/03/29/Prevent_Resubmit_When_Refresh_Reload_In_ASP_NET ...
- .net防止刷新重复提交(转)
net 防止重复提交 微软防止重复提交方案,修改版 Code ; i < cookie.Values.Count; i++) log.Info(" ...
- java web解决表单重复提交问题
我们大家再进行web开发的时候,必不可少会遇见表单重复提交问题.今天就来给总结如何解决表单提交问题,欢迎大家交流指正. 首先我们在讨论如何解决表单重复提交问题之前先来解决三个问题:1.什么叫表单重复提 ...
- Struts(二十七):使用token或tokenSession防止表单重复提交
什么是表单重复提交 表单重复提交包括以下几种情况: 前提:不刷新表单页面 1.多次点击“提交”按钮后,重复提交了多次: 2.已经提交成功之后,按“回退”按钮之后,在点击“提交”按钮后,提交成功: 3. ...
- easyUI的form表单重复提交处理
1. 问题 生产环境出现过新增用户提交, 入库两条重复数据的情况; 但是我查看代码, 页面做了校验, 后台插入数据也做了校验; 出现这种几率的事件的非常小的, 但是还是会碰到, 客户会对我们的产品产 ...
- Restful api 防止重复提交
当前很多网站是前后分离的,前端(android,iso,h5)通过restful API 调用 后端服务器,这就存在一个问题,对于创建操作,比如购买某个商品,如果由于某种原因,手抖,控件bug,网络错 ...
随机推荐
- 区块链入门到实战(27)之以太坊(Ethereum) – 智能合约开发
智能合约的优点 与传统合同相比,智能合约有一些显著优点: 不需要中间人 费用低 代码就是规则 区块链网络中有多个备份,不用担心丢失 避免人工错误 无需信任,就可履行协议 匿名履行协议 以太坊(Ethe ...
- DVWA之文件包含(一)
文件包含又可以是本地文件包含或远程文件包含. 本地文件包含:文件存在本地目录 远程文件包含:文件共享或存在网络上,需要访问 <?php // The page we wish to displa ...
- 解Bug之路-dubbo应用无法重连zookeeper
前言 dubbo是一个成熟且被广泛运用的框架.饶是如此,在某些极端条件下基于dubbo的应用还会出现无法重连zookeeper的问题.由于此问题容易导致比较大的故障,所以笔者费了一番功夫去定位,现将排 ...
- Java中解析wav音频文件信息:音频声道数,采样频率,采样位数、声音尺寸
前言:请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i 音频解析方法: public static int toInt(byte[] b) { return ((b[3] << 2 ...
- 5分钟快速学会xpath定位
今天我们先来和大家说一下appium,首先教大家如何定位xpath,五分钟即可学会:例:现在我想定位下面这个登录按钮: xpath该怎么写呢? 先不管三七二十几,先写//,然后找你要定位元素最近的可以 ...
- Apache Pulsar 社区周报:08-15 ~ 08-21
关于 Apache Pulsar Apache Pulsar 是 Apache 软件基金会顶级项目,是下一代云原生分布式消息流平台,集消息.存储.轻量化函数式计算为一体,采用计算与存储分离架构设计,支 ...
- systemctl 如何启动、关闭、启用/禁用服务
启动服务:systemctl start xxx.service 关闭服务:systemctl stop xxx.service 重启服务:systemctl restart xxx.service ...
- 聊一聊RocketMQ的注册中心NameServer
前言 上次我们一起了解了RocketMQ的基本架构原理,那简单的回顾一下RocketMQ的架构组成. RocketMQ其实包含了四个核心部分,NameServer.Broker.Producer.Co ...
- 简单对比了一下MonoXml与SystemXml在Unity下的表现
测试代码 public class NewBehaviourScript : MonoBehaviour { // Use this for initialization void Start () ...
- 记录call、apply、bind的源码
记录一下call.apply.bind的源码,然后从根本上明白其用法. 都知道call.apply与bind的用法,call(this,...arguments).apply(this,[argume ...