在使用Spring Security时要求所有请求都需要授权访问,此时会定义过滤规则如下
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
// 指定登录页面
.loginPage("/login")
// 允许所有用户可以访问指定的登录页面
.permitAll();
}
此时访问页面静态资源会由于授权失败进入到登录页面。
Spring Security已定义了默认的不需要授权访问的路径,此时我们可以将静态资源放到这些路径下面就可以正常访问到,相关定义代码如下:
public class SpringBootWebSecurityConfiguration {
private static List<String> DEFAULT_IGNORED = Arrays.asList("/css/**", "/js/**", "/images/**", "/webjars/**", "/**/favicon.ico"); ......
}
调用如下: