浅析Spring Security登录验证流程源码
作者:字母哥博客 时间:2023-03-22 01:46:44
一、登录认证基于过滤器链
Spring Security的登录验证流程核心就是过滤器链。当一个请求到达时按照过滤器链的顺序依次进行处理,通过所有过滤器链的验证,就可以访问API接口了。
SpringSecurity提供了多种登录认证的方式,由多种Filter过滤器来实现,比如:
BasicAuthenticationFilter实现的是HttpBasic模式的登录认证
UsernamePasswordAuthenticationFilter实现用户名密码的登录认证
RememberMeAuthenticationFilter实现登录认证的“记住我”的功能
SmsCodeAuthenticationFilter实现短信验证码登录认证
SocialAuthenticationFilter实现社交媒体方式登录认证的处理
Oauth2AuthenticationProcessingFilter和Oauth2ClientAuthenticationProcessingFilter实现Oauth2的鉴权方式
根据我们不同的需求实现及配置,不同的Filter会被加载到应用中。
二、结合源码讲解登录验证流程
我们就以用户名、密码登录方式为例讲解一下Spring Security的登录认证流程。
2.1 UsernamePasswordAuthenticationFilter
该过滤器封装用户基本信息(用户名、密码),定义登录表单数据接收相关的信息。如:
默认的表单用户名密码input框name是username、password
默认的处理登录请求路径是/login、使用POST方法
2.2 AbstractAuthenticationProcessingFilter的doFilter方法的验证过程
UsernamePasswordAuthenticationFilter继承自抽象类AbstractAuthenticationProcessingFilter,该抽象类定义了验证成功与验证失败的处理方法。
2.3 验证成功之后的Handler和验证失败之后的handler
也就是说当我们需要自定义验证成功或失败的处理方法时,要去实现AuthenticationSuccessHandler或AuthenticationfailureHandler接口
三、登录验证内部细节
3.1多种认证方式的管理 ProviderManager
ProviderManager用继承于AuthenticationManager是登录验证的核心类。ProviderManager保管了多个AuthenticationProvider,用于不同类型的登录验证。比如:
RememberMeAuthenticationProvider定义了“记住我”功能的登录验证逻辑
DaoAuthenticationProvider加载数据库用户信息,进行用户密码的登录验证
public class ProviderManager implements AuthenticationManager, MessageSourceAware, InitializingBean {
……
private List<AuthenticationProvider> providers;
……
下文是ProviderManager的核心源码,遍历不同登录验证的AuthenticationProvider,只有当这种方式被支持的时候,才执行具体的登录验证逻辑。
3.2 登录认证接口 AuthenticationProvider
public interface AuthenticationProvider {
Authentication authenticate(Authentication var1) throws AuthenticationException;
boolean supports(Class<?> var1);
}AuthenticationProvider的实现类定义了具体的登录验证逻辑
3.3 数据库加载用户信息 DaoAuthenticationProvider
public class DaoAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
从数据库获取用户信息源码
所以当我们需要加载用户信息进行登录验证的时候,我们需要实现UserDetailsService接口,重写loadUserByUsername方法,参数是用户输入的用户名。返回值是UserDetails。
总结
以上所述是小编给大家介绍的Spring Security登录验证流程源码网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://www.cnblogs.com/zimug/archive/2019/11/18/11880050.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java抽象类和抽象方法定义与用法实例详解
C# WPF数据绑定模板化操作的完整步骤
Jmeter接口登录获取参数token报错问题解决方案
![](https://img.aspxhome.com/file/2023/7/113247_0s.png)
C# SQLite库使用技巧
![](https://img.aspxhome.com/file/2023/2/75602_0s.jpg)
Java中ArrayList与顺序表的概念与使用实例
![](https://img.aspxhome.com/file/2023/2/73092_0s.png)
如何使用C#在PDF文件添加图片印章
![](https://img.aspxhome.com/file/2023/3/108663_0s.png)
Java面试必备八股文整理
![](https://img.aspxhome.com/file/2023/8/59068_0s.jpg)
C#数据结构之最小堆的实现方法
![](https://img.aspxhome.com/file/2023/7/102147_0s.jpg)
Java实现选择排序
![](https://img.aspxhome.com/file/2023/2/84602_0s.jpg)
java基于netty NIO的简单聊天室的实现
![](https://img.aspxhome.com/file/2023/7/69697_0s.png)
Android中加入名片扫描功能实例代码
![](https://img.aspxhome.com/file/2023/2/86002_0s.png)
IOS开发向右滑动返回前一个页面功能(demo)
![](https://img.aspxhome.com/file/2023/5/96015_0s.gif)
Android RecyclerBarChart绘制使用教程
![](https://img.aspxhome.com/file/2023/1/83521_0s.png)
自定义视图view使用Canvas实现手写板和涂鸦功能
![](https://img.aspxhome.com/file/2023/4/106014_0s.gif)
Java流程控制语句之If选择结构
![](https://img.aspxhome.com/file/2023/3/58853_0s.png)
C#最简单的关闭子窗体更新父窗体的实现方法
解决JAVA遍历List集合,删除数据时出现的问题
Java设计模式之桥接模式
Android按钮按下的时候改变颜色实现方法
![](https://img.aspxhome.com/file/2023/1/137991_0s.jpg)
maven私服搭建与使用教程
![](https://img.aspxhome.com/file/2023/7/75667_0s.jpg)