spring boot 实现一个 禁止重复请求

在Spring Boot中,实现禁止重复请求可以通过以下步骤:

1.添加依赖 首先,需要添加Spring Boot的Web依赖,在pom.xml文件中添加以下依赖:

xml 复制代码
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.创建自定义注解 创建一个自定义的注解,用于标注需要进行重复请求限制的方法。可以通过@Retention(RetentionPolicy.RUNTIME)注解来指定注解的保留策略。

java 复制代码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface NoRepeatRequest {
}

3.编写拦截器 创建一个拦截器,用于拦截标有@NoRepeatRequest注解的方法。在拦截器中可以使用一个容器来存储已处理的请求,比如使用ConcurrentHashMap。

java 复制代码
@Component
public class NoRepeatRequestInterceptor implements HandlerInterceptor {

   private Map<String, Object> requestMap = new ConcurrentHashMap<>();

   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       // 获取请求的URL和方法名
       String url = request.getRequestURL().toString();
       String method = request.getMethod();

       // 生成请求的唯一标识
       String key = url + "_" + method;

       // 如果容器中已存在该请求,则表示是重复请求
       if (requestMap.containsKey(key)) {
           response.getWriter().write("Duplicate request");
           return false;
       }

       // 将请求添加到容器中
       requestMap.put(key, new Object());
       return true;
   }

   @Override
   public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
       // 请求处理完毕后,从容器中移除该请求
       String url = request.getRequestURL().toString();
       String method = request.getMethod();
       String key = url + "_" + method;
       requestMap.remove(key);
   }
}

4.注册拦截器 在配置类中注册拦截器,并将其应用于需要进行重复请求限制的方法。

java 复制代码
@Configuration
public class WebConfig implements WebMvcConfigurer {

   @Autowired
   private NoRepeatRequestInterceptor noRepeatRequestInterceptor;

   @Override
   public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(noRepeatRequestInterceptor)
               .addPathPatterns("/**")  // 可以根据具体的URL进行配置
               .excludePathPatterns("/error");  // 排除错误页面的拦截
   }
}

现在,只需要在需要进行重复请求限制的方法上添加@NoRepeatRequest注解即可。

java 复制代码
@Controller
public class TestController {

   @NoRepeatRequest
   @RequestMapping("/test")
   @ResponseBody
   public String test() {
       // 处理业务逻辑
       return "success";
   }
}

这样,当重复请求该方法时,会返回"Duplicate request",避免重复执行相同的操作。

以上就是使用Spring Boot实现禁止重复请求的方法。通过自定义注解、拦截器和容器,可以实现对重复请求的限制。

相关推荐
Albert Edison4 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
Piper蛋窝5 小时前
深入 Go 语言垃圾回收:从原理到内建类型 Slice、Map 的陷阱以及为何需要 strings.Builder
后端·go
六毛的毛8 小时前
Springboot开发常见注解一览
java·spring boot·后端
AntBlack8 小时前
拖了五个月 ,不当韭菜体验版算是正式发布了
前端·后端·python
31535669138 小时前
一个简单的脚本,让pdf开启夜间模式
前端·后端
uzong8 小时前
curl案例讲解
后端
开开心心就好9 小时前
免费PDF处理软件,支持多种操作
运维·服务器·前端·spring boot·智能手机·pdf·电脑
一只叫煤球的猫9 小时前
真实事故复盘:Redis分布式锁居然失效了?公司十年老程序员踩的坑
java·redis·后端
猴哥源码9 小时前
基于Java+SpringBoot的农事管理系统
java·spring boot
大鸡腿同学10 小时前
身弱武修法:玄之又玄,奇妙之门
后端