@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html", "/", "/login","/reg", "/code", "/css/*", "/js/*", "/img/*"); }
时间: 2023-06-26 12:06:03 浏览: 55
这是一个在 Spring Boot 中注册拦截器的方法。在这个方法中,我们创建了一个 `LoginHandlerInterceptor` 的实例,并将其添加到 `InterceptorRegistry` 中。我们还指定了哪些 URL 不需要被拦截器拦截,这些 URL 包括:`/index.html`、`/`、`/login`、`/reg`、`/code`、`/css/*`、`/js/*` 和 `/img/*`。
当请求被发送到控制器(Controller)之前,拦截器会先进行处理。在这个示例中,`LoginHandlerInterceptor` 拦截所有请求,并在请求被处理之前进行一些操作,比如验证用户是否已经登录等。如果用户未登录,则可以将请求重定向到登录页面,或者直接返回错误信息。
相关问题
@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`的拦截逻辑。其他未匹配到拦截规则的路径将不会被拦截器处理。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)