AuthenticationManagerBuilder和HttpSecurity.authenticationProvider之间的区别

时间:2017-03-28 13:03:05

标签: spring authentication spring-boot spring-security

要在spring boot中提供自定义身份验证提供程序,是否需要以下两项?他们的区别是什么?

AuthenticationManagerBuilder

HttpSecurity.authenticationProvider(...)

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

  @Autowired
  private MyAuthenticationProvider myAuthenticationProvider;

  @Autowired
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.authenticationProvider(myAuthenticationProvider);
  }

  // --------------- OR/AND ? ----------------
  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http
      .authenticationProvider(myAuthenticationProvider)
      // ...
  }
}

1 个答案:

答案 0 :(得分:2)

您只需使用两个选项中的一个。在内部,http.authenticationProvider只是调用AuthenticationManagerBuilder.authenticationProvider。