文章目录

    • @[toc]
  • springmvc原理大概
    • 实现springMVC的HandlerInterceptor
  • 拦截器的注册
    • ssm中
    • SpringBoot中

springmvc原理大概

核心方法doDispatch()如下:

/*
通过getHandle方法,找到handler,并返回一个HandlerExecutionChain执行链(mappedHandler),
HandlerExecutionChain中有找到的handler的信息,以及拦截器信息;
*/mappedHandler = getHandler(processedRequest);... /*
通过getHandlerAdapter方法,找到合适的HandlerAdapter处理器适配器;
*/HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());.../*
在执行handler之前,先执行HandlerExecutionChain中拦截器
*/if (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}/*
处理器适配器 执行handler,返回一个ModleAndView
*/mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

https://blog.51cto.com/u_15009384/2563948

https://www.cnblogs.com/guoxiaoyu/archive/2020/07/31/13402861.html

这里具体查看getHandler方法

getHandler方法在AbstractHandlerMapping中被实现

 public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {...//注意getHandlerExecutionChain方法,HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);...}

通过getHandlerExecutionChain方法,给ExecutionChain添加拦截器

 protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ?(HandlerExecutionChain) handler : new HandlerExecutionChain(handler));String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);for (HandlerInterceptor interceptor : this.adaptedInterceptors) {if (interceptor instanceof MappedInterceptor) {MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {chain.addInterceptor(mappedInterceptor.getInterceptor());}}else {chain.addInterceptor(interceptor);}}return chain;}

SpringBoot在启动时会检查容器中所有实现了WebMvcConfigurer接口的类,因为我们重写了addInterceptors方法,所以拦截器被自动添加了。

拦截器最终在doDispatch方法的applyPreHandle方法中被执行;


实现springMVC的HandlerInterceptor

先实现springMVC的HandlerInterceptor

public abstract class HandlerInterceptorAdapter implements AsyncHandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {}@Overridepublic void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {}
preHandle:在业务处理器处理请求之前被调用。预处理,可以进行编码、安全控制、权限校验等处理;
拦截器器是链式的,一个接一个的执行,当preHandle方法返回false时,后面的拦截器就不会执行了;postHandle:在业务处理器处理请求执行完成后,生成视图之前执行。如果控制器Controller出现了异常,则不会执行此方法afterCompletion:不管有没有异常,这个afterCompletion都会被调用,在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)

拦截器的注册

自定义拦截器,先实现springMVC的HandlerInterceptor;

然后完成自定义拦截器的注册;

ssm中

(1)拦截所有Controller类里的所有处理方法

<!-- 配置拦截器:--><mvc:interceptors><!-- 会拦截所有Controller类里的所有处理方法 --><bean class="com.zy.web.HandlerInterceptor1"></bean><bean class="com.zy..web.HandlerInterceptor2"></bean></mvc:interceptors>

或者
(2)只拦截某个请求路径的处理方法

 <!-- 配置拦截器:--><mvc:interceptors><!-- 会拦截所有Controller类里的所有处理方法 --><bean class="cn.zy.web.HandlerInterceptor1"></bean><mvc:interceptor><!-- 只拦截该路径 --><mvc:mapping path="/users"/><bean class="cn.zy.web.HandlerInterceptor2"></bean></mvc:interceptor></mvc:interceptors>

SpringBoot中

@Configuration
public class WebSecurityConfig implements WebMvcConfigurer{@Overridepublic void addInterceptors(InterceptorRegistry registry){registry.addInterceptor(new AuthenticationInterceptor())//所有路径都被拦截.addPathPatterns("/**")//添加不拦截的路径.excludePathPatterns("/userLogin", "/css/**", "/images/**", "/js/**", "/login.html");registry.addInterceptor(new OtherInterceptor()).addPathPatterns("/**");}
}
``

SpringMVC、SpringBoot拦截器的实现和原理相关推荐

  1. springboot拦截器 跳过_springboot创建拦截器过程图解

    springboot创建拦截器过程图解 这篇文章主要介绍了springboot创建拦截器过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一. ...

  2. SpringBoot拦截器与过滤器

    一.拦截器与过滤器 在讲Spring boot之前,我们先了解一下过滤器和拦截器.这两者在功能方面很类似,但是在具体技术实现方面,差距还是比较大的.在分析两者的区别之前,我们先理解一下AOP的概念,A ...

  3. springboot拦截器与过滤器详解

    前言 不管是传统的ssm项目,还是springboot项目,拦截器和过滤器在项目开发中都离不开,比如说对于ssm框架类的项目来说,许多登录逻辑的前置校验,黑白名单的检查,以及部分请求的数据分析等依然需 ...

  4. Spring-Boot 拦截器

    拦截器 是springmvc提供了一个拦截器的机制,它专门用于拦截controller的路由请求. 它的本质是:AOP面向切面的编程,也就是说符合横切关注点的功能都可以考虑使用拦截器实现.比如一些应用 ...

  5. Marco's Java【SpringMVC入门(五) 之 SpringMVC的拦截器的使用】

    前言 我们知道在web开发中,一般有三大板块:Servlet(服务连接器) .Listener(监听器) 和Filter(过滤器),而今天我们要学习的拦截器可以算是一个精致的过滤器"法宝&q ...

  6. springboot + 拦截器 + 注解 实现自定义权限验证

    springboot + 拦截器 + 注解 实现自定义权限验证 最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定 ...

  7. SpringMVC——自定义拦截器、异常处理以及父子容器配置

    SpringMVC--自定义拦截器.异常处理以及父子容器配置 参考文章: (1)SpringMVC--自定义拦截器.异常处理以及父子容器配置 (2)https://www.cnblogs.com/so ...

  8. 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener

    =================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...

  9. SpringMVC应用拦截器判断用户是否登录

    拦截器定义 实现HandlerInterceptor接口,实现接口方法. import javax.servlet.http.HttpServletRequest; import javax.serv ...

  10. SpringMVC经典系列-14自己定义SpringMVC的拦截器---【LinusZhu】

    注意:此文章是个人原创.希望有转载须要的朋友们标明文章出处.假设各位朋友们认为写的还好,就给个赞哈.你的鼓舞是我创作的最大动力,LinusZhu在此表示十分感谢,当然文章中如有纰漏,请联系linusz ...

最新文章

  1. MySQL安装时MySQL server一直安装失败日志显示This application requires Visual Studio 2013 Redistributable...
  2. NOIP2001-普及组复赛-第一题-数的计算
  3. redis服务器防止入侵,加ip,密码限制
  4. python编辑公式_分分钟甩Word几条街,Python编辑公式竟可以如此简单
  5. 慎用window.showModalDialog()
  6. 计算机语言php自学,php自学需要多久?
  7. android9.0变化,十年巨变 Android 1.0对比Android 9
  8. 用Nginx作反向代理简单多了
  9. C#利用NPOI导出Excel
  10. kotlin版本组件化+mvvm项目架构
  11. Chapter3:Gradle依赖管理
  12. 项目实战 - tpshop商城项目环境搭建
  13. 汉字转拼音之Jpinyin 简单使用
  14. 微信小程序 关于下载文件、打开文件预览文件(wx.downloadFile和wx.openDocument)
  15. 红米k50电竞版和红米k40游戏增强版哪个好 参数对比
  16. 2. 企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时, 奖金可提10%;利润高于10万元, 低于20万元时, 低于10万元的部分按10%提成,...
  17. 怎么样去提升网站长尾词在百度搜狗360的排名?
  18. 转行软件测试,现状以及就业前景,你后悔了吗?
  19. Canvas之绘制时钟
  20. Vue-一些常用的工具类

热门文章

  1. html资源路径404,webpack vue 项目打包生成的文件,资源文件报404问题的修复方法(总结篇)...
  2. STM32——库函数版——交叉闪烁灯程序
  3. 信用评分-(scorecard)记分卡开发流程,详细介绍分数校准原理calibration
  4. [PAT B1023]组个最小数
  5. 算法:Jump Game
  6. 2021-09-08推荐系统有如下三大类算法
  7. 2021-09-0818. 四数之和
  8. 357.计算各个位数不同的数字个数
  9. 334.递增的三元子序列
  10. linux下安装xgboost