当前位置:网站首页>解决1秒钟内,用户快速点击,重复请求的问题
解决1秒钟内,用户快速点击,重复请求的问题
2022-07-23 19:51:00 【bulingbuling^_^】
前端也可以处理这个问题,到时绕过前端的情况,就需要后端处理了。
后端处理逻辑:自定义注解+AOP+Redis自动过期key。
1.首先自定义一个注解@NoRepeatSubmit
// 作用到方法上
@Target(ElementType.METHOD)
// 运行时有效
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {
/**
* 默认时间3秒
*/
int time() default 3 * 1000;
}2.在需要拦截的controller上加上注解,作为pointcut切入点。
@RestController
public class HomeController {
@NoRepeatSubmit
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(@RequestParam String userName) {
System.out.println(userName);
return userName;
}
@NoRepeatSubmit
@RequestMapping(value = "/login2", method = RequestMethod.GET)
public String login2(@RequestParam String userName2) {
System.out.println(userName2);
return userName2;
}
}3.在切面类中判断是否重复提交。
@Slf4j
@Aspect
@Component
public class NoRepeatSubmitAspect {
@Autowired
private RedisTemplate redisTemplate;
@Pointcut("@annotation(noRepeatSubmit)")
public void pointcut(NoRepeatSubmit noRepeatSubmit) {
}
@Before("pointcut(noRepeatSubmit)")
public void before(final JoinPoint joinPoint, NoRepeatSubmit noRepeatSubmit) throws Exception {
log.info("joinPoint:"+joinPoint);
//获取目标类的名称
String target = joinPoint.getTarget().getClass().getName();
//获取目标类的方法
String methodName = joinPoint.getSignature().getName();
log.info("target:"+target);
log.info("methodName:"+methodName);
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
log.info("request:"+request);
String url = request.getRequestURL().toString();
log.info("url:"+url);
String token = request.getHeader("token");
log.info("token:"+token);
//自行定义符合业务需要的key
String redisKey = target+"_"+methodName+"_"+token;
if ( redisTemplate.hasKey(redisKey)) {
throw new Exception( "重复提交");
}else {
//此处设置的1秒过期
redisTemplate.opsForValue().set(redisKey, "不要重复提交!!!",1000, TimeUnit.MILLISECONDS);
log.info("value:::::"+redisTemplate.opsForValue().get(redisKey));
}
}
}发起请求

当快速点击两次,就会报错啦

边栏推荐
- Data warehouse 4.0 notes - data warehouse environment construction - DataGrid preparation and data preparation
- next数值型数据类型()出现输入错误后,下次依然能正常输入
- Uncover the working principle of solid state disk
- 【C语言】通讯录(静态版本)
- AtCoder B - Pizza
- After the input error of next numerical data type () occurs, it can still be input normally next time
- Atelier macoll - notes de développement de la secte de l'ours 2
- The numerical sequence caused by the PostgreSQL sequence cache parameter is discontinuous with interval gap
- 数组——977. 有序数组的平方
- Leetcode 216. combined sum III
猜你喜欢

Drools(1):Drools简介

不用MQTT C库就能实现MQTT连接、订阅和发布

干货!神经网络中的隐性稀疏正则效应
![[development experience] development project trample pit collection [continuous update]](/img/02/7bea3bf09e9a27b6ab74399639f197.png)
[development experience] development project trample pit collection [continuous update]

MongoDB-查询语句中逻辑运算符not、and、or、nor用法介绍

千呼万唤,5G双卡双通到底有多重要?

剑指 Offer II 115. 重建序列

TASK03|回归

osgearth2.8编译siverlining云效果

Energy principle and variational method note 12: minimum potential energy principle
随机推荐
Phar deserialization
如何合理地估算线程池大小
梅科尔工作室-小熊派开发笔记3
Mecol Studio - Little Bear Development Notes 3
Set asp Net MVC site default page is the specified page
Mekol Studio - Little Bear Development Notes 2
When using polymorphism, two ideas to judge whether it can be transformed downward
121. 买卖股票的最佳时机
13. Roman to Integer罗马数字转整数
AtCoder——Subtree K-th Max
牛客C基础题练习
shell脚本中$#、$*、[email protected]、$?、$0等含义一文搞懂
2022/7/21 training summary
Debian | Can’t locate Debian/Debhelper/Sequence/germinate.pm in @INC
MongoDB-查询语句中逻辑运算符not、and、or、nor用法介绍
Win11没有Word文档怎么办?Win11没有Word文档解决教程
2022山东老博会,山东养老展,中国国际养老服务业展9月举办
JDK installation package and MySQL installation package sorting
Leetcode 228. 汇总区间(可以,已解决)
MongoDB-查询语句中$exists以及结合$ne、$nin、$nor、$not使用介绍