Refused to execute script from ‘http://localhost:8080/login.html‘

Refused to execute script from ‘http://localhost:8080/login.html’

最近学习SpringSecurity,在添加了SpringSecurity依赖后导致原先的网站图片、js代码都显示不出来了,浏览器报错,代码如下

报错代码

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

报错原因

原因,将静态页面、JS、img等资源都放在了resource的static文件夹下,导致security默认拦截了这些请求,从而访问页面时组件的事件不起作用,控制台报错如下

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uLRPng8d-1685589971193)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20230601111031138.png)]

解决方法

方法一

在配置类中添加忽略静态资源,将静态资源放行

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启方法授权的检测
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
        //忽略静态路径
        web.ignoring().antMatchers("/css/**","/js/**","/imgs/**");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //忽略静态路径
        web.ignoring().antMatchers("/css/**","/js/**","/imgs/**");
    }
}

方法二

可能SecurityConfig配置类设置了白名单,可以取消设置,也可以设置为黑名单

  //设置黑名单(需要登录才能访问资源)  顺序为:    authorizeHttpRequests -> mvcMatchers -> authenticated -> anyRequest -> permitAll
        String[] urls = {"/admin.html"};
        http.authorizeHttpRequests()//对请求进行授权
                .mvcMatchers(urls)//匹配某些路径
                .authenticated()//要求通过认证的
                .anyRequest() //任意请求
                .permitAll(); //直接许可,即不需要认证即可访问

        //设置白名单  顺序为:  authorizeHttpRequests -> mvcMatchers -> permitAll -> anyRequest -> authenticated
        /*String[] urls = {"/index.html","/","/reg.html", "/login.html", "/v1/banners/", "/v1/users/reg","/v1/users/login"};
        http.authorizeHttpRequests()//对请求进行授权
                .mvcMatchers(urls)//匹配某些路径
                .permitAll() //直接许可,即不需要认证即可访问
                .anyRequest() //任意请求
                .authenticated();//要求通过认证的*/
    }

成功解决

改为之后,问题解决,事件也起了作用,img、js都可以了

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xjBRcpqs-1685589971200)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20230601111619026.png)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天你学Java了吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值