shiro與spring集成基礎(chǔ)Hello案例詳解
這篇文章主要介紹了shiro與spring集成基礎(chǔ)Hello案例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
shiro的四大基石
- 身份驗(yàn)證(登錄)Authentication:身份認(rèn)證 / 登錄,驗(yàn)證用戶是不是擁有相應(yīng)的身份;
- 授權(quán)(權(quán)限)Authorization:驗(yàn)證某個(gè)已登錄的用戶是否擁有某個(gè)權(quán)限
- 密碼學(xué)(密碼加密) Cryptography:加密,保護(hù)數(shù)據(jù)的安全性,如密碼加密存儲(chǔ)到數(shù)據(jù)庫(kù),而不是明文存儲(chǔ);
- 會(huì)話管理 Session Management:用戶登錄后就是一次會(huì)話,在沒(méi)有退出之前,它的所有信息都在會(huì)話中;
導(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> <!--測(cè)試包--> <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這個(gè)用戶擁有所有權(quán)限 # it = employee:* :it這個(gè)角色擁有員工的所有權(quán)限 # hr = employee:save :hr這個(gè)角色擁有員工添加權(quán)限 # ----------------------------------------------------------------------------- [roles] admin = * it = employee:* hr = employee:save
功能測(cè)試(一定要有測(cè)試包org.junit.Test才能測(cè)試)
主要測(cè)試登錄,權(quán)限認(rèn)證
@Test public void testHello() throws Exception{ //①.拿到權(quán)限管理對(duì)象 /** * 讀取了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)前上下文 /** * 可以讓我們?cè)诋?dāng)前系統(tǒng)的任何位置都可以拿到SecurityManager對(duì)象 */ SecurityUtils.setSecurityManager(securityManager); //③.拿到當(dāng)前用戶(沒(méi)有登錄就是游客) Subject currentUser = SecurityUtils.getSubject(); System.out.println("用戶是否登錄:"+currentUser.isAuthenticated()); //④.如果沒(méi)有登錄,讓他進(jìn)行登錄 if(!currentUser.isAuthenticated()){ //ctrl+alt+t :包含代碼 try { //4.1 準(zhǔn)備令牌(對(duì)象) 用戶名密碼令牌 UsernamePasswordToken token = new UsernamePasswordToken("guest","guest"); //4.2 進(jìn)行登錄功能 currentUser.login(token); } catch (UnknownAccountException e) { //Unknown(未知)Account(賬號(hào))Exception:用戶名不存在 e.printStackTrace(); System.out.println("哥,你是傻子嘛?"); }catch (IncorrectCredentialsException e){ //Incorrect(不正確)Credentials(憑證)Exception:密碼錯(cuò)誤 e.printStackTrace(); System.out.println("哥,密碼錯(cuò)誤了?"); }catch (AuthenticationException e){ //AuthenticationException:登錄中最大的那個(gè)異常 e.printStackTrace(); System.out.println("發(fā)生了一個(gè)神秘的錯(cuò)誤?。?!"); } } 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()); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring Security和Shiro的相同點(diǎn)與不同點(diǎn)整理
- Spring與Shiro整合及加載權(quán)限表達(dá)式問(wèn)題
- 基于springboot實(shí)現(xiàn)整合shiro實(shí)現(xiàn)登錄認(rèn)證以及授權(quán)過(guò)程解析
- SpringBoot + Shiro前后端分離權(quán)限
- shiro整合springboot前后端分離
- Spring boot整合shiro+jwt實(shí)現(xiàn)前后端分離
- 如何在Spring boot加入shiro支持
- spring boot整合shiro安全框架過(guò)程解析
- SpringBoot Shiro授權(quán)實(shí)現(xiàn)過(guò)程解析
- Springboot整合Shiro的代碼實(shí)例
- Spring Boot 整合 Shiro+Thymeleaf過(guò)程解析
- Spring Boot集成Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限的完整步驟
- SpringBoot中Shiro緩存使用Redis、Ehcache的方法
- SpringBoot2.0整合Shiro框架實(shí)現(xiàn)用戶權(quán)限管理的示例
- Spring Boot 自定義 Shiro 過(guò)濾器無(wú)法使用 @Autowired問(wèn)題及解決方法
- SpringBoot整合Shiro兩種方式(總結(jié))
- Spring配置shiro時(shí)自定義Realm中屬性無(wú)法使用注解注入的解決辦法
- Spring Boot2開(kāi)發(fā)之Spring Boot整合Shiro兩種詳細(xì)方法
相關(guān)文章
Spring boot如何通過(guò)@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及多線程配置
這篇文章主要介紹了Spring boot如何通過(guò)@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及多線程配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12