Spring security:WebSecurityConfigurerAdapter 和 GlobalAuthenticationConfigurerAdapter 的区别

Sim*_*imo 6 java authentication spring spring-security spring-boot

这两个类:

WebSecurityConfigurerAdapter 
GlobalAuthenticationConfigurerAdapter 
Run Code Online (Sandbox Code Playgroud)

似乎对我做同样的事情。它们都提供了不同的方法configure(...)来自定义 WebSecurity,例如配置UserDetailsService. 在互联网上找到的一些示例中,我看到两个类都被扩展了(就像这个,http : //ryanjbaxter.com/2015/01/06/securing-rest-apis-with-spring-boot/):

@Configuration
class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {...}
Run Code Online (Sandbox Code Playgroud)

@EnableWebSecurity
@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {...}
Run Code Online (Sandbox Code Playgroud)

但在某些示例中,只WebSecurityConfigurerAdapter需要(扩展)。

我不确定两者之间的区别?哪一个能做而另一个不能做?或者如果它们都需要,那么它们中的哪一个用于 Spring 安全的哪个方面?

我看到的唯一区别是,@EnableWebSecurity它通常在 extends 类上方注释WebSecurityConfigurerAdapter,而不是在 extends 类中GlobalAuthenticationConfigurerAdapter

==============实验==================

我尝试删除 extends 的类GlobalAuthenticationConfigurerAdapter,并将与 UserDetailsS​​ervice 相关的代码携带到扩展的类中WebSecurityConfigurerAdapter(实际代码请参见上面的链接),它仍然有效。

小智 0

基本上WebSecurityConfigurerAdapter用于创建FilterChainProxy 对本文档的引用,因为它可GlobalAuthenticationConfigurerAdapter用于SecurityConfigurer轻松构建内存身份验证、LDAP 身份验证、基于 JDBC 的身份验证、添加 UserDetailsS​​ervice 和添加 AuthenticationProvider。请参阅此文档希望这有帮助!