Shiro登錄機制驗證,自定義FormAuthenticationFilter

自定義登錄form攔截器:org.apache.shiro.web.filter.authc.FormAuthenticationFilter


問題描述

使用shiro進行系統身份驗證-權限控制,登錄界面進行登錄操作何時觸發

boolean org.apache.shiro.web.filter.authc.AuthenticatingFilter.executeLogin(ServletRequest request, ServletResponse response) throws Exception方法?

問題分析

探知login.jsp的form與shiro form filter的識別認證規則需要查看org.apache.shiro.web.filter.authc.FormAuthenticationFilter中的源碼。

第一、輸入控件name命名:


在此可知,登錄表單的用戶名、密碼等輸入控件的name默認值。

第二、表單action與login.jsp的訪問地址規則:

shiro過濾器的配置:



boolean org.apache.shiro.web.filter.authc.FormAuthenticationFilter.onAccessDenied(ServletRequest request, ServletResponse response) throws Exception


文字選中方法:boolean org.apache.shiro.web.filter.authc.AuthenticatingFilter.executeLogin(ServletRequest request, ServletResponse response) throws Exception  該方法在

org.apache.shiro.web.filter.authc.AuthenticatingFilter中有具體實現:

當登錄表單需要擴展時,需要在擴展類裏重新實現此方法,比如,登錄時增加驗證碼,實現如下:

本文重點探究該方法何時被正確出發?

1. 判斷當前URL是否爲登錄地址:

shiroFilter的loginUrl值。

boolean org.apache.shiro.web.filter.AccessControlFilter.isLoginRequest(ServletRequest request, ServletResponse response);

2. 如果當前請求URL爲登錄地址時,判斷是否爲form提交。
boolean org.apache.shiro.web.filter.authc.FormAuthenticationFilter.isLoginSubmission(ServletRequest request, ServletResponse response);

在此兩點可以總結出登錄表單及登錄界面URL的規則,即:
get loginUrl:進入登錄界面
post loginUrl:提交登錄表單
username:表單用戶名
password:表單密碼

探究結論

           shiroFilter配置的loginUrl的GET請求進入登錄界面,POST請求爲提交登錄表單。觸發
boolean org.apache.shiro.web.filter.authc.AuthenticatingFilter.executeLogin(ServletRequest request, ServletResponse response) throws Exception方法。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章