spring security 表单认证的流程

表单认证过程
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方法进行验证。
如果以上过程没有抛出异常,则为认证成功。 
原文地址:https://www.cnblogs.com/lshan/p/8920788.html