shiro.ini实现授权
shiro.ini實(shí)現(xiàn)授權(quán)
前提:必須先認(rèn)證通過之后有授權(quán)之說
1,授權(quán)概述
授權(quán),也叫訪問控制,即在應(yīng)用中控制誰能訪問哪些資源(如訪問頁(yè)面/編輯數(shù)據(jù)/頁(yè)面操作等)。在授權(quán)中需了解的幾個(gè)關(guān)鍵對(duì)象:主體(Subject)、資源(Resource)、權(quán)限(Permission)、角色(Role)。
2,關(guān)鍵對(duì)象介紹
1,主體
主體,即訪問應(yīng)用的用戶,在Shiro中使用Subject代表該用戶。用戶只有授權(quán)后才允許訪問相應(yīng)的資源。
2,資源
在應(yīng)用中用戶可以訪問的任何東西,比如訪問JSP 頁(yè)面、查看/編輯某些數(shù)據(jù)、訪問某個(gè)業(yè)務(wù)方法、打印文本等等都是資源。用戶只要授權(quán)后才能訪問。
3,權(quán)限
安全策略中的原子授權(quán)單位,通過權(quán)限我們可以表示在應(yīng)用中用戶有沒有操作某個(gè)資源的權(quán)力。即權(quán)限表示在應(yīng)用中用戶能不能訪問某個(gè)資源,如:訪問用戶列表頁(yè)面查看/新增/修改/刪除用戶數(shù)據(jù)(即很多時(shí)候都是CRUD(增查改刪)式權(quán)限控制)打印文檔等等。。。
4,角色
角色代表了操作集合,可以理解為權(quán)限的集合,一般情況下我們會(huì)賦予用戶角色而不是權(quán)限,即這樣用戶可以擁有一組權(quán)限,賦予權(quán)限時(shí)比較方便。典型的如:項(xiàng)目經(jīng)理、技術(shù)總監(jiān)、CTO、開發(fā)工程師等都是角色,不同的角色擁有一組不同的權(quán)限。
3,授權(quán)流程
4,相關(guān)方法說明
1 subject.hasRole(“”); 判斷是否有角色
2 subject.hasRoles(List);分別判斷用戶是否具有List中每個(gè)內(nèi)容
3 subject.hasAllRoles(Collection);返回boolean,要求參數(shù)中所有角色用戶都需要具有.
4 subject.isPermitted(“”);判斷是否具有權(quán)限.
5 subject.isPermittedAll(“”);判斷是否具有權(quán)限.
shiro.ini
#配置用戶 [users] zhangsan=123456,role1 lisi=123456,role2 wangwu=123456,role3 zhaoliu=123456,role2,role3 sunqi=123456,role4#聲明角色 [roles] role1=user:query,user:add,user:update,user:delete,user:export role2=user:query,user:add role3=user:query,user:export role4=*:*TestAuthorizationApp.java
package com.sxt.shiro;import java.util.Arrays; import java.util.List;import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.config.IniSecurityManagerFactory; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; import org.slf4j.Logger; import org.slf4j.LoggerFactory;/*** shiro的認(rèn)證使用shiro.ini文件**/ @SuppressWarnings("deprecation") public class TestAuthorizationApp {// 日志輸出工具private static final transient Logger log = LoggerFactory.getLogger(TestAuthorizationApp.class);public static void main(String[] args) {String username = "zhangsan";String password = "123456";log.info("My First Apache Shiro Application");// 1,創(chuàng)建安全管理器的工廠對(duì)象 org.apache.shiro.mgt.SecurityManager; 不能使用java.lang.SecurityManagerFactory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");// 2,使用工廠創(chuàng)建安全管理器SecurityManager securityManager = factory.getInstance();// 3,把當(dāng)前的安全管理器綁定當(dāng)?shù)骄€的線程SecurityUtils.setSecurityManager(securityManager);// 4,使用SecurityUtils.getSubject得到主體對(duì)象Subject subject = SecurityUtils.getSubject();// 5,封裝用戶名和密碼AuthenticationToken token = new UsernamePasswordToken(username, password);// 6,得到認(rèn)證try {subject.login(token);System.out.println("認(rèn)證通過");} catch (AuthenticationException e) {System.out.println("用戶名或密碼不正確");} //subject.logout();//退出的方法//判斷用戶是否認(rèn)證通過boolean authenticated = subject.isAuthenticated();System.out.println("是否認(rèn)證通過:"+authenticated);//角色判斷boolean hasRole1 = subject.hasRole("role1");System.out.println("是否有role1的角色:"+hasRole1);//分別判斷集合里面的角色 返回?cái)?shù)組List<String> roleIdentifiers=Arrays.asList("role1","role2","role3");boolean[] hasRoles = subject.hasRoles(roleIdentifiers);for (boolean b : hasRoles) {System.out.println(b);}//判斷當(dāng)前用戶是否有roleIdentifiers集合里面的所有角色boolean hasAllRoles = subject.hasAllRoles(roleIdentifiers);System.out.println(hasAllRoles);//權(quán)限判斷boolean permitted = subject.isPermitted("user:query");System.out.println("判斷當(dāng)前用戶是否有user:query的權(quán)限 "+permitted);boolean[] permitted2 = subject.isPermitted("user:query","user:add","user:export");for (boolean b : permitted2) {System.out.println(b);}boolean permittedAll = subject.isPermittedAll("user:query","user:add","user:export");System.out.println(permittedAll);}}?
?
?
?
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的shiro.ini实现授权的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shiro实现认证_ini
- 下一篇: 自定义Realm实现认证