1.permitAll配置实例

  1. @EnableWebSecurity
  2. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  3. @Override
  4. public void configure(HttpSecurity http) throws Exception {
  5. http
  6. .authorizeRequests()
  7. .antMatchers("/css/**", "/js/**","/fonts/**").permitAll()
  8. .anyRequest().authenticated();
  9. }
  10. }

2. web ignore配置实例

  1. @EnableWebSecurity
  2. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  3. @Override
  4. public void configure(WebSecurity web) throws Exception {
  5. web.ignoring().antMatchers("/css/**");
  6. web.ignoring().antMatchers("/js/**");
  7. web.ignoring().antMatchers("/fonts/**");
  8. }
  9. }

二者区别

顾名思义,WebSecurity主要是配置跟web资源相关的,比如css、js、images等等,但是这个还不是本质的区别,关键的区别如下:

  • ingore是完全绕过了spring security的所有filter,相当于不走spring security
  • permitall没有绕过spring security,其中包含了登录的以及匿名的。

    小结

  • web ignore比较适合配置前端相关的静态资源,它是完全绕过spring security的所有filter的;

  • 而permitAll,会给没有登录的用户适配一个AnonymousAuthenticationToken,设置到SecurityContextHolder,方便后面的filter可以统一处理authentication。