@Override public void addInterceptors(InterceptorRegistry registry) { //过滤器 registry.addInterceptor(accessInterceptor) .addPathPatterns("/**") .excludePathPatterns("/api/checkHealth") .excludePathPatterns("/api/user/login"); }检查下代码是否有问题
时间: 2024-04-23 15:21:51 浏览: 18
这段代码看起来是没有问题的,它实现了对所有请求的拦截,除了"/api/checkHealth"和"/api/user/login"这两个请求。这意味着除了这两个请求以外,所有请求都会被accessInterceptor拦截器所拦截。如果您的业务逻辑需要对所有请求进行拦截,那么这段代码是没有问题的。但是,如果您只需要对某些请求进行拦截,那么您需要根据具体情况来设置拦截路径,避免因为拦截造成业务逻辑的异常。
相关问题
@Override public void addInterceptors(InterceptorRegistry registry) { //过滤器 registry.addInterceptor(accessInterceptor) .addPathPatterns("/api/**") .excludePathPatterns("/api/checkHealth") .excludePathPatterns("/api/user/login"); }改成全部拦截
如果您想将所有请求进行拦截,可以将excludePathPatterns方法中的所有路径都删除,也就是不再排除任何路径,如下所示:
```
@Override
public void addInterceptors(InterceptorRegistry registry) {
//过滤器
registry.addInterceptor(accessInterceptor)
.addPathPatterns("/api/**");
}
```
这样,所有以"/api"开头的请求都会被拦截。但是请注意,这样做可能会影响系统的正常运行,因为某些请求可能无法通过拦截器而被拦截。因此,建议在实际使用时,根据具体情况来设置拦截路径。
public class InterceptorConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new JwtInterceptor()) // .addPathPatterns("/admin/**") // .addPathPatterns("/patient/**") // .addPathPatterns("/doctor/**") .addPathPatterns("/666") .excludePathPatterns("/patient/pdf") .excludePathPatterns("/**/login"); } }
您好!根据您提供的代码段,这是一个拦截器配置类。在这个类中,您通过实现`WebMvcConfigurer`接口,并覆盖`addInterceptors`方法来配置拦截器。
在`addInterceptors`方法中,您通过`registry.addInterceptor(new JwtInterceptor())`来添加一个名为`JwtInterceptor`的拦截器。接下来,您使用`addPathPatterns`方法为特定的URL路径添加拦截规则。您注释掉了`.addPathPatterns("/admin/**")`、`.addPathPatterns("/patient/**")`和`.addPathPatterns("/doctor/**")`这些路径的拦截规则,而使用`.addPathPatterns("/666")`添加了一个路径为`/666`的拦截规则。
此外,您使用`.excludePathPatterns`方法来排除一些路径不被拦截。您排除了`/patient/pdf`和`/**/login`这两个路径,即这两个路径不会被`JwtInterceptor`拦截。
这样配置后,当请求的URL路径匹配到拦截规则时,将会触发`JwtInterceptor`的拦截逻辑。其他未匹配到拦截规则的路径将不会被拦截器处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![](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)