首页 > 编程知识 正文

spring拦截器和aop执行顺序,springmvc拦截请求

时间:2023-05-05 18:25:19 阅读:176437 作者:187

1、总结差异

执行顺序:过滤-拦截-切面过滤,拦截为请求级拦截; 切面是方法级方框2,根据框架,dome

说明:拦截器继承HandlerInterceptorAdapter接口,实现preHandle、postHandle、afterCompletion方法。

)1) preHandle方法用于拦截处理器,顾名思义,它在Controller操作之前被调用。

)2)只有当前此interceptor pre handle方法的返回值为true时,才会执行此方法。

)3)只有当前相应的interceptor pre handle方法的返回值必须为true时,才会执行此方法。

package com.RDM.framework.interceptor; import java.lang.reflect.Method; import javax.servlet.http.http servlet request; import javax.servlet.http.http无servlet轮询; import com.RDM.com mon.enums.repeatsubmittype; import com.RDM.com mon.utils.string utils; importorg.spring framework.lang.nullable; importorg.spring framework.stereotype.com ponent; importorg.spring framework.web.method.handler method; importorg.spring framework.web.servlet.modelandview; importorg.spring framework.web.servlet.handler.handlerinterceptoradapter; import com.Alibaba.fast JSON.JSON object; import com.RDM.com mon.annotation.repeat submit; import com.RDM.com mon.core.domain.Ajax result; import com.RDM.com mon.utils.servlet utils; /** *防止再次提交拦截程序* * @author RDM */@ componentpublicabstractclassrepeatsubmitinterceptorextendshandlerinterceptoradapter { @ overridepublicvoidposthandle (httpsttttter ) sthandle、HttpServletResponse response、Object handler、@ nullablemodelandviewmodelandview (throws exception { extracted } respon ) HHT TTP } @ overridepublicbooleanprehandle (httpservletrequestrequest,HttpServletResponse response, object handler (throws exception (if ) HandlerinstanceofHandlermethod ) handlermethod=) Handlermethod ) handler repeatsubmitannotation=method.get annotation (repeat submit.class; if (匿名!=null (string value=annotation.value ); string utils.is blank (value ) { value=method.getdeclaringclass ).getName; } if (this.isrepeatsubmit (request,value,annotation.type ) ) ajaxresultajaxresult=Ajax result.error '允许重复返回假; }

} 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(); }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。