问题描述

在整合 Spring Boot、Spring Security、Thymeleaf 的练习中,对页面进行调试时,发现如下错误提示:

Refused to execute script from 'http://localhost:8080/codelib-springsecurity-sample-web/js/ie10-viewport-bug-workaround.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

解决过程

1、 首先看到 “because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled”,因为 Thymeleaf 对于页面的元素必须是严格的格式,所以我以为是因为我页面代码上没有加 type="text/javascript" 的原因。

<script th:src="@{js/ie-emulation-modes-warning.js}" src="../static/js/ie-emulation-modes-warning.js" type="text/javascript"></script>

结果加上了并不能解决问题。

2、 再看 “Refused to execute script ...”,为什么会被拒绝执行呢?进而想到可能是权限的控制问题,亦即是 Spring Security 的静态资源访问配置问题。经核查,的确是这样的问题。

正确配置如下:

@Override
protected void configure(HttpSecurity http) throws Exception {
// http.authorizeRequests()每个匹配器按照它们被声明的顺序被考虑。
http
.authorizeRequests()
// 所有用户均可访问的资源
.antMatchers("/css/**", "/js/**","/images/**", "/webjars/**", "**/favicon.ico", "/index").permitAll()
// ROLE_USER的权限才能访问的资源
.antMatchers("/user/**").hasRole("USER")
// 任何尚未匹配的URL只需要验证用户即可访问
.anyRequest().authenticated()
.and()
.formLogin()
// 指定登录页面,授予所有用户访问登录页面
.loginPage("/login")
.permitAll()
.and()
.headers()
.frameOptions().sameOrigin();
}

问题在于我原来配置中没有将 "/js/**" 的路径添加到配置中,导致没有验证的用户没有权限访问。

总结

项目结构如下,假如我要能否访问 plugins 文件夹下的文件,也需要将 “/plugins/**” 添加到相应到上述的配置中,允许所有请求访问。

注:对于Spring Boot 整合 Thymeleaf,静态资源默认存放在 static 文件夹下,在页面上写路径上不需要加上 static 路径,而 html 页面则放在 templates 文件夹下。

所以写法如下:

<script th:src="@{js/ie-emulation-modes-warning.js}" src="../static/js/ie-emulation-modes-warning.js" type="text/javascript"></script>

Refused to execute script from '....js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.md的更多相关文章

  1. 对于错误“Refused to execute script from '...' because its MIME type ('') is not executable, and strict MIME type checking is enabled.”的处理。

    今天在是用公司的报表插件Stimulsoft时发现的问题.之前可以正常使用,突然不能加载了.查看发现得到这个错误. 查看请求头 可以看到,请求正常响应,但是发现 Content-Type是空的,但是引 ...

  2. Refused to execute script from '...' because its MIME type ('') is not executable, and strict MIME type checking is enabled.

    写在前面 部署项目到weblogic上启动首页访问空白, 浏览器控制台报如题错误. web.xml中把响应头添加防止攻击的报文过滤器禁用就行了(仅仅是为了启动), 以下为转载内容, 可以根据需要自行测 ...

  3. Refused to execute inline event handler because it violates the following Content Security Policy directive: "xxx". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...')

    /********************************************************************************* * Refused to exec ...

  4. [Matlab] Attempt to execute SCRIPT *** as a function

    Attempt to execute SCRIPT *** as a function 问题: 在运行MATLAB程序的时候,出现如题的报错. 原因: 在系统中,现有的.m文件有的与***函数重名,所 ...

  5. <!--[if IE]><script type="text/javascript" src="matrix/js/html5.js"></script><![endif]-->代码解释

    块注释例子 1. <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]-->2. <!--[if IE]> 所有的I ...

  6. matlab: Attempt to execute SCRIPT *** as a function 错误

    编写matlab程序时,出现了“Attempt to execute SCRIPT mean as a function”,其实这是“Attempt to execute SCRIPT *** as ...

  7. <script type="text/template">是干什么的,为什么要把html写在js中? 这是什么编程语言风格,都能这样用吗?

    这一段存放了一个模板.在js里面,经常需要使用js往页面中插入html内容.比如这样: var number = 123; $('#d').append('<div class="t& ...

  8. 使用【单独】的一个<script>进行js文件的引用

    刚才用jQuery的时候,总是发现js代码不被执行...后来发现我的代码是这么写的: <script type="text/javascript" src="htt ...

  9. 【解决】MacOS下 Python3.7 使用 pyinstaller 打包后执行报错 Failed to execute script pyi_rth__tkinter

    Fix tcl/tk libs inclusion in tkinter with Python3.7 under MacOS 使用 Pyinstaller 打包时候报错 3027 ERROR: Tc ...

随机推荐

  1. python selenium--常用函数3

    ActionChains类鼠标操作的常用方法: 引入ActionChains类:from selenium.webdriver.common.action_chains import ActionCh ...

  2. 5V转3.3v电路

    方案一: MIC5205-3.3 输出电流150ma 输出电压3.3V 其中:CT24为钽电容: 方案二: AMS1117-3.3 输出电流800ma 输出电压:3.3V 输入电压:4.75~12v

  3. pandas drop_duplicates

    函数 : DataFrame.drop_duplicates(subset=None, keep='first', inplace=False) 参数:这个drop_duplicate方法是对Data ...

  4. FreeSWITCH小结:关于sip的UDP、TCP与MTU

    1.关于SIP的UDP与MTU的关系 如果sip消息的大小超过了MTU,则有可能被网络中的某一节点分片,而UDP处理分片会有很大的问题,从而导致sip消息传输失败.要解决该问题的话,两种方案: 1)减 ...

  5. 双向链表LinkedList使用

    LinkedList是传统意义上的链表也就是双向链表.每个元素都是节点,都可以指向下一级 在前添加,在后添加: mSource.AddLast(...) mSource.AddFirst(...) 在 ...

  6. Atitit. Class  元数据的反射操作 api apache  工具

    Atitit. Class  元数据的反射操作 api apache  工具 1 BeanUtils & PropertyUtils & MethodUtils类使用方法 - 短裤党  ...

  7. 斑马Zebra ZPLII指令集中文说明解释

      我们最常用的斑马(Zebra)条码打印机,应用ZPLII命令来控制打印,说明书中有每条指令的详细说明及相关示例,下面是各指令的中文释义: ^A 对Zebra内置点阵字体缩放 ^A(可缩放/点阵字体 ...

  8. iOS9 3DTouch、ShortcutItem、Peek And Pop技术一览

    [iOS]iOS9 3DTouch.ShortcutItem.Peek And Pop技术一览   3DTouch UITouch类里API的变化 iOS9中添加的属性 altitudeAngle 当 ...

  9. (转)Python开发规范

    转自:https://www.jianshu.com/p/d414e90dc953 Python风格规范 本项目包含了部分Google风格规范和PEP8规范,仅用作内部培训学习 Python风格规范 ...

  10. CSS学习笔记(3)--表格边框

    http://www.alixixi.com/web/a/2009082657736.shtml 对于很多初学HTML的人来说,表格<table>是最常用的标签了,但对于表格边框的控制,很 ...