PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/src/main/java/com/ruoyi/framework/interceptor/RepeatSubmitInterceptor.java

https://gitlab.com/mrsunchangemyselfsun/ruoyi-vue
Java | 55 lines | 40 code | 3 blank | 12 comment | 4 complexity | 75dcf575964ced690a68d0ee311b418e MD5 | raw file
  1. package com.ruoyi.framework.interceptor;
  2. import java.lang.reflect.Method;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.springframework.stereotype.Component;
  6. import org.springframework.web.method.HandlerMethod;
  7. import org.springframework.web.servlet.HandlerInterceptor;
  8. import com.alibaba.fastjson.JSONObject;
  9. import com.ruoyi.common.annotation.RepeatSubmit;
  10. import com.ruoyi.common.core.domain.AjaxResult;
  11. import com.ruoyi.common.utils.ServletUtils;
  12. /**
  13. * 防止重复提交拦截器
  14. *
  15. * @author ruoyi
  16. */
  17. @Component
  18. public abstract class RepeatSubmitInterceptor implements HandlerInterceptor
  19. {
  20. @Override
  21. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
  22. {
  23. if (handler instanceof HandlerMethod)
  24. {
  25. HandlerMethod handlerMethod = (HandlerMethod) handler;
  26. Method method = handlerMethod.getMethod();
  27. RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
  28. if (annotation != null)
  29. {
  30. if (this.isRepeatSubmit(request, annotation))
  31. {
  32. AjaxResult ajaxResult = AjaxResult.error(annotation.message());
  33. ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult));
  34. return false;
  35. }
  36. }
  37. return true;
  38. }
  39. else
  40. {
  41. return true;
  42. }
  43. }
  44. /**
  45. * 验证是否重复提交由子类实现具体的防重复提交的规则
  46. *
  47. * @param request
  48. * @return
  49. * @throws Exception
  50. */
  51. public abstract boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation);
  52. }