表单认证过程
Spring security的表单认证过程是由org.springframework.security.web.authentication. UsernamePasswordAuthenticationFilter类中实现。而在spring security3.0之前是在AuthenticationProcessingFilter类中实现的。
在UsernamePasswordAuthenticationFilter中的参数:
public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "j_username";//用户输入的用户名
public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "j_password";//用户输入的密码
public static final String SPRING_SECURITY_LAST_USERNAME_KEY = "SPRING_SECURITY_LAST_USERNAME";
private boolean postOnly = true;//指定是不是由Post提交,而此类是不支持post提交的
当过滤器链执行到UsernamePasswordAuthenticationFilter时会调用其父类AbstractAuthenticationProcessingFilter的doFilter方法。在这个方法中首先执行requiresAuthentication方法,判断此uri是不是j_spring_security_check,如果不是则判断不是认证操作,如果是则取得用户名和密码进行认证。AbstractAuthenticationProcessingFilter会调用UsernamePasswordAuthenticationFilter的attemptAuthentication方法进行验证,如果验证成功则放回一个经过认证的Authentication对象。
进行认证时会取得提供管理ProvicerMnager,并执行其doAuthenticate方法,利用认证提供者类进行认证。
第一个认证提供者类是:
org.springframework.security.authentication.AnonymousAuthenticationProvider,此类不提供认证
第二个认证提供者类是:
org.springframework.security.authentication.dao.DaoAuthenticationProvider
主要的认证操作是在DaoAuthenticationProvider中执行的。首先DaoAuthenticationProvider的父类执行authenticate方法。这个方法需要一个根据用户名获得的Authentication对象,authenticate方法首先在缓冲中判断有没有此用户,如果没有则执行DaoAuthenticationProvider的retrieveUser方法,这个方法通过配置文件中配置的UserDetails执行其loadUserByUsername返回一个UserDetails对象。之后在DaoAuthenticationProvider的additionalAuthenticationChecks方法对返回的UserDetails通过PlaintextPasswordEncoder类中的isPasswordValid方法进行验证。
如果以上过程没有抛出异常,则为认证成功。
Spring security的表单认证过程是由org.springframework.security.web.authentication. UsernamePasswordAuthenticationFilter类中实现。而在spring security3.0之前是在AuthenticationProcessingFilter类中实现的。
在UsernamePasswordAuthenticationFilter中的参数:
public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "j_username";//用户输入的用户名
public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "j_password";//用户输入的密码
public static final String SPRING_SECURITY_LAST_USERNAME_KEY = "SPRING_SECURITY_LAST_USERNAME";
private boolean postOnly = true;//指定是不是由Post提交,而此类是不支持post提交的
当过滤器链执行到UsernamePasswordAuthenticationFilter时会调用其父类AbstractAuthenticationProcessingFilter的doFilter方法。在这个方法中首先执行requiresAuthentication方法,判断此uri是不是j_spring_security_check,如果不是则判断不是认证操作,如果是则取得用户名和密码进行认证。AbstractAuthenticationProcessingFilter会调用UsernamePasswordAuthenticationFilter的attemptAuthentication方法进行验证,如果验证成功则放回一个经过认证的Authentication对象。
进行认证时会取得提供管理ProvicerMnager,并执行其doAuthenticate方法,利用认证提供者类进行认证。
第一个认证提供者类是:
org.springframework.security.authentication.AnonymousAuthenticationProvider,此类不提供认证
第二个认证提供者类是:
org.springframework.security.authentication.dao.DaoAuthenticationProvider
主要的认证操作是在DaoAuthenticationProvider中执行的。首先DaoAuthenticationProvider的父类执行authenticate方法。这个方法需要一个根据用户名获得的Authentication对象,authenticate方法首先在缓冲中判断有没有此用户,如果没有则执行DaoAuthenticationProvider的retrieveUser方法,这个方法通过配置文件中配置的UserDetails执行其loadUserByUsername返回一个UserDetails对象。之后在DaoAuthenticationProvider的additionalAuthenticationChecks方法对返回的UserDetails通过PlaintextPasswordEncoder类中的isPasswordValid方法进行验证。
如果以上过程没有抛出异常,则为认证成功。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?