InterceptorRegistration中addPathPatterns和excludePathPatterns使用先后顺序的区别
时间: 2024-05-06 07:16:33 浏览: 15
InterceptorRegistration 中的 `addPathPatterns` 和 `excludePathPatterns` 是用来指定哪些请求需要拦截,哪些请求不需要拦截的。它们的区别在于:
- `addPathPatterns`:指定需要拦截的请求路径,只有匹配上该路径的请求才会被拦截。
- `excludePathPatterns`:指定不需要拦截的请求路径,匹配上该路径的请求不会被拦截。
这两个方法的执行顺序是先执行 `addPathPatterns`,再执行 `excludePathPatterns`。因此,如果同一个请求路径既匹配了 `addPathPatterns`,又匹配了 `excludePathPatterns`,那么最终的结果是不拦截该请求。
举个例子,假设有以下配置:
```java
registry.addInterceptor(myInterceptor)
.addPathPatterns("/api/**")
.excludePathPatterns("/api/login");
```
上述配置表示拦截 `/api/**` 路径下的所有请求,但排除了 `/api/login` 请求。因此,请求 `/api/login` 不会被拦截,其他 `/api/**` 路径下的请求都会被拦截。注意,如果把 `excludePathPatterns` 配置放在 `addPathPatterns` 前面,那么 `/api/login` 请求就会被拦截了,这是因为 `excludePathPatterns` 的优先级更高。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![dll](https://img-home.csdnimg.cn/images/20210720083646.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)