欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > SpringBoot防止重复提交 AOP+自定义注解+redis

SpringBoot防止重复提交 AOP+自定义注解+redis

2024/10/24 6:26:13 来源:https://blog.csdn.net/m0_57921272/article/details/140333165  浏览:    关键词:SpringBoot防止重复提交 AOP+自定义注解+redis

1.什么是重复提交呢

  在Web开发中,重复提交(也称为双重提交或重复表单提交)是指用户在没有明确意图的情况下,多次提交同一表单的情况。这可能是由于用户多次点击提交按钮、表单提交过程中的网络延迟导致用户重复点击、或者由于浏览器的自动重试机制(如在网络中断后恢复连接时)等原因造成的。

  这种情况可能造成数据库插入多条数据等等

2.用一个小例子实现防止重复提交

2.1 首先自定义一个注解,用来给方法设置多长时间内再次调用相同的数据,属于重复提交

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExpirationTime {// 可以定义一些属性,比如超时时间等long timeout() default 60; // 默认60秒
}

2.2 通过AOP在执行方法前做检查,存入到redis中,通过给redis中设置过期时间,实现防止重复提交功能

@Component
@Aspect
public class RepeatSubmitAspect {@Autowiredprivate RedisTemplate redisTemplate;@Pointcut("@annotation(com.qcby.submitageain.annotationaop.ExpirationTime)")public void repeatSubmit() {}@Around("repeatSubmit()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();StringBuffer requestURL = request.getRequestURL();// 如果需要包括查询字符串,可以添加String queryString = request.getQueryString();if (queryString != null) {requestURL.append("?").append(queryString);}String requestURL1 = requestURL.toString();Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();// 获取防重复提交注解ExpirationTime annotation = method.getAnnotation(ExpirationTime.class);//设置初始值为0,表示如果该没有这个注解,就设置过期时间为0,也就是不存入redis中long timeout=0;if(annotation!=null){timeout = annotation.timeout();}if (!redisTemplate.hasKey(requestURL1)||this.redisTemplate.opsForValue().get(requestURL1)==null) {this.redisTemplate.opsForValue().set(requestURL1, true, timeout, TimeUnit.SECONDS);try {//正常执行方法并返回return joinPoint.proceed();} catch (Throwable throwable) {throw new Throwable(throwable);}} else {// 抛出异常System.out.println("请勿重复提交");return null;}}
}

2.3 此时就可以编写controller层来测试代码是否成功啦~

@Controller
public class TestController {@RequestMapping("/a2")@ExpirationTime(timeout = 5)@ResponseBodypublic void test(){System.out.println("提交成功");}@RequestMapping("/a1")@ResponseBodypublic void test1(){System.out.println("提交成功1");}
}

2.4 此刻一个简单的防止重复提交的一个小程序就完成啦~

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com