springboot静态资源拦截
时间: 2023-11-26 16:48:27 浏览: 32
在Spring Boot 2.x中,静态资源会被HandlerInterceptor拦截,这是因为Spring Boot 2.x默认使用了WebMvcConfigurerAdapter,而WebMvcConfigurerAdapter会拦截所有的请求,包括静态资源的请求。解决这个问题的方法是,继承WebMvcConfigurationSupport类,并重写addResourceHandlers方法,将静态资源的路径添加到静态资源处理器中。具体实现代码如下所示:
```java
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}
}
```
其中,addResourceHandler方法用于指定静态资源的访问路径,addResourceLocations方法用于指定静态资源的存放路径。上述代码中,静态资源的访问路径为“/static/**”,静态资源的存放路径为“classpath:/static/”。
相关问题
springboot拦截静态资源
Spring Boot 会默认拦截/static、/public、/resources和/META-INF/resources 目录下的静态资源,如果需要自定义静态资源的路径,则需要在application.properties文件中添加以下配置:spring.mvc.static-path-pattern=/customPath/**,其中/customPath/为自定义的路径。
springboot拦截器静态资源
Spring Boot对静态资源的拦截器支持是通过WebMvcConfigurerAdapter类实现的。默认情况下,Spring Boot会自动配置静态资源的处理,并且提供了一组默认的静态资源映射规则。这些规则包括以下路径:
1. classpath:/META-INF/resources
2. classpath:/resources
3. classpath:/static
4. classpath:/public
如果你想自定义静态资源的映射规则,可以继承WebMvcConfigurerAdapter类,并覆盖其中的addResourceHandlers()方法。通过添加自定义的资源处理器和资源映射规则,你可以实现对静态资源的拦截器处理。