注解防止表单重复提交 发表于 2019-06-10 | 分类于 Java,SpringBoot技能大全 1. 注解1234@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface StopRepeatSubmit {} 2. 拦截处理器12345678910111213141516171819202122232425262728293031323334353637383940/** * @author pibigstar * @desc 防止表单重复提交拦截处理器 **/public class StopRepeatSubmitHandlerInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); StopRepeatSubmit annotation = method.getAnnotation(StopRepeatSubmit.class); if (annotation!=null) { // 判断是否已经提交过了 String paramter = request.getParameterMap().toString(); String url = request.getRequestURL().toString(); String token = url+paramter; Object attrToken = request.getSession().getAttribute("token"); if (attrToken == null) { // 第一次请求 request.getSession().setAttribute("token",token); return true; } if (attrToken.toString().equals(token)){ // 重复提交了 return false; } else { // 第一次提交 request.getSession().setAttribute("token",token); return true; } } return true; } else { return super.preHandle(request, response, handler); } }} 3. 使用1234@PostMapping("/submit")@StopRepeatSubmitpublic void submit(){} -------------本文结束感谢您的阅读-------------