当前位置:网站首页>解决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));
}
}
}发起请求

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

边栏推荐
- Introduction to web security SSH testing and defense
- vim 常用快捷键
- AtCoder B - Pizza
- 扫雷游戏
- did you register the component correctly
- 【ASP.NET Core】选项模式的相关接口
- Applet avatar group style
- Failure after reinstalling the system (error: Reboot and select proper boot device or insert boot media in selected boot device)
- 20.ref与props
- 17.生命周期
猜你喜欢

When using polymorphism, two ideas to judge whether it can be transformed downward

Parity rearrangement of Bm14 linked list

How important is 5g dual card and dual access?

小程序頭像組樣式

TASK03|回归

21. Mix in details

2022DASCTF MAY

Osgearth uses sundog's Triton ocean and silverlining cloud effects

I deliberately leave a loophole in the code. Is it illegal?

安装Win11找不到固态硬盘如何解决?
随机推荐
el-upload实现上传文件预览
Applet avatar group style
Chinese [easy to understand] cannot be set when installing SVN localization package
Energy principle and variational method note 12: minimum potential energy principle
21.mixin混入详解
剑指 Offer II 115. 重建序列
absl教程(四):Strings Library
17. Life cycle
JDK installation package and MySQL installation package sorting
如何在OneFlow中新增算子
13. Roman to Integer罗马数字转整数
2022/7/22 训练日志
2022DASCTF MAY
Osgearth uses sundog's Triton ocean and silverlining cloud effects
Leetcode 219. duplicate Element II exists (yes, resolved)
关于网段CIDR的笔记
Adobe Acrobat两个强大的插件
Leetcode 152. product maximum subarray (brute force cracking can actually pass!)
Debian | Can’t locate Debian/Debhelper/Sequence/germinate.pm in @INC
The numerical sequence caused by the PostgreSQL sequence cache parameter is discontinuous with interval gap