WebSecurityConfigurerAdapter
GlobalAuthenticationConfigurerAdapter
对我来说,似乎两者都提供了不同的方法 configure(...)
来自定义WebSecurity,例如配置 UserDetailsService
。在互联网上找到的一些示例中,我看到这两个类都被扩展了(例如此示例:http://ryanjbaxter.com/2015/01/06/securing-rest-apis-with-spring-boot/):
@Configuration
class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {...}
并且
@EnableWebSecurity
@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {...}
但在某些情况下,只需要扩展WebSecurityConfigurerAdapter
。
我不确定两者之间的区别是什么?它们各自能做什么?或者如果两者都需要,那么它们分别用于Spring安全的哪个方面?
我唯一看到的区别是@EnableWebSecurity
通常注释在扩展WebSecurityConfigurerAdapter
的类上,而不是在扩展GlobalAuthenticationConfigurerAdapter
的类上。
=============实验==================
我尝试删除扩展GlobalAuthenticationConfigurerAdapter
的类,并将与UserDetailsService相关的代码移到扩展WebSecurityConfigurerAdapter
的类中(请参阅上面的链接以查看实际代码),它仍然可以正常工作。
configure(AuthenticationManagerBuilder auth)
的方法,用于执行此操作。 - SimoGlobalAuthenticationConfigurerAdapter
来配置全局配置,然后我使用了WebSecurityConfigurerAdapter
来覆盖那些具有不同配置的特定路由的配置。 - Madhatter