spring security-疑难杂症

Spring Security 静态资源访问

在搞 Spring Security 的时候遇到了一个小坑,就是静态资源加载的问题。

当我们继承了 WebSecurityConfigurerAdapter的时候,会去重写几个方法。去设定我们自己要过滤的路径或者是权限的一些规则。

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) //开启security注解
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/assets/**","/").permitAll();
            /*上面的不用拦截*/
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        /*解决静态资源被拦截*/
        web.ignoring().antMatchers("/assets/**");

    }


}
@Component
@Slf4j
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // TODO Auto-generated method stub
        registry.addResourceHandler("/static/**").addResourceLocations     ("classpath:/static/");

        super.addResourceHandlers(registry);
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_42413153/article/details/84949520
今日推荐