spring-security(二)java config加载机制-@EnableGlobalAuthentication

本文深入剖析Spring Security的@EnableGlobalAuthentication注解,讲解如何加载AuthenticationManager及其构建过程。从@EnableGlobalAuthentication引入的AuthenticationConfiguration类开始,详细介绍了AuthenticationManagerBuilder、GlobalAuthenticationConfigurerAdapter的作用,以及WebSecurityConfigurerAdapter、HttpSecurity在配置认证过程中的角色。通过实例解释了UserDetailsService和PasswordEncoder在自定义认证逻辑中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言
在上一篇文章中通过对EnableWebSecurity注解中的配置类WebSecurityConfiguration的探讨,我们知道了filter各种各样的filter以及鉴权用的AccessDecisionManager是怎么加载进来的,但是具体用来认证的AuthenticationManager怎么加载还没有说明,这篇文章,我们就重点分析下各种各样的AuthenticationManager是怎么加载的。
通过上一篇我们知道EnableWebSecurity这个注解除了引入WebSecurityConfiguration这个配置类外,还引入了EnableGlobalAuthentication这个注解,和认证机制相关的秘密就在这个注解中,下面我们重点看看这个注解是怎么做的
环境:
spring-boot version:1.5.4.RELEASE
1..@EnableGlobalAuthentication

@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(value = { java.lang.annotation.ElementType.TYPE })
@Documented
@Import(AuthenticationConfiguration.class)
@Configuration
public @interface EnableGlobalAuthentication {
}

这个注解主要是引入了AuthenticationConfiguration这个配置类
2. AuthenticationConfiguration类

...
@Bean
public AuthenticationManagerBuilder authenticationManagerBuilder(
ObjectPostProcessor<Object> objectPostProcessor) {
return new AuthenticationManagerBuilder(objectPostProcessor);
}

@Bean
public static GlobalAuthenticationConfigurerAdapter enableGlobalAuthenticationAutowiredConfigurer(
ApplicationContext context) {
return new EnableGlobalAuthenticationAutowiredConfigurer(context);
}

@Bean
public static InitializeUserDetailsBeanManagerConfigurer initializeUserDetailsBeanManagerConfigurer(ApplicationContext context) {
return new InitializeUserDetailsBeanManagerConfigurer(context);
}

@Bean
public static InitializeAuthenticationProviderBeanManagerConfigurer initializeAuthenticationProviderBeanManagerConfigurer(ApplicationContext context) {
return new InitializeAuthenticationProviderBeanManagerConfigurer(context);
}

public AuthenticationManager getAuthenticationManager() throws Exception {
if (this.authenticationManagerInitialized) {
return this.authenticationManager;
}
AuthenticationManagerBuilder authBuilder = authenticationManagerBuilder(
this.objectPostProcessor);
if (this.buildingAuthenticationManager.getAndSet(true)) {
ret
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值