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才可以)