我们前面实现了使用自定义认证界面的功能,但是后台认证校验还是使用的’/login’来处理的,对比的账号密码还是我们写在内存的数据,那我们如果想要实现和数据库中的数据比较,那么我们就必须要实现自定义认证逻辑的实现,本文我们就先来分析下系统自带的认证是怎么走的。
1.UsernamePasswordAuthenticationFilter
系统认证是通过UsernamePasswordAuthenticationFilter
过滤器实现的,所以我们需要来分析下这个过滤器的源码
1.1.表单提交参数
1.2.doFilter方法
因为UsernamePasswordAuthenticationFilter
就是一个过滤器,所以我们要分析他的原理,肯定需要通过doFilter方法来开始,注意该方法在父类中。
通过上面我们发现,要查看认证的流程我们需要进入attemptAuthentication
方法中查看
2.认证的过程
![在这里插入图片描述](https://img-blog.csdnimg.cn/ea9dae011afd41a6bcd38be172fc39f4.png
进入this.getAuthenticationManager().authenticate(authRequest)
;中
由上面源码得知,真正认证操作在AuthenticationManager
里面!然后看AuthenticationManager
的实现类ProviderManager
:
循环所有AuthenticationProvider,匹配当前认证类型。找到了对应认证类型就继续调用AuthenticationProvider对象完成认证业务。
我们发现最终去做认证的是 UserDetailsService
接口的实现去完成的,那么我们要自定义认证过程,那么也只需要实现该接口接口,下篇我们具体看看如何实现。