用源码分析分析Spring Security 认证过程

Spring Security 认证过程

首页集成WebSecurityConfigurerAdapter并使用@EnableWebSecurity和@Configuration

WebSecurityConfigurerAdapter有三个方法分别是configure(WebSecurity web),configure(HttpSecurity http)和configure(AuthenticationManagerBuilder auth)

程序运行起来之后会首页进入到
 是吃擦擦擦吃在这里插入图片描述 从 源码可以看到会先进入到WebSecurityConfigurerAdapter中init()方法,然后进入到getHttp();
在这里插入图片描述
2.getHttp()中会先去获取AuthenticationManager这个类
在这里插入图片描述
3。而authenticationManager()方法会先去执行configure(AuthenticationManagerBuilder auth),由此可以知。程序运行之后最先执行的是configure(AuthenticationManagerBuilder auth),而这个方法主要是做什么用的呢?我们可以去看看上面的源码
在这里插入图片描述4。我们进入到这个bulid()方法里面
在这里插入图片描述
5。然后进入到doBuild()方法中
在这里插入图片描述6.再进入到performBuild()方法,可以看到我们会进入到AuthenticationManagerBuilder中的performBuild();而AuthenticationManagerBuilder正是上面说到configure(AuthenticationManagerBuilder auth)的参数
在这里插入图片描述而performBuild()我们可以看到返回的是ProviderManager类,这个类是AuthenticationManager的实现类,实际认证都在这个类中进行。

自定义Filter完成验证码校验

自定义Filter 要 extends AbstractAuthenticationProcessingFilter,查看AbstractAuthenticationProcessingFilter的源码在这里插入图片描述7.可以看到验证过程是执行了attemptAuthentication(request, response);
在这里插入图片描述8.可以看到这个方法是UsernamePasswordAuthenticationFilter 集成 AbstractAuthenticationProcessingFilter后实现的过程最终会调用AuthenticantionManager.authenticate(authentication);也正是上面说到的实际是ProviderManager.authenticate(authentication)方法去执行验证。我们自定义的话也可以自己去覆盖attemptAuthentication(request, response)方法 并传入AuthenticantionManager,例如:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述10.然后我们去研究研究ProviderManager.authenticate(authentication)里面是什么过程。
在这里插入图片描述11.在这个方法可以看到是AuthenticationProvider 类执行的provider.authenticate(authentication);而AuthenticationProvider 在ProviderManager中是已集合的形式保存的
在这里插入图片描述
12.而ProviderManager的实例化我们在上面已经看到是在 AuthenticationManagerBuilder的performBuild()方法去完成的。

而AuthenticationProvider 是由AuthenticationManagerBuilder的authenticationProvider(AuthenticationProvider authenticationProvider)去提供的,如下图所示
在这里插入图片描述且authenticationProvider(AuthenticationProvider authenticationProvider)是接口形式。
在这里插入图片描述13.而authenticationProvider(authenticationProvider)中的authenticationProvider我们可以自定义authenticationProvider去实现AuthenticationProvider,如下图
在这里插入图片描述最终验证的过程就在(Authentication authentication)中 就是第11步中provider.authenticate(authentication);

以上就是全部认证过程的逻辑。以前看源码之后没有记录,一段时间之后又忘记了,所以这一次记录一下方便以后看看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值