Refused to execute script from '....js' because its MIME type ('text/html') is not executable...

本文详细解析了在SpringBoot项目中,由于SpringSecurity配置不当导致的静态资源访问失败问题。通过调整HttpSecurity配置,明确指定/js/**等静态资源路径为公开访问,确保未认证用户也能正常加载页面资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述
在整合 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.
 

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

@Override
	protected void configure(HttpSecurity http) throws Exception{
		http.authorizeRequests()
			.antMatchers("/css/**", "/js/**","/images/**", "/webjars/**", "**/favicon.ico", "/index")
			.permitAll()
			.anyRequest().authenticated()
			.and()
			.formLogin()
			.loginPage("/login")
			.failureUrl("/login?error")
			.permitAll()
			.and()
			.logout().permitAll();
	}

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

装载自:https://blog.youkuaiyun.com/c4jem/article/details/77131422

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值