(一)拦截器:
1.使用拦截器的第一步是如何创建拦截器,我使用的是接口HandlerInterceptorAdapter。在csdn上有很多介绍可以查看,这里就不过多论述了,比较推荐看这个:
HandlerInterceptor拦截器的使用https://blog.csdn.net/zhibo_lv/article/details/81699360
其内容就是HandlerInterceptorAdapter接口中有三个方法,三个方法对应三个不同的时间段,举例:你早上起床的时候做的事情应该在方法一(preHandle)中,起床中做的事应该在方法二(postHandle)中,最后起完床做的事情应该在方法三(afterCompletion)中。更加详细的方法介绍可以看这个:
Interceptor的基本介绍和使用preHandle、postHandle与afterCompletionhttps://blog.csdn.net/qq_34246965/article/details/122943699 在实现了HandlerInterceptorAdapter接口的类中三个方法都会有类似这样的一句:
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
我也不知道这句具体是干什么的,如果有哪位大佬知道评论告知一下。如果不知道接口.super是什么请看这个:
Super和This的介绍https://www.runoob.com/w3cnote/the-different-this-super.html接口.super介绍
https://blog.csdn.net/weixin_52597250/article/details/123299602
拦截器就像一个闸门,拦截谁他不管,只有怎么拦截,比如登录如果我查询了Session中没有用户,那么拦截器就会返回一个false不允许通过,反之就返回一个ture允许通过。
(二)WebMvcConfigurer
上面已经说了拦截器,及如何去拦截东西。那么有了怎么拦截后就需要一个东西来告诉拦截器拦截那些东西,而WebMvcConfigurer就是这个用处。不是很懂的话可以按照顺序看一遍下面的链接:
Spring MVC 介绍及使用https://blog.csdn.net/m0_64338546/article/details/132071506SpringBoot默认MVC配置
https://zhuanlan.zhihu.com/p/344541013WebMvcConfigurer介绍
https://segmentfault.com/a/1190000019448892
在WebMvcConfigurer中我们最常用的方法就是addInterceptors(InterceptorRegistry registry),在上面最后一个链接中有简单的介绍,接下来贴上几个链接是Springboot文档中的介绍(用那个看更快):
InterceptorRegistry类介绍https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/InterceptorRegistry.html 上面那个InterceptorRegistry类的方法addInterceptor会放回下面这个类
InterceptorRegistration介绍https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/InterceptorRegistration.html 这个InterceptorRegistration的作用是用来协助创建MappedInterceptor,这个MappedInterceptor的介绍如下
MappedInterceptor类介绍https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/handler/MappedInterceptor.html 上面三个内部都有更加详细的官方文档,只是是全英文的,自行阅读。如果看完后还有一些不太懂,可以看看这个网页的代码:
上面三个类的代码应用https://www.cnblogs.com/codeclock/p/13808735.html#%E6%9F%A5%E7%9C%8B%E6%BA%90%E7%A0%81 进入这个网页后直接页面查找:查看源码。还是不懂得话我也没办法了
(三)以上内容的辅助阅读
最后还有一些不错的文章作为补充,都是一些我觉得写的不错的文章,大家在看完上面的内容后感觉还是有所不足的或者没有理解透彻的话,可以查看
拦截器HandlerInterceptor和InterceptorRegistryhttps://blog.csdn.net/weixin_41986096/article/details/131309888SpringBoot源码分析:拦截器注册原理
https://blog.csdn.net/bejsoiv/article/details/111927634SpringBoot实现登录拦截器超详细
https://blog.csdn.net/weixin_54040016/article/details/129041207?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-129041207-blog-111927634.235^v43^pc_blog_bottom_relevance_base3&spm=1001.2101.3001.4242.1&utm_relevant_index=1
InterceptorRegistry类有什么作用https://blog.csdn.net/zhoqua697/article/details/130868110