细说shiro之三:在独立应用中使用shiro
官網(wǎng):https://shiro.apache.org/
?
1. 下載
在非Web環(huán)境的獨立應(yīng)用中使用Shiro時,只需要shiro-core組件。
在Maven項目中的依賴配置如下:
<!-- Shiro uses SLF4J for logging. We'll use the 'simple' binding in this example app. See http://www.slf4j.org for more info. --> <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>1.6.1</version> </dependency>
特別地!Shiro使用了日志框架slf4j,因此需要對應(yīng)配置指定的日志實現(xiàn)組件,如:log4j,logback等。
在此,使用slf4j的簡單日志實現(xiàn)slf4j-simple。
2. 數(shù)據(jù)源配置
在Shiro中,Realm定義了訪問數(shù)據(jù)的方式,用來連接不同的數(shù)據(jù)源,如:LDAP,關(guān)系數(shù)據(jù)庫,配置文件等等。
Realm類圖:
也就是說,可以根據(jù)實際需求及應(yīng)用的權(quán)限管理復(fù)雜度靈活選擇指定數(shù)據(jù)源。
在此,以org.apache.shiro.realm.text.IniRealm為例,具體配置如下:
shiro.ini:
# ============================================================================= # Tutorial INI configuration # # Usernames/passwords are based on the classic Mel Brooks' film "Spaceballs" :) # =============================================================================# ----------------------------------------------------------------------------- # Users and their (optional) assigned roles # username = password, role1, role2, ..., roleN # ----------------------------------------------------------------------------- [users] root = secret, admin guest = guest, guest presidentskroob = 12345, president darkhelmet = ludicrousspeed, darklord, schwartz lonestarr = vespa, goodguy, schwartz# ----------------------------------------------------------------------------- # Roles with assigned permissions # roleName = perm1, perm2, ..., permN # ----------------------------------------------------------------------------- [roles] admin = * schwartz = lightsaber:* goodguy = winnebago:drive:eagle5選擇了數(shù)據(jù)源,現(xiàn)在開始使用Shiro進(jìn)行認(rèn)證和訪問授權(quán)控制。
?
3. 認(rèn)證
在Shiro中,認(rèn)證即執(zhí)行用戶登錄,讀取指定Realm連接的數(shù)據(jù)源,以驗證用戶身份的有效性與合法性。
?
4. 訪問授權(quán)
在Shiro中,訪問授權(quán)即驗證用戶是否具備執(zhí)行指定操作的權(quán)限(角色或權(quán)限驗證)。
特別地!在執(zhí)行訪問授權(quán)驗證之前,必須執(zhí)行用戶認(rèn)證。
?
5. 完整示例
詳見:https://git.oschina.net/cchanghui/test-shirocli.git
?
轉(zhuǎn)載于:https://www.cnblogs.com/nuccch/p/6780550.html
總結(jié)
以上是生活随笔為你收集整理的细说shiro之三:在独立应用中使用shiro的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光大Joy&Doga小白联名卡额
- 下一篇: 第六章 预处理器