springboot使用拦截器时,屏蔽了静态资源。

本文详细介绍了在使用SpringBoot 1.5.7.RELEASE版本时,如何正确配置拦截器以避免加载静态资源的问题。通过对比不同配置方式,指出使用WebMvcConfigurerAdapter而非WebMvcConfigurationSupport的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在网上看到不是介绍但是,我尝试的时候,一直无法加载静态资源

后来我发现我使用的是springboot  1.5.7.RELEASE版本 ,应该这样

@Configuration
public class AppConfigurer extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
   
        registry.addInterceptor(new HttpInterceptor()).
                addPathPatterns("/**").excludePathPatterns("/users/device/**","/users/user/**","/static/**");
      
        super.addInterceptors(registry);
    }
}

 

但是我在错误的使用extends WebMvcConfigurationSupport  的方式,虽然没有报错,而且也成功实现对非静态资源的拦截和不拦截,但是用这个方式即使excludePathPatterns了"/static/**",也还是屏蔽了静态资源,但是在springboot2.x这样用没有问题,所以如果用的是1.5.7版本,拦截器就用上面的方法。

@Configuration
public class AppConfigurer implements WebMvcConfiguter{
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        
        registry.addInterceptor(new Interceptor()).addPathPatterns("/users/device/*").excludePathPatterns("/static/**");
        registry.addInterceptor(new Interceptor()).addPathPatterns("/users/user/*").excludePathPatterns("/static/**");
        registry.addInterceptor(new HttpInterceptor()).
                addPathPatterns("/**").excludePathPatterns("/users/device/**","/users/user/**","/static/**");
       
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值