shiro與spring集成基礎(chǔ)Hello案例詳解
這篇文章主要介紹了shiro與spring集成基礎(chǔ)Hello案例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
shiro的四大基石
- 身份驗(yàn)證(登錄)Authentication:身份認(rèn)證 / 登錄,驗(yàn)證用戶是不是擁有相應(yīng)的身份;
- 授權(quán)(權(quán)限)Authorization:驗(yàn)證某個已登錄的用戶是否擁有某個權(quán)限
- 密碼學(xué)(密碼加密) Cryptography:加密,保護(hù)數(shù)據(jù)的安全性,如密碼加密存儲到數(shù)據(jù)庫,而不是明文存儲;
- 會話管理 Session Management:用戶登錄后就是一次會話,在沒有退出之前,它的所有信息都在會話中;
導(dǎo)包
<!--使用shiro需要先導(dǎo)包-->
<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)限認(rè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();
//②.相當(dāng)于把SecurityManager放到了當(dāng)前上下文
/**
* 可以讓我們在當(dāng)前系統(tǒng)的任何位置都可以拿到SecurityManager對象
*/
SecurityUtils.setSecurityManager(securityManager);
//③.拿到當(dāng)前用戶(沒有登錄就是游客)
Subject currentUser = SecurityUtils.getSubject();
System.out.println("用戶是否登錄:"+currentUser.isAuthenticated());
//④.如果沒有登錄,讓他進(jìn)行登錄
if(!currentUser.isAuthenticated()){
//ctrl+alt+t :包含代碼
try {
//4.1 準(zhǔn)備令牌(對象) 用戶名密碼令牌
UsernamePasswordToken token = new UsernamePasswordToken("guest","guest");
//4.2 進(jìn)行登錄功能
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring Security和Shiro的相同點(diǎn)與不同點(diǎn)整理
- Spring與Shiro整合及加載權(quán)限表達(dá)式問題
- 基于springboot實(shí)現(xiàn)整合shiro實(shí)現(xiàn)登錄認(rèn)證以及授權(quán)過程解析
- SpringBoot + Shiro前后端分離權(quán)限
- shiro整合springboot前后端分離
- Spring boot整合shiro+jwt實(shí)現(xiàn)前后端分離
- 如何在Spring boot加入shiro支持
- spring boot整合shiro安全框架過程解析
- SpringBoot Shiro授權(quán)實(shí)現(xiàn)過程解析
- Springboot整合Shiro的代碼實(shí)例
- Spring Boot 整合 Shiro+Thymeleaf過程解析
- Spring Boot集成Shiro實(shí)現(xiàn)動態(tài)加載權(quán)限的完整步驟
- SpringBoot中Shiro緩存使用Redis、Ehcache的方法
- SpringBoot2.0整合Shiro框架實(shí)現(xiàn)用戶權(quán)限管理的示例
- Spring Boot 自定義 Shiro 過濾器無法使用 @Autowired問題及解決方法
- SpringBoot整合Shiro兩種方式(總結(jié))
- Spring配置shiro時自定義Realm中屬性無法使用注解注入的解決辦法
- Spring Boot2開發(fā)之Spring Boot整合Shiro兩種詳細(xì)方法
相關(guān)文章
@Accessors(chain = true)注解報錯的解決方案
這篇文章主要介紹了@Accessors(chain = true)注解報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring?Security權(quán)限控制的實(shí)現(xiàn)接口
這篇文章主要介紹了Spring?Security的很多功能,在這些眾多功能中,我們知道其核心功能其實(shí)就是認(rèn)證+授權(quán)。Spring教程之Spring?Security的四種權(quán)限控制方式2023-03-03
淺談Map集合中g(shù)et不存在的key值,會拋出異常嗎?
這篇文章主要介紹了淺談Map集合中g(shù)et不存在的key值,會拋出異常嗎?具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
SpringBoot使用Kafka來優(yōu)化接口請求的并發(fā)方式
這篇文章主要介紹了SpringBoot使用Kafka來優(yōu)化接口請求的并發(fā)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07

