shiro将session认证改成token认证_Shiro 运行过程
什么是shiro
shiro是apache的一個開源框架,是一個權限管理的框架,實現(xiàn) 用戶認證、用戶授權、
shiro架構(gòu)
subject:主體,可以是用戶也可以是程序,主體要訪問系統(tǒng),系統(tǒng)需要對主體進行認證、授權。
securityManager:安全管理器,主體進行認證和授權都是通過securityManager進行。
authenticator:認證器,主體進行認證最終通過authenticator進行的。
authorizer:授權器,主體進行授權最終通過authorizer進行的。
sessionManager:web應用中一般是用web容器對session進行管理,shiro也提供一套session管理的方式。
SessionDao: 通過SessionDao管理session數(shù)據(jù),針對個性化的session數(shù)據(jù)存儲需要使用sessionDao。
cache Manager:緩存管理器,主要對session和授權數(shù)據(jù)進行緩存,比如將授權數(shù)據(jù)通過cacheManager進行緩存管理,和ehcache整合對緩存數(shù)據(jù)進行管理。
realm:域,領域,相當于數(shù)據(jù)源,通過realm存取認證、授權相關數(shù)據(jù)。
注意:在realm中存儲授權和認證的邏輯。
cryptography:密碼管理,提供了一套加密/解密的組件,方便開發(fā)。比如提供常用的散列、加/解密等功能。
比如 md5散列算法。
認證過程,授權過程
認證過程
1、通過ini配置文件創(chuàng)建securityManager
2、調(diào)用subject.login方法主體提交認證,提交的token
3、securityManager進行認證,securityManager最終由ModularRealmAuthenticator進行認證。
4、ModularRealmAuthenticator調(diào)用IniRealm(給realm傳入token) 去ini配置文件中查詢用戶信息
5、IniRealm根據(jù)輸入的token(UsernamePasswordToken)從 shiro.ini查詢用戶信息,根據(jù)賬號查詢用戶信息(賬號和密碼)
如果查詢到用戶信息,就給ModularRealmAuthenticator返回用戶信息(賬號和密碼)
如果查詢不到,就給ModularRealmAuthenticator返回null
6、ModularRealmAuthenticator接收IniRealm返回Authentication認證信息
如果返回的認證信息是null,ModularRealmAuthenticator拋出異常(org.apache.shiro.authc.UnknownAccountException)
如果返回的認證信息不是null(說明inirealm找到了用戶),對IniRealm返回用戶密碼 (在ini文件中存在)
和 token中的密碼 進行對比,如果不一致拋出異常(org.apache.shiro.authc.IncorrectCredentialsException)
授權流程
1、對subject進行授權,調(diào)用方法isPermitted("permission串")
2、SecurityManager執(zhí)行授權,通過ModularRealmAuthorizer執(zhí)行授權
3、ModularRealmAuthorizer執(zhí)行realm(自定義的Realm)從數(shù)據(jù)庫查詢權限數(shù)據(jù)
調(diào)用realm的授權方法:doGetAuthorizationInfo
4、realm從數(shù)據(jù)庫查詢權限數(shù)據(jù),返回ModularRealmAuthorizer
5、ModularRealmAuthorizer調(diào)用PermissionResolver進行權限串比對
6、如果比對后,isPermitted中"permission串"在realm查詢到權限數(shù)據(jù)中,說明用戶訪問permission串有權限,否則 沒有權限,拋出異常。
其他安全框架對比
spring中有spring security (原名Acegi),是一個權限框架,它和spring依賴過于緊密,沒有shiro使用簡單。
shiro不依賴于spring,shiro不僅可以實現(xiàn) web應用的權限管理,還可以實現(xiàn)c/s系統(tǒng),分布式系統(tǒng)權限管理,shiro屬于輕量框架,越來越多企業(yè)項目開始使用shiro。
使用shiro實現(xiàn)系統(tǒng)的權限管理,有效提高開發(fā)效率,從而降低開發(fā)成本。
總結(jié)
以上是生活随笔為你收集整理的shiro将session认证改成token认证_Shiro 运行过程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lua 区间比较_Lua(模糊查找):判
- 下一篇: 有效数据外含有额外数据_basemap之