赞
踩
参考文章:https://www.jianshu.com/p/09c6b05b670a
小程序上传图片接口,经常出现短时间提交多条记录的问题,程序需要多线程处理3秒
思路:根据登陆token 设置no-repeat redis缓存 过期时间为3秒 3秒内提交不生效
- package com.common;
-
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- @Target(ElementType.METHOD) // 作用到方法上
- @Retention(RetentionPolicy.RUNTIME) // 运行时有效
- /**
- * @功能描述 防止重复提交标记注解
- * @author www.gaozz.club
- * @date 2018-08-26
- */
- public @interface NoRepeatSubmit {
- }

pom.xml Application.java 非重点忽略
- @Slf4j
- @Aspect
- @Component
- /**
- * @功能描述 aop解析注解
- * @author zhq
- * @date 2020-07-02
- */
- public class NoRepeatSubmitAop {
-
- private final static Logger logger = LoggerFactory.getLogger(NoRepeatSubmitAop.class);
-
- @Autowired
- private RedisTemplate redisTemplate;
- @Autowired
- private JwtTokenUtil jwtTokenUtil;
- @Value("${jwt.noRepeat}")
- private String noRepeat;
-
- @Around("execution(* com.xxx..*Controller.*(..)) && @annotation(nrs)")
- public Object arround(ProceedingJoinPoint pjp, NoRepeatSubmit nrs) {
- try {
- //ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
- String token = noRepeat+jwtTokenUtil.getToken(request);
- if (redisTemplate.opsForValue().get(token) == null) {// 如果缓存中有这个url视为重复提交
- redisTemplate.opsForValue().set(token, 0, 3, TimeUnit.SECONDS);
- Object o = pjp.proceed();
- return o;
- } else {
- logger.error("重复提交");
- System.out.println("重复提交");
- return null;
- }
- } catch (Throwable e) {
- e.printStackTrace();
- logger.error("验证重复提交时出现未知异常!");
- return new ReturnVo(ReturnCodeMsg.fail,"验证重复提交时出现未知异常!",null);
- //return "{\"code\":-889,\"message\":\"验证重复提交时出现未知异常!\"}";
- }
-
- }
-
- }

application.yml
jwt:
# 防止重复key noRepeat: no-repeat/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。