如何解决Spring boot拦截静态资源问题

问题描述:

Spring boot拦截静态资源。
浏览器不显示css样式浏览器控制台显示:
Resource interpreted as Stylesheet but transferred with MIME type text/html: “http://localhost:8080/”.错误
使用Thymeleaf时有时会导致不能加载css、js文件

问题展示:

Spring boot拦截静态资源

问题分析:

1版本的Spring boot和2版本的spring boot的静态资源访问是有区别的。

  • 1版本的Spring boot的resources/static目录下的静态资源可以直接访问,并且访问路径上不用带static,当有配置自定义HandlerInterceptor拦截器时,请求静态资源路径不会被拦截。
  • 2版本的Spring boot的如果自定义HandlerInterceptor拦截器时访问静态资源就会被同步拦截

问题解析:

  1. 问题是使用了自定义的拦截器
  2. Spring boot在升级为2。0后抛弃了WebMvcConfigurerAdapter,这里官方文档推荐使用WebMvcConfigurer重写addInterceptors方法达到拦截器的效果

问题解决:

  1. 创建一个类名为MyMvcConfig继承WebMvcConfigurer
  2. 这个类功能是扩展mvc
  3. 我们使用的Springboot本身把静态资源目录设置为"/"所以我们访问静态资源时是不需要加上/static的,因此在放行拦截的使用不能使用/static/**.因此我在/static/下创建目录asserts放入我全部的静态资源,并以此目录放行。
package ink.poesy.spirngboot04.config;

import ink.poesy.spirngboot04.component.LoginHandlerInterceptor;
import ink.poesy.spirngboot04.component.MyLoacleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.*;


/**
 * 扩展mvc功能
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
	//映射
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/main.html").setViewName("dashboard");
    }
    
    /**
     * 起初以为:
     * 在初始化期间根据您可能发送的一个文件进行设置,
     * 而不是根据您实际发送的文件进行设置。
     * 这将导致您可能发送的任何非HTML文件(例如CSS或JS文件)
     * 带有误导性的Content-Type标头。您需要在请求处理程序中进行检查。
     * 实际:
     * Spring boot在升级为2。0后抛弃了WebMvcConfigurerAdapter,
     * 这里官方文档推荐使用WebMvcConfigurer重写,结果面临的问题是
     * 静态资源拦截,为了达到使用静态目录文件的意图,
     * 我选择使用排除拦截的方式放行静态资源的访问。
     * 原本使用"/static/**"->>结果并不好用,总结原因:
     * 我们使用的Springboot本身把静态资源目录设置为"/"所以我们访问静态资源时
     * 是不需要加上/static的,因此在放行拦截的使用不能使用/static/**
     *的,Spring boot找不到。而又不能使用/作为放行路径,
     因此我在/static/下创建目录asserts放入我全部的静态资源,并以此目录放行。
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor())
             	.excludePathPatterns("/","/index","/index.html",
             						"/user/login","/asserts/**",
             						"/webjars/**")
             						//放行静态资源,放行webjars
                .addPathPatterns("/**");//拦截全部
    }
}

静态资源映射,保证资源的正确访问

重写上述方法可以达到拦截器的效果,但是在使用Thymeleaf时有时会导致不能加载css、js文件。
上述创建类中重写
这时我们需要重写映射文件类public void addResourceHandlers(ResourceHandlerRegistry registry)

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
        		.addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**")
        		.addResourceLocations("classpath:/templates/");
    }
发布了67 篇原创文章 · 获赞 22 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_42119415/article/details/100089147
今日推荐