SpringBoot(五):SpringBoot使用拦截器
2024-10-19 05:42:52
1.按照SpringMVC的方式编写一个拦截器:
2.配置一个类 implements WebMvcConfigurer 接口 为该类添加注解@Configuration (等价于一个spring的xml文件 比如applicationContext.xml) 标注一个配置类,让Springboot扫描到。覆盖其中的方法并添加已经编写好的拦截器
贴段代码:
@Configuration // 等价于一个spring的xml文件 比如applicationContext.xml
public class WebConfig implements WebMvcConfigurer {
/**
* 添加拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 拦截器要拦截的路径
String [] pathPatterns = {
"/**"
};
// 拦截器不拦截的路径
String [] excluedPathPatterns = {
"/boot/hello",
"boot/jsp"
};
registry.addInterceptor(new LoginInterceptor()).addPathPatterns(pathPatterns).excludePathPatterns(excluedPathPatterns); //如果项目中有多个拦截器,把上面代码在复制一行,修改参数即可 }
}
addPathPatterns(pathPatterns)和excludePathPatterns(excluedPathPatterns)是可变参数,二u过要拦截的路径只有一条,可以直接写路径名字,不用写数组,如
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/boot/**").excludePathPatterns(/boot/login);
最新文章
- 纯html页面之间传参
- WIN32 窗口封装类实现
- C语言位运算详解[转]
- js私有共有成员
- JZ2440开发笔记(9)——位置无关代码设计【转】
- Kernel Bypass &; Offload 介绍
- 测试linux和window下 jdk最大能使用多大内存
- Codeforces 437C The Child and Toy(贪心)
- 一些Android经验
- linux新建用户登录不了
- 关于使用Git的几点小技巧
- Visual Studio Code自定义快捷键(eclipse习惯)
- java:找出占用CPU资源最多的那个线程
- 服务器配置用户信息、ssh免密码登录和防火墙等安全配置
- 【English 】20190319
- BZOJ 3669 魔法森林
- jdbc工具类1.0
- Arrow functions and the ‘this’ keyword
- JS-几大排序算法(更新中...)
- dubbox2.8.4例子教程一
热门文章
- spoj687 REPEATS - Repeats (后缀数组+rmq)
- Codeforces Round #345 (Div. 1) C. Table Compression (并查集)
- Best Reward &;&; Girls' research
- - 迷宫问题 POJ - 3984 bfs记录路径并输出最短路径
- DNA Sequence POJ - 2778 AC自动机 &;&; 矩阵快速幂
- C# 之 dynamic
- LSTM - 长短期记忆网络
- [Golang]-6 超时处理、非阻塞通道操作、通道的关闭和遍历
- MySQL——时间、字符串、时间戳相互转换
- bzoj1013球形空间产生器sphere 高斯消元(有系统差的写法