拒绝应用样式,因为其 MIME 类型('application/json')不受支持

Ich*_*aki 7 css spring spring-security

我想使用fullcalendar库,使用gulp和yarn这是生成的链接标签:

但我在控制台中收到此错误:

拒绝应用“ http://localhost/bower_components/fullcalendar/dist/fullcalendar.css ”中的样式,因为其 MIME 类型(“application/json”)不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查。

为什么我会遇到这个问题以及如何解决它?

Reg*_*tos 3

您的应用程序以某种方式使用错误的MIME 类型(“application/json”)提供fullcalendar.css。因此,我的建议是相信错误消息来挖掘问题。

就我而言,我第一次使用 spring-boot尝试ThymeleafWebJars ,并盲目地遵循了我添加的一些教程:

@Configuration
public class WebConfigurer extends WebMvcConfigurerAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/");
  }
}
Run Code Online (Sandbox Code Playgroud)

另外,我的样式在 html 中定义如下:

<link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" />
<link rel="stylesheet" type="text/css" th:href="@{/css/main.css}" />
Run Code Online (Sandbox Code Playgroud)

这样,“bootstrap.min.css”和其他人就会得到同样的错误:“拒绝应用样式,因为它的 MIME 类型('application/json')不受支持”

我仍然没有详细分析这种行为的原因,但是当我意识到 spring-boot 具有 WebJars 的自动配置时,我删除了该注册,一切都开始正常工作。

注意:有一些类似的“MIME type ('text/html')”轨道,原因通常是安全过滤器不允许css文件并重定向到 html 登录页面。因此,请记住这一点并检查您是否对此请求有某种过滤器/重定向。