细说shiro之三:在独立应用中使用shiro
官網:https://shiro.apache.org/
?
1. 下載
在非Web環境的獨立應用中使用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,因此需要對應配置指定的日志實現組件,如:log4j,logback等。
在此,使用slf4j的簡單日志實現slf4j-simple。
2. 數據源配置
在Shiro中,Realm定義了訪問數據的方式,用來連接不同的數據源,如:LDAP,關系數據庫,配置文件等等。
Realm類圖:
也就是說,可以根據實際需求及應用的權限管理復雜度靈活選擇指定數據源。
在此,以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選擇了數據源,現在開始使用Shiro進行認證和訪問授權控制。
?
3. 認證
在Shiro中,認證即執行用戶登錄,讀取指定Realm連接的數據源,以驗證用戶身份的有效性與合法性。
?
4. 訪問授權
在Shiro中,訪問授權即驗證用戶是否具備執行指定操作的權限(角色或權限驗證)。
特別地!在執行訪問授權驗證之前,必須執行用戶認證。
?
5. 完整示例
詳見:https://git.oschina.net/cchanghui/test-shirocli.git
?
轉載于:https://www.cnblogs.com/nuccch/p/6780550.html
總結
以上是生活随笔為你收集整理的细说shiro之三:在独立应用中使用shiro的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光大Joy&Doga小白联名卡额
- 下一篇: 第六章 预处理器