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实现禁止重复请求的方法。通过自定义注解、拦截器和容器,可以实现对重复请求的限制。

相关推荐
KYGALYX20 小时前
服务异步通信
开发语言·后端·微服务·ruby
掘了20 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
爬山算法21 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
猫头虎21 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
Moment21 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
Cobyte1 天前
AI全栈实战:使用 Python+LangChain+Vue3 构建一个 LLM 聊天应用
前端·后端·aigc
程序员侠客行1 天前
Mybatis连接池实现及池化模式
java·后端·架构·mybatis
Honmaple1 天前
QMD (Quarto Markdown) 搭建与使用指南
后端
MZ_ZXD0011 天前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
PP东1 天前
Flowable学习(二)——Flowable概念学习
java·后端·学习·flowable