registry.addInterceptor(myInterceptor) .addPathPatterns("/**") .excludePathPatterns("/login.html")
时间: 2023-06-15 15:06:15 浏览: 54
这是一个使用 Spring MVC 框架的拦截器配置代码。该代码将 myInterceptor 拦截器添加到所有请求路径("/**")中,并排除了 "/login.html" 路径,即在访问 "/login.html" 时不会被该拦截器拦截。拦截器可以在请求处理前、请求处理后以及视图渲染后执行一些操作,比如权限校验、日志记录等。
相关问题
InterceptorRegistration中addPathPatterns和excludePathPatterns使用先后顺序的区别
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` 的优先级更高。
excludePathPatterns
excludePathPatterns是在配置拦截器时用来排除某些路径不被拦截的方法。在上述引用中提到了两种配置excludePathPatterns的方法。第一种是在拦截器配置类中直接使用excludePathPatterns方法将要排除的路径添加进去,如addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/**/login","/**/register","/download/**")。第二种是在WebMvcConfigurerAdapter的子类中重写addInterceptors方法,在其中使用excludePathPatterns方法进行路径排除的配置,如@Configuration public class InterceptorConfig extends WebMvcConfigurerAdapter { @Autowired private MyInterceptor myInterceptor; @Override public void addInterceptors(InterceptorRegistry registry){ registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/**/login","/**/register","/survey-project/download/**"); } }。这两种方法可以根据具体的需求选择使用。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![tar](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![](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)