cl
2024-07-17 2da22bb80f786b5ba45dbdf927102510b2c86c67
提交 | 用户 | 时间
0ca254 1 package com.jcdm.framework.interceptor;
A 2
3 import java.lang.reflect.Method;
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpServletResponse;
6 import org.springframework.stereotype.Component;
7 import org.springframework.web.method.HandlerMethod;
8 import org.springframework.web.servlet.HandlerInterceptor;
9 import com.alibaba.fastjson2.JSON;
10 import com.jcdm.common.annotation.RepeatSubmit;
11 import com.jcdm.common.core.domain.AjaxResult;
12 import com.jcdm.common.utils.ServletUtils;
13
14 /**
15  * 防止重复提交拦截器
16  *
17  * @author jc
18  */
19 @Component
20 public abstract class RepeatSubmitInterceptor implements HandlerInterceptor
21 {
22     @Override
23     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
24     {
25         if (handler instanceof HandlerMethod)
26         {
27             HandlerMethod handlerMethod = (HandlerMethod) handler;
28             Method method = handlerMethod.getMethod();
29             RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
30             if (annotation != null)
31             {
32                 if (this.isRepeatSubmit(request, annotation))
33                 {
34                     AjaxResult ajaxResult = AjaxResult.error(annotation.message());
35                     ServletUtils.renderString(response, JSON.toJSONString(ajaxResult));
36                     return false;
37                 }
38             }
39             return true;
40         }
41         else
42         {
43             return true;
44         }
45     }
46
47     /**
48      * 验证是否重复提交由子类实现具体的防重复提交的规则
49      *
50      * @param request 请求信息
51      * @param annotation 防重复注解参数
52      * @return 结果
53      * @throws Exception
54      */
55     public abstract boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation);
56 }