SpringBoot 自定义注解
新增注解类
NotRepeatSubmit.java
package com.example.demo.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 自定义注解,防止重复提交。 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface NotRepeatSubmit { /** 过期时间,单位毫秒 **/ long value() default 5000; }
ApiUti.java
package com.example.demo.annotation; import com.example.demo.token.NotRepeatSubmit; import org.springframework.messaging.handler.HandlerMethod; import java.lang.reflect.Method; /** * API工具类 */ public class ApiUtil { /** * 获取方法上的@NotRepeatSubmit注解 * * @param handler * @return */ public static NotRepeatSubmit getNotRepeatSubmit(Object handler) { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); NotRepeatSubmit annotation = method.getAnnotation(NotRepeatSubmit.class); return annotation; } return null; } }
拦截器类
MyInterceptor.java
package com.example.demo.annotation; import com.example.demo.token.NotRepeatSubmit; import io.jsonwebtoken.lang.Assert; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.concurrent.TimeUnit; /** * 拦截器 */ @Component public class MyInterceptor extends HandlerInterceptorAdapter { @Autowired private RedisTemplate redisTemplate; /** * @param request * @param response * @param handler 访问的目标方法 * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //这是请求接口时带的随机字符串 String sign = request.getHeader("sign"); // 获取注解上的超时时间 默认为5分钟 NotRepeatSubmit notRepeatSubmit = ApiUtil.getNotRepeatSubmit(handler); long expireTime = notRepeatSubmit == null ? 5 * 60 * 1000 : notRepeatSubmit.value(); // 拒绝重复调用(第一次访问时存储,过期时间和请求超时时间保持一致), 只有标注不允许重复提交注解的才会校验 if (notRepeatSubmit != null) { boolean exists = redisTemplate.hasKey(sign); Assert.isTrue(!exists, "请勿重复提交"); redisTemplate.opsForValue().set(sign, 0, expireTime, TimeUnit.MILLISECONDS); } return super.preHandle(request, response, handler); } }
WebMvcConfiguration.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * 将自己写的MyInterceptor添加到拦截配置器中 尽量使用实现 WebMvcConfigurer 的接口的方式 */ @Configuration public class WebMvcConfiguration implements WebMvcConfigurer { @Autowired private MyInterceptor myInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor) .addPathPatterns("/api/**"); } }
使用方式 控制器类
package com.example.demo.annotation; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; /** * 请求控制器 */ @Slf4j @RestController public class TestController { /** * @NotRepeatSubmit 是自定义注解 防止重复提交 * @return */ @NotRepeatSubmit(5000) @PostMapping("/api/test") public void test(String name) { } }
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
分类:
JAVA
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了