本身的研究shiro框架的感悟2

根據感悟1過濾器的流程,假設此時jsessionid已經保存在瀏覽器當中了。一路執行會來到FormAuthenticationFilter的onAccessDenied()方法。以下圖:瀏覽器

該onAccessDenied()方法內部有個executeLogin()方法,跟蹤進去。如圖:session

咱們來看看onLoginSuccess()和onLoginFailure()方法,如圖:app

如今稍微打斷一下。咱們來分析各個過濾器的大致做用:ide

AccessControlFilter:控制獲取資源和重定向到登錄頁面在用戶沒有認證的狀況下的那些過濾器的超類。因此須要注意「控制」和「重定向」這兩個詞。大體須要對loginUrl的一些操做,好比saveRequestAndRedirectToLogin()、saveRequest()、redirectToLogin(),就是保存request請求,並重定向到登錄頁面。注意保存request,怎麼保存?見下面。post

咱們來看看saveRequest方法,Convenience method merely delegates to WebUtils.saveRequest(request) to save the request state for reuse later. This is mostly used to retain user request state when a redirect is issued to return the user to their originally requested url/resource.,就是保存當前請求狀態,之後會用到。以下圖:ui

WebUtils.saveRequest()方法:以下圖url

由此咱們知道,saveRequest是把當前的request保存到session當中。若是session爲null的話,就去建立session。spa

AccessControlFilter還有一個關鍵詞就是「控制」:onPreHandle()、isAccessAllowed()、onAccessDenied()。須要特定的操做,子類須要重寫它。code

 

AdviceFilter:A Servlet Filter that enables AOP-style "around" advice for a ServletRequest via preHandle, postHandle, and afterCompletion hooks.環繞通知過濾器,專門處理前置與後置的工做。好比後置工做就是cleanup()方法,前置的具體的工做由子類重寫實現具體的工做。orm

 

 

AuthenticationFilter:Base class for all Filters that require the current user to be authenticated. This class encapsulates the logic of checking whether a user is already authenticated in the system while subclasses are required to perform specific logic for unauthenticated requests.大概的意思是:該類是全部須要當前用戶進行認證的過濾器的基類。該類封裝了驗證當前用戶是否驗證過在本系統的一些處理邏輯。同時要求子類實如今沒有認證過的請求的一些特殊的處理邏輯。

咱們能夠推想一下,在驗證過程當中,若是驗證成功須要幹嗎?須要重定向到successUrl,對吧。若是驗證成功須要幹嗎?固然保存保存當前請求,而後重定向到loginUrl。可是:AuthenticationFilter主要是該用戶驗證成功過的一些處理邏輯。你看,驗證成功,大體須要successUrl的信息和issueSuccessRedirect()方法,這個類也主要就是這些信息。

 

AuthenticatingFilter:前面咱們知道,一個是AuthenticationFilter,一個是AuthenticatingFilter。帶"ing"的AuthenticatingFilter表示執行驗證登錄的具體操做,而父類AuthenticationFilter前面也說過只負責successUrl和重定向successUrl的操做,而該類就是真正的executeLogin()。

 

FormAuthenticationFilter:

Requires the requesting user to be authenticated for the request to continue, and if they are not, forces the user to login via by redirecting them to the loginUrl you configure.

This filter constructs a UsernamePasswordToken with the values found in username, password, and rememberMe request parameters. It then calls Subject.login(usernamePasswordToken) , effectively automatically performing a login attempt. Note that the login attempt will only occur when the isLoginSubmission(request,response) is true, which by default occurs when the request is for the loginUrl and is a POST request.

If the login attempt fails, the resulting AuthenticationException fully qualified class name will be set as a request attribute under the failureKeyAttribute key. This FQCN can be used as an i18n key or lookup mechanism to explain to the user why their login attempt failed (e.g. no account, incorrect password, etc).

If you would prefer to handle the authentication validation and login in your own code, consider using the PassThruAuthenticationFilter instead, which allows requests to the loginUrl to pass through to your application's code directly.

FormAuthenticationFilter顧名思義就是表單認證過濾器,首先就是要一個post方式提交的表單,認證用戶的時候,須要username和password對吧,不然怎麼認證。認證成功則調用超類(AuthenticationFilter)重定向到successUrl(默認successUrl的路徑是」/「),若是認證失敗則繼續執行前往登錄頁面,並給request添加一個認證失敗的參數」shiroLoginFailure「。

相關文章
相關標籤/搜索