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

相关推荐
weixin_7042660521 小时前
SpringBoot全注解开发指南
java·spring boot·mybatis
想打游戏的程序猿1 天前
核心概念层——深入理解 Agent 是什么
后端·ai编程
woniu_maggie1 天前
SAP Web Service日志监控:如何用SRT_UTIL快速定位接口问题
后端
一线大码1 天前
Java 使用国密算法实现数据加密传输
java·spring boot·后端
Rust语言中文社区1 天前
【Rust日报】用 Rust 重写的 Turso 是一个更好的 SQLite 吗?
开发语言·数据库·后端·rust·sqlite
在屏幕前出油1 天前
06. FastAPI——中间件
后端·python·中间件·pycharm·fastapi
wuqingshun3141591 天前
说一下spring的bean的作用域
java·后端·spring
钟智强1 天前
从2.7GB到481MB:我的Docker Compose优化实战,以及为什么不能全信AI
后端·docker
华科易迅1 天前
Spring JDBC
java·后端·spring
小村儿1 天前
一起吃透 Claude Code,告别 AI 编程迷茫
前端·后端·ai编程