1、总结区别
- 执行顺序:过滤器->拦截器->切面
- 过滤器、拦截器属于请求层面的拦截;切面属于方法层面的拦截
2、若依框架 dome
说明:拦截器是继承 HandlerInterceptorAdapter 接口,实现preHandle、postHandle、afterCompletion方法。
(1) preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用。
(2)这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。
(3)该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。
package com.rdm.framework.interceptor;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rdm.common.enums.RepeatSubmitType;
import com.rdm.common.utils.StringUtils;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.alibaba.fastjson.JSONObject;
import com.rdm.common.annotation.RepeatSubmit;
import com.rdm.common.core.domain.AjaxResult;
import com.rdm.common.utils.ServletUtils;
/**
* 防止重复提交拦截器
*
* @author RDM
*/
@Component
public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter {
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
extracted(request, handler);
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
if (annotation != null) {
String value= annotation.value();
if(StringUtils.isBlank(value)){
value=method.getDeclaringClass().getName();
}
if (this.isRepeatSubmit(request, value,annotation.type())) {
AjaxResult ajaxResult = AjaxResult.error("不允许重复提交,请稍后再试");
ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult));
return false;
}
}
return true;
} else {
return super.preHandle(request, response, handler);
}
}
/**
* 添加此方法,可以防止方法中抛出异常,之后导致下一次正常提交无法执行的问题
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
extracted(request, handler);
}
/**
* 验证是否重复提交由子类实现具体的防重复提交的规则
*
* @param request
* @return
* @throws Exception
*/
public abstract boolean isRepeatSubmit(HttpServletRequest request, String value, RepeatSubmitType type);
/**
* 接口结束添加约束
*
* @param request
* @return
* @throws Exception
*/
public abstract void removeRepeatSubmit(HttpServletRequest request, String value, RepeatSubmitType type);
/**
* 接口结束取消约束
* @param request
* @param handler
*/
private void extracted(HttpServletRequest request, Object handler) {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
if(annotation!=null){
String value= annotation.value();
if(StringUtils.isBlank(value)){
value=method.getDeclaringClass().getName();
}
this.removeRepeatSubmit(request,value,annotation.type());
}
}
}
}
package com.rdm.classification.controller;
import com.rdm.classification.service.IClassificationDictRecordService;
import com.rdm.classification.vo.ClassificationDictRecordInsertVO;
import com.rdm.common.annotation.Log;
import com.rdm.common.annotation.RepeatSubmit;
import com.rdm.common.core.controller.BaseController;
import com.rdm.common.core.domain.AjaxResult;
import com.rdm.common.core.domain.BizResult;
import com.rdm.common.enums.BusinessType;
import com.rdm.common.enums.RepeatSubmitType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 分类树字典分组名记录信息
*
* @author RDM
* @date 2021-07-22
*/
@Api(tags = "分类树字典分组名记录信息管理")
@RestController
@RequestMapping("/classification/classificationDictRecord")
public class ClassificationDictRecordController extends BaseController {
@Autowired
private IClassificationDictRecordService iClassificationDictRecordService;
/**
* 用户勾选或取消分组名后保存操作记录,默认前端勾选所有分组。
* 取消分组时保存操作记录
* 勾选分组时删除操作记录
*/
@Log(title = "用户勾选或取消分组名后保存操作记录", businessType = BusinessType.INSERT)
@ApiOperation("用户勾选或取消分组名后保存操作记录")
@PostMapping
@RepeatSubmit(type = RepeatSubmitType.GLOBAL)
public AjaxResult check(@Validated @RequestBody ClassificationDictRecordInsertVO classificationDictRecordInsertVO) {
BizResult<String> bizResult = iClassificationDictRecordService.check(classificationDictRecordInsertVO);
return bizResult.toAjaxResult();
}
}