- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- shiro與spring集成的示例分析
這篇文章將為大家詳細講解有關(guān)shiro與spring集成的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
shiro的四大基石
身份驗證(登錄)Authentication:身份認證 / 登錄,驗證用戶(hù)是不是擁有相應的身份;
授權(權限)Authorization:驗證某個(gè)已登錄的用戶(hù)是否擁有某個(gè)權限
密碼學(xué)(密碼加密) Cryptography:加密,保護數據的安全性,如密碼加密存儲到數據庫,而不是明文存儲;
會(huì )話(huà)管理 Session Management:用戶(hù)登錄后就是一次會(huì )話(huà),在沒(méi)有退出之前,它的所有信息都在會(huì )話(huà)中;
導包
<!--使用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文件)文件中有咱們的用戶(hù)角色權限
# ini文件里面放的就是咱們的用戶(hù),角色,權限,資源 # ----------------------------------------------------------------------------- # users:用戶(hù) # root:用戶(hù)名 123456:密碼 admin:角色 # ----------------------------------------------------------------------------- [users] root = 123456, admin guest = guest, it # ----------------------------------------------------------------------------- # roles:角色 # admin = * :admin這個(gè)用戶(hù)擁有所有權限 # it = employee:* :it這個(gè)角色擁有員工的所有權限 # hr = employee:save :hr這個(gè)角色擁有員工添加權限 # ----------------------------------------------------------------------------- [roles] admin = * it = employee:* hr = employee:save
功能測試(一定要有測試包org.junit.Test才能測試)
主要測試登錄,權限認證
@Test public void testHello() throws Exception{ //①.拿到權限管理對象 /** * 讀取了shiro.ini的文件(隱藏了realm) -> 隱藏了iniRealm * SecurityManager:權限管理器,shiro的所有功能都放在里面 */ Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); //②.相當于把SecurityManager放到了當前上下文 /** * 可以讓我們在當前系統的任何位置都可以拿到SecurityManager對象 */ SecurityUtils.setSecurityManager(securityManager); //③.拿到當前用戶(hù)(沒(méi)有登錄就是游客) Subject currentUser = SecurityUtils.getSubject(); System.out.println("用戶(hù)是否登錄:"+currentUser.isAuthenticated()); //④.如果沒(méi)有登錄,讓他進(jìn)行登錄 if(!currentUser.isAuthenticated()){ //ctrl+alt+t :包含代碼 try { //4.1 準備令牌(對象) 用戶(hù)名密碼令牌 UsernamePasswordToken token = new UsernamePasswordToken("guest","guest"); //4.2 進(jìn)行登錄功能 currentUser.login(token); } catch (UnknownAccountException e) { //Unknown(未知)Account(賬號)Exception:用戶(hù)名不存在 e.printStackTrace(); System.out.println("哥,你是傻子嘛?"); }catch (IncorrectCredentialsException e){ //Incorrect(不正確)Credentials(憑證)Exception:密碼錯誤 e.printStackTrace(); System.out.println("哥,密碼錯誤了?"); }catch (AuthenticationException e){ //AuthenticationException:登錄中最大的那個(gè)異常 e.printStackTrace(); System.out.println("發(fā)生了一個(gè)神秘的錯誤?。?!"); } } System.out.println("用戶(hù)是否登錄:"+currentUser.isAuthenticated()); System.out.println("是否是管理員角色:"+currentUser.hasRole("admin")); System.out.println("是否是IT角色:"+currentUser.hasRole("it")); System.out.println("是否可以操作employee:save權限:"+currentUser.isPermitted("employee:save")); System.out.println("是否可以操作employee:index權限:"+currentUser.isPermitted("employee:index")); System.out.println("是否可以操作department:index權限:"+currentUser.isPermitted("department:index")); //⑤.還可以登出(注銷(xiāo)) currentUser.logout(); System.out.println("用戶(hù)是否登錄:"+currentUser.isAuthenticated()); }
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站