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);
}
}