springboot静态资源加载问题:能加载到文件,但是文件没有内容。拦截器的问题。

在使用springboot+thymeleaf的时候发现了这样的情况:加载到的js和css文件都没有内容。

但是在项目中是正常的文件。
尝试配置了许多东西之后发现是拦截器的问题。
1、在实现了WebMvcConfigurer接口的配置类中先重写addResourceHandlers方法。

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/resources/")
            .addResourceLocations("classpath:/static/")
            .addResourceLocations("classpath:/templates/");
    WebMvcConfigurer.super.addResourceHandlers(registry);
}

2、在addInterceptors方法中配置excludePathPatterns

registry.addInterceptor(authenticationInterceptor)
        .addPathPatterns("/**")
        .excludePathPatterns("/login/**","/**/*.js","/**/*.css","/favicon.ico","/bootstrap/**");//主要是这里,直接从static下一级目录开始写。

最后,可以在InterceptorpreHandler方法中LOGGER.info(request.getRequestURI());打印访问路径来查看是否被排除了拦截~

posted on 2023-10-04 12:35  枫沰  阅读(62)  评论(0编辑  收藏  举报

导航