欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

使用SpringBoot框架和SpringSecurity框架整合出现because its MIME type ('text/html') is not executable

发布时间:2025/5/22 73 豆豆
生活随笔 收集整理的这篇文章主要介绍了 使用SpringBoot框架和SpringSecurity框架整合出现because its MIME type ('text/html') is not executable 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

前端页面出现:

Refused to execute script from ‘http://localhost:8080/login’ because its MIME type (‘text/html’) is not executable, and strict MIME type checking is enabled.
解决方法:

  • 将静态资源的路径写在自定义的安全策略中

    所以要找到对自定义的springSecurity安全框架的配置类进行修改

    自定义的springSecurity安全框架的配置类,继承WebSecurityConfigurerAdapter,重写其中的方法configure
  • 我们可以在authorizeRequests() 后定义多个antMatchers()配置器来控制不同的url接受不同权限的用户访问。
    antMatchers在这里配置哪些页面或资源不需要认证:

  • 如果此方法还会出现
    Refused to execute script from ‘http://localhost:8080/login’ because its MIME type (‘text/html’) is not executable, and strict MIME type checking is enabled.
    可对页面中的引用路径进行修改,有可能是页面中的资源路径没有引用到static下的静态资源。
    例如:

    在这个相对路径中,我的images文件夹在default下

    由于前面少加一个文件夹的路径,就会导致Spring Security框架拦截了该资源的请求,
    所以在前面加上default

    问题就解决了
  • 注意:该报错不会具体指出是哪一行出现错误,所以要细心检查前端引用资源的代码,也有可能是图片,js, css, 字体文件,音频,视频没有引用到static下的静态资源。

  • 关于spring boot资源的存放

    static 目录存放我们的web静态资源页面
    templates 存放我们的html, jsp, ftl等各种格式网站页面
  • 总结

    以上是生活随笔为你收集整理的使用SpringBoot框架和SpringSecurity框架整合出现because its MIME type ('text/html') is not executable的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。