赞
踩
- /**
- * @Description 防止重复提交注解
- * @Author jinpk
- * @Date 2022/8/15 10:51
- */
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface NoRepeatSubmit {
-
- }
- /**
- * @Description 定义切面,实现拦截功能
- * @Author jinpk
- * @Date 2022/8/15 10:58
- */
- @Aspect
- @Component
- public class NoRepeatSubmitAspect {
-
- protected final Logger log = LoggerFactory.getLogger(this.getClass());
-
- @Autowired
- RedisTemplate redisTemplate;
-
- //临界值
- @Value("${redisConfig.criticality}")
- private Integer criticality;
-
- /**
- * 横切点
- */
- @Pointcut("@annotation(noRepeatSubmit)")
- public void repeatPoint(NoRepeatSubmit noRepeatSubmit) {
- }
-
- /**
- * 接收请求,并记录数据
- */
- @Around(value = "repeatPoint(noRepeatSubmit)")
- public Object doBefore(ProceedingJoinPoint joinPoint, NoRepeatSubmit noRepeatSubmit) {
- //参数
- Object[] args = joinPoint.getArgs();
- //参数转化成json
- String data = JsonUtils.objectToJson(args);
- //参数加密
- String key = MD5.stringToMD5(data);
- //方法名
- String methodName = joinPoint.getSignature().toShortString();
- log.info("进行防重复提交");
- try {
- // 如果缓存中有这个url视为重复提交
- if (redisTemplate.boundHashOps("repeat:key").get(key) == null) {
- //获取全部的key
- Set<String> keys = redisTemplate.boundHashOps("repeat:key").keys();
- if (!CollectionUtils.isEmpty(keys)){
- System.out.println("========redis:防止重复提交的key的数量=========:"+keys.size());
- //大于临界值时 redis repeat:key 此文件夹下所有数据清零
- if (keys.size() >= criticality){
- redisTemplate.delete("repeat:key");
- }
- }
- redisTemplate.boundHashOps("repeat:key").put(key, methodName);
- return joinPoint.proceed();
- }else {
- log.info("重复提交");
- return AjaxResult.error("请勿重复提交!");
- }
- } catch (Throwable throwable) {
- log.info("运行业务代码出错", throwable);
- return AjaxResult.error("请求异常请稍后!");
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。