shiro 認(rèn)證流程圖:
shiro認(rèn)證流程圖.png
shiro 授權(quán)流程圖:
shiro-授權(quán)校驗.png
shiro 認(rèn)證時序圖:
shiro 認(rèn)證時序圖 (1).png
shiro + cas 認(rèn)證時序圖:
shiro+cas 認(rèn)證時序圖 (2).png
- 首先訪問http://localhost:8083/ ,會被UserFilter 攔截,因為我們在配置中配置了filterChainDefinitionMap.put("/**", "user");
- UserFilter 的邏輯就是巧还,是否被認(rèn)證過唯蝶,沒有認(rèn)證過重定向到登錄頁患朱;這里我們配置的是Cas認(rèn)證中心
- Cas 認(rèn)證中心認(rèn)證過后寥裂,會重定向到我們的回調(diào)地址:http://127.0.0.1:8083/node3/cas, 這個在application.yml 有配置萤厅;
- http://127.0.0.1:8083/node3/cas 會被CasFilter 攔截阎毅,因為也是在配置中配置了; filterChainDefinitionMap.put("/node3/cas", "cas");
- CasFilter的邏輯就是獲取Subject屈梁,并調(diào)用subject.login(token) 來認(rèn)證Token;
- Subject 自己不會認(rèn)證token嗤练,而是轉(zhuǎn)交給SecutityManger 來驗證
- SecutityManger 通過securityManager.login(this, token) 來驗證token,但是它也沒自己認(rèn)證在讶,而是轉(zhuǎn)手給了Authenticator來做token的驗證煞抬;即:Authenticator才是token的真正驗證者
- Authenticator 接口通過authenticate(token)方法來驗證token;這里要詳細(xì)講解一下:
1)ModularRealmAuthenticator 為Authenticator 的實現(xiàn)類构哺,也是負(fù)責(zé)驗證token的核心類
2)ModularRealmAuthenticator 對象里面有一個Collection<Realm> realms 屬性革答,看到這個是不是有點熟悉的味道,對曙强!沒錯残拐,我們自定義的CasRealm就在這里面;因為我們在配置里面配置了碟嘴;
3)CasRealm 類通過AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) 方法返回AuthenticationInfo
(1) 這里可以獲取ticket
(2)拿取到ticket可以驗證ticket的有效性
(3)ticket有效會返回token
(4)拿到token可以獲取用戶的基本信息
(5)用戶的基本信息封裝為AuthenticationInfo 對象返回
4)拿到AuthenticationInfo 對象后溪食,AuthenticatingRealm 的this.assertCredentialsMatch(token, info) 方法會校驗token;具體看源碼娜扇;- 過程驗證成功返回AuthenticationInfo 對象错沃,這里對應(yīng)到 7步驟
6)然后就一直返回,直到subject.login(token) - 然后CasFilter 會調(diào)用父類AuthenticatingFilter 類onLoginSuccess()方法雀瓢,進行認(rèn)證成功頁面的跳轉(zhuǎn)枢析;
8)認(rèn)證成功頁面如果不配置放行,也會走UserFilter刃麸,但是這時已經(jīng)認(rèn)證過了醒叁,所以就放行,這樣就訪問到目標(biāo)資源了;
- 過程驗證成功返回AuthenticationInfo 對象错沃,這里對應(yīng)到 7步驟
demo地址:https://gitee.com/ShaoNianRengNianShao/springboot-shiro-cas-demo.git