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方法進行驗證。 若是以上過程沒有拋出異常,則爲認證成功。 
相關文章
相關標籤/搜索