WebMvcConfigurer 放行接口
时间: 2023-11-04 14:54:29 浏览: 39
WebMvcConfigurer是一个接口,它定义了一些方法,用于自定义Spring MVC的配置。通过实现WebMvcConfigurer接口,我们可以自定义一些配置,例如添加拦截器、添加格式化器、添加消息转换器等。在Java 8之前,我们可以继承WebMvcConfigurerAdapter类来实现WebMvcConfigurer接口的方法,但在Java 8之后,接口可以包含默认方法,所以WebMvcConfigurerAdapter类已经被废弃了。
相关问题
WebMvcConfigurer 放行某个接口
要放行某个接口,可以在实现`WebMvcConfigurer`接口的配置类中,重写`addInterceptors`方法,并在该方法中使用`InterceptorRegistry`对象的`excludePathPatterns`方法来添加需要放行的接口路径。
示例代码如下:
```java
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new WebInterceptor())
.excludePathPatterns("/api/xxx") // 需要放行的接口路径
.addPathPatterns("/**"); // 所有请求都需要拦截
}
}
```
Security放行静态资源
在Spring Security中,可以通过创建配置类来放行静态资源。首先,需要创建一个实现了WebMvcConfigurer接口的配置类,并在其中重写addResourceHandlers方法。在该方法中,通过调用registry.addResourceHandler("/**").addResourceLocations("classpath:/static/")来指定静态资源的路径。这样配置后,Spring Security会自动放行这些静态资源,不会对其进行拦截。
相关推荐
![](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)