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 {
    @Autowired
    private 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)
    @ResponseBody
    public void test(){
        System.out.println("提交成功");
    }
    @RequestMapping("/a1")
    @ResponseBody
    public void test1(){
        System.out.println("提交成功1");
    }
}

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

相关推荐

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

    2024-07-11 13:44:04       23 阅读
  2. Spring Cloud项目如何防止重复提交定义注解

    2024-07-11 13:44:04       60 阅读
  3. Spring定义注解+AOP实现接口防重复提交

    2024-07-11 13:44:04       37 阅读
  4. Redis定义注解实现重复

    2024-07-11 13:44:04       52 阅读
  5. SpringBoot使用AOP防止接口重复提交

    2024-07-11 13:44:04       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 13:44:04       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 13:44:04       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 13:44:04       57 阅读
  4. Python语言-面向对象

    2024-07-11 13:44:04       68 阅读

热门阅读

  1. 在Spring Boot中实现多租户架构的数据隔离

    2024-07-11 13:44:04       21 阅读
  2. LeetCode --- 2119. A Number After a Double Reversal 解题报告

    2024-07-11 13:44:04       19 阅读
  3. sublime使用

    2024-07-11 13:44:04       21 阅读
  4. Linux Conda简介

    2024-07-11 13:44:04       22 阅读
  5. 数据结构笔记之线索二叉树找前驱后继

    2024-07-11 13:44:04       21 阅读
  6. Mybatis之动态sql、缓存、分页、配置数据源

    2024-07-11 13:44:04       17 阅读
  7. python的入门知识(下)

    2024-07-11 13:44:04       23 阅读