相关知识
关于拦截器的相关知识可参考:https://jinnianshilongnian.iteye.com/blog/1670856
本章目标
学会在 SpringBoot 的项目中怎么配置拦截器
操作步骤
添加依赖
引入 Spring Boot Starter 父工程

引入 spring-boot-starter-web 的依赖

编码
编写拦截器


注册拦截器
WebMvcConfigurer 接口提供了对 SpringMVC 的个性化配置。实现 WebMvcConfigurer 接口的 addInterceptors 方法进行拦截器的注册。

编写 controller 接口


验证结果
编写测试用例

源码地址
本章源码 : https://github.com/caiyuanzi-song/boot.git
总结
使用 SpringMVC 时是使用 XML 进行注册,SpringBoot 则推荐使用代码进行注册,最终结果其实是一样的,所以只需要知道操作步骤即可。