java – ‘configure’和’configureGlobal’方法有什么区别?

我正在使用 Spring Security配置并发现,配置内存中身份验证的最常用方法是使用configureGlobal()方法:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
    auth
      .inMemoryAuthentication()
        .withUser("user").password("userPwd").roles("USER");
  }
}

但是有另一种方法,它使用不太广泛,覆盖WebSecurityConfigurerAdapter的configure()方法:

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
      .inMemoryAuthentication(
        .withUser("user").password("userPwd").roles("USER");
  }
}

我只是想知道,它们之间有什么区别,使用configureGlobal()方法的用途是什么呢?

最佳答案 正如 spring security doc所说:

The name of the configureGlobal method is not important. However, it
is important to only configure AuthenticationManagerBuilder in a class
annotated with either @EnableWebSecurity, @EnableGlobalMethodSecurity,
or @EnableGlobalAuthentication. Doing otherwise has unpredictable
results.

点赞