Spring Boot2.0 静态资源被拦截问题

在Spring Boot2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截。但是在spring1.0+的版本中,是不会拦截静态资源的。

因此,在使用Spring Boot2.0+时,配置拦截器之后,我们要把静态资源的路径加入到不拦截的路径之中。

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
        registration.addPathPatterns("/**");                    //所有路径都被拦截
        registration.excludePathPatterns("/","/login","/error","/static/**","/qwe/**");       //添加不拦截路径
    }
}

注意,要实现的接口是WebMvcConfigurer。

不拦截路径的写法是“/static/”。网上其他写法,比如/js/ , /static/js/**, 尝试过都没有效果,可能是因为spring Boot2.0的愿意把

在application.yml中这可以配置静态资源 不过要在上面增加不拦截 才会生效

mvc:
      static-path-pattern: /qwe/**

转载于:https://www.cnblogs.com/rolandlee/p/9577158.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值