spring security中前端访问Refused to execute script from 'http://localhost:8080/' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.


springboot集成spring Security时前端出现Refused to execute script from 'http://localhost:8080/' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.问题

最近学习springSecurity,遇到很多坑,在此记录一下,借此可以帮助更多的人

1. 出现情况

2. 原因分析

第一次是我以为是路径问题,可是查看后发现路径并未写错,springboot默认静待资源映射是static目录下,但是之前可以正常访问,于是考虑是spring security问题,于是查看spring Security配置类添加忽略拦截文件

  @Override
  public void configure(WebSecurity web) throws Exception {
    // 设置拦截忽略文件夹,可以对静态资源放行
      web.ignoring().antMatchers("/js/**", "/css/**","/images/**","/lib/**");
  }

表示忽略拦截static目录下指定路径静态文件,注意这里千万不要使用"/**"。

3. 测试结果


发现控制台已经不再报错,问题解决

4. 总结

如果使用"/**"如下面示例代码:

 @Override
    public void configure(WebSecurity web) throws Exception {
        // 设置拦截忽略文件夹,可以对静态资源放行
        web.ignoring().antMatchers("/**");
    }

这样会有大问题。因为WebSecurity是全局配置,如果配置"/**"会导致权限访问问题这个看我后续博客会说明。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM