Spring Security 对于静态资源的拦截与放行

初始创建Spring Boot项目,使用thymeleaf作为模板引擎,利用Spring Security进行验证管理,根据官方例子试验成功(官方的Spring Security示例)。

然后准备整合页面直接将html甩到templates目录下,静态资源甩到static目录下。
简单的测试页面,发现会报错如下:

Refused to apply style from 'http://localhost:8080/login' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

刚开始以为是模板引擎的语法写错了,后来一理思路,原来直接引入的时候就是好的,那就应该是Spring Security给我把资源拦截了下来。现在要做的就是放行啦。

WebSecurityConfig配置类中添加如下放行规则:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    // ...
    @Override
    public void configure(WebSecurity web) throws Exception {
        //解决静态资源被拦截的问题
        web.ignoring().antMatchers("/css/**","/vendors/**");
    }
}

至此,资源被正确引入啦。

    原文作者:AidenEcho
    原文地址: https://segmentfault.com/a/1190000020233313
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞