在使用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 }

调用如下:

Spring Security静态资源访问

 

相关文章:

  • 2022-12-23
  • 2021-12-01
  • 2021-09-20
  • 2022-02-06
  • 2021-11-20
  • 2021-11-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2021-11-02
  • 2021-08-13
  • 2021-12-30
  • 2022-12-23
相关资源
相似解决方案