问题描述
使用spring Security实现后台管理系统登录验证加拦截,访问图片即静态资源时响应需要登录验证,分析问题得出结论未配置security的拦截规则,没有对静态资源进行登录放行
解决方案:
配置spring Security拦截规则,将访问路径添加进入白名单中,比如博主将resources下files下的图片设置白名单,规则就是"/files/**",以下就是博主的配置
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/files/**").permitAll()// 配置拦截规则
.anyRequest().authenticated();
}
}
有关spring security的配置器相关规则
http.authorizeRequests().antMatchers("/api/**").denyAll(); //拒绝api路径下所有的访问
http.authorizeRequests().antMatchers("/api/**").authenticated(); //api路径下访问需认证通过
http.authorizeRequests().antMatchers("/api/**").permitAll(); //api路径下无条件允许访问