shiro與spring集成基礎(chǔ)Hello案例詳解
更新時間:2019年11月25日 16:56:23 作者:肖鏡澤
這篇文章主要介紹了shiro與spring集成基礎(chǔ)Hello案例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
這篇文章主要介紹了shiro與spring集成基礎(chǔ)Hello案例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
shiro的四大基石
- 身份驗證(登錄)Authentication:身份認證 / 登錄,驗證用戶是不是擁有相應的身份;
- 授權(quán)(權(quán)限)Authorization:驗證某個已登錄的用戶是否擁有某個權(quán)限
- 密碼學(密碼加密) Cryptography:加密,保護數(shù)據(jù)的安全性,如密碼加密存儲到數(shù)據(jù)庫,而不是明文存儲;
- 會話管理 Session Management:用戶登錄后就是一次會話,在沒有退出之前,它的所有信息都在會話中;
導包
<!--使用shiro需要先導包--> <dependencies> <!--shiro的核心包--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version> </dependency> <!--日志包--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!--測試包--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> </dependencies>
ini文件(再創(chuàng)建shiro.ini文件)文件中有咱們的用戶角色權(quán)限
# ini文件里面放的就是咱們的用戶,角色,權(quán)限,資源 # ----------------------------------------------------------------------------- # users:用戶 # root:用戶名 123456:密碼 admin:角色 # ----------------------------------------------------------------------------- [users] root = 123456, admin guest = guest, it # ----------------------------------------------------------------------------- # roles:角色 # admin = * :admin這個用戶擁有所有權(quán)限 # it = employee:* :it這個角色擁有員工的所有權(quán)限 # hr = employee:save :hr這個角色擁有員工添加權(quán)限 # ----------------------------------------------------------------------------- [roles] admin = * it = employee:* hr = employee:save
功能測試(一定要有測試包org.junit.Test才能測試)
主要測試登錄,權(quán)限認證
@Test public void testHello() throws Exception{ //①.拿到權(quán)限管理對象 /** * 讀取了shiro.ini的文件(隱藏了realm) -> 隱藏了iniRealm * SecurityManager:權(quán)限管理器,shiro的所有功能都放在里面 */ Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); //②.相當于把SecurityManager放到了當前上下文 /** * 可以讓我們在當前系統(tǒng)的任何位置都可以拿到SecurityManager對象 */ SecurityUtils.setSecurityManager(securityManager); //③.拿到當前用戶(沒有登錄就是游客) Subject currentUser = SecurityUtils.getSubject(); System.out.println("用戶是否登錄:"+currentUser.isAuthenticated()); //④.如果沒有登錄,讓他進行登錄 if(!currentUser.isAuthenticated()){ //ctrl+alt+t :包含代碼 try { //4.1 準備令牌(對象) 用戶名密碼令牌 UsernamePasswordToken token = new UsernamePasswordToken("guest","guest"); //4.2 進行登錄功能 currentUser.login(token); } catch (UnknownAccountException e) { //Unknown(未知)Account(賬號)Exception:用戶名不存在 e.printStackTrace(); System.out.println("哥,你是傻子嘛?"); }catch (IncorrectCredentialsException e){ //Incorrect(不正確)Credentials(憑證)Exception:密碼錯誤 e.printStackTrace(); System.out.println("哥,密碼錯誤了?"); }catch (AuthenticationException e){ //AuthenticationException:登錄中最大的那個異常 e.printStackTrace(); System.out.println("發(fā)生了一個神秘的錯誤?。?!"); } } System.out.println("用戶是否登錄:"+currentUser.isAuthenticated()); System.out.println("是否是管理員角色:"+currentUser.hasRole("admin")); System.out.println("是否是IT角色:"+currentUser.hasRole("it")); System.out.println("是否可以操作employee:save權(quán)限:"+currentUser.isPermitted("employee:save")); System.out.println("是否可以操作employee:index權(quán)限:"+currentUser.isPermitted("employee:index")); System.out.println("是否可以操作department:index權(quán)限:"+currentUser.isPermitted("department:index")); //⑤.還可以登出(注銷) currentUser.logout(); System.out.println("用戶是否登錄:"+currentUser.isAuthenticated()); }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Spring Security和Shiro的相同點與不同點整理
- Spring與Shiro整合及加載權(quán)限表達式問題
- 基于springboot實現(xiàn)整合shiro實現(xiàn)登錄認證以及授權(quán)過程解析
- SpringBoot + Shiro前后端分離權(quán)限
- shiro整合springboot前后端分離
- Spring boot整合shiro+jwt實現(xiàn)前后端分離
- 如何在Spring boot加入shiro支持
- spring boot整合shiro安全框架過程解析
- SpringBoot Shiro授權(quán)實現(xiàn)過程解析
- Springboot整合Shiro的代碼實例
- Spring Boot 整合 Shiro+Thymeleaf過程解析
- Spring Boot集成Shiro實現(xiàn)動態(tài)加載權(quán)限的完整步驟
- SpringBoot中Shiro緩存使用Redis、Ehcache的方法
- SpringBoot2.0整合Shiro框架實現(xiàn)用戶權(quán)限管理的示例
- Spring Boot 自定義 Shiro 過濾器無法使用 @Autowired問題及解決方法
- SpringBoot整合Shiro兩種方式(總結(jié))
- Spring配置shiro時自定義Realm中屬性無法使用注解注入的解決辦法
- Spring Boot2開發(fā)之Spring Boot整合Shiro兩種詳細方法
相關(guān)文章
Spring boot如何通過@Scheduled實現(xiàn)定時任務及多線程配置
這篇文章主要介紹了Spring boot如何通過@Scheduled實現(xiàn)定時任務及多線程配置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12