当前位置:网站首页>Solve the problem that the user clicks quickly and repeats the request within 1 second
Solve the problem that the user clicks quickly and repeats the request within 1 second
2022-07-23 20:25:00 【bulingbuling^_^】
The front end can also handle this problem , Bypass the front end at that time , You need back-end processing .
Back end processing logic : Custom annotation +AOP+Redis Automatically expire key.
1. First, customize an annotation @NoRepeatSubmit
// It works on the method
@Target(ElementType.METHOD)
// Run time effective
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {
/**
* Default time 3 second
*/
int time() default 3 * 1000;
}2. Where interception is required controller Add a note to it , As pointcut The breakthrough point .
@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. Determine whether to submit repeatedly in the facet class .
@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);
// Get the name of the target class
String target = joinPoint.getTarget().getClass().getName();
// Get the method of the target class
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);
// Define what meets the business needs key
String redisKey = target+"_"+methodName+"_"+token;
if ( redisTemplate.hasKey(redisKey)) {
throw new Exception( " Repeated submission ");
}else {
// Set here 1 Seconds expired
redisTemplate.opsForValue().set(redisKey, " Don't repeat the submission !!!",1000, TimeUnit.MILLISECONDS);
log.info("value:::::"+redisTemplate.opsForValue().get(redisKey));
}
}
}Initiate request

When you quickly click twice , It will be wrong

边栏推荐
- 多子系统多业务模块的复杂数据处理——基于指令集物联网操作系统的项目开发实践
- 数仓4.0笔记——数仓环境搭建—— DataGrip准备和数据准备
- 使用多态时,判断能否向下转型的两种思路
- next数值型数据类型()出现输入错误后,下次依然能正常输入
- What antenna is used for ant interface_ There is an interface at the back of the TV that says standard ant 75 Euro input. What does it mean, antenna? Can you connect the closed route "Suggested collec
- Debian | Can’t locate Debian/Debhelper/Sequence/germinate.pm in @INC
- [unity project practice] level unlocking
- Reduced order method of linear algebraic determinant calculation method
- Odrive application 6 encoder
- 2022DASCTF MAY
猜你喜欢

梅科尔工作室-小熊派开发笔记3

AtCoder B - Pizza
![[ar learning] - II. Environment construction](/img/e8/c20de6a46ef70b6eb49684d685e4cd.png)
[ar learning] - II. Environment construction

2022DASCTF MAY

TASK03|回归

MongoDB-查询语句中$exists以及结合$ne、$nin、$nor、$not使用介绍

AtCoder——Subtree K-th Max

Drools(1):Drools简介

EXCEL的密码相关

The numerical sequence caused by the PostgreSQL sequence cache parameter is discontinuous with interval gap
随机推荐
Leetcode 152. 乘积最大子数组(暴力破解居然可以通过!)
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 5
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 6
Atelier macoll - notes de développement de la secte de l'ours 2
BM14 链表的奇偶重排
osgearth2.8编译siverlining云效果
百度地图数据可视化
QT With OpenGL(帧缓存篇)
多子系统多业务模块的复杂数据处理——基于指令集物联网操作系统的项目开发实践
Odrive application 6 encoder
【ASP.NET Core】选项模式的相关接口
扫雷游戏
QT下assimp库的模型加载
121. 买卖股票的最佳时机
Discussion on the usage of scanf () and getchar ()
微软网站上关于在Edge浏览器中打开或关闭smartScreen的说明有误
How to solve the problem that the solid state disk cannot be found when installing win11?
数组——11. 盛最多水的容器
osgearth使用sundog公司的triton海洋和silverlining云彩效果
Leetcode 209. subarray with the smallest length