Spring Security静态资源访问

在使用Spring Security时要求所有请求都需要授权访问,此时会定义过滤规则如下

 1 protected void configure(HttpSecurity http) throws Exception {
 2     http
 3         .authorizeRequests()
 4             .anyRequest().authenticated()
 5             .and()
 6         .formLogin()
 7             // 指定登录页面
 8             .loginPage("/login")
 9             // 允许所有用户可以访问指定的登录页面
10             .permitAll();
11 }
12        

此时访问页面静态资源会由于授权失败进入到登录页面。

Spring Security已定义了默认的不需要授权访问的路径,此时我们可以将静态资源放到这些路径下面就可以正常访问到,相关定义代码如下:

1 public class SpringBootWebSecurityConfiguration {
2     private static List<String> DEFAULT_IGNORED = Arrays.asList("/css/**", "/js/**", "/images/**", "/webjars/**", "/**/favicon.ico");
3 
4     ......
5 }

调用如下:

 

posted @ 2018-03-20 10:33  雪域熊猫  阅读(2021)  评论(0编辑  收藏  举报