這一節是屬於授權的內容,我們的例子是要說明如何判斷主體是否有相應的角色和許可權。
配置檔案片段:import org.apache
.shiro
.securityutils
;import org.apache
.shiro
.authc
.authenticationexception
;import org.apache
.shiro
.authc
.usernamepasswordtoken
;import org.apache
.shiro
.config
.inisecuritymanage***ctory
;import org.apache
.shiro
.mgt
.securitymanager
;import org.apache
.shiro
.subject
.subject
;import org.apache
.shiro
.util
.factory
;/**
* created by liwei on 16/8/12.
*/public class shiroutil catch (authenticationexception e)
return currentuser;
}}
我們可以看到乙個使用者具有的角色是通過乙個字串的集合來表示的。[users]
# 表示記憶體中有乙個使用者(使用者名稱 liwei,密碼 123456),他的角色是 role1 、role2
liwei=123456,role1,role2
zhouguang=123456,role1
測試**:
配置檔案片段:public
class
roletest
// 區別上面的方法,下面的方法測試的是,乙個角色是否同時擁有指定的幾個角色
system.out.println(currentuser.hasallroles(arrays.aslist("role1","role2")));
system.out.println(currentuser.hasallroles(arrays.aslist("role1","role2","role3")));
currentuser.logout();
}/**
* 與上面的方法的不同之處在於,角色驗證沒有通過,丟擲執行時異常
*/@test
public
void
testcheckrole()
}
測試**:[users]
liwei=123456,role1,role2
zhouguang=123456,role1
[roles]
role1=user:select
role2=user:add,user:update,user:delete
/**
* created by liwei on 2016/2/9.
*/public class permissiontest
// 測試是否擁有全部的許可權
system.out.println(currentuser.ispermittedall("
user:select
","user:update
","user:add
","user:delete
"));
currentuser.logout();
}@test
public void testcheckpermitted()
}
Shiro學習筆記(四) shiro實現授權
授權,也叫訪問控制,即在應用中控制誰能訪問哪些資源 如訪問頁面 編輯資料 頁面操作等 在授權中需了解的幾個關鍵物件 主體 subject 資源 resource 許可權 permission 角色 role subject 發起請求,判斷是否具有相應的角色或許可權 securitymanager 委...
Shiro學習之Shiro授權的內部處理機制(六)
1 在應用程式中呼叫授權驗證方法 subject的ispermitted 或hasrole 等 2 sbuject的例項通常是delegatingsubject類 或子類 的例項物件,在認證開始時,會委託應用程式設定的securitymanager例項呼叫相應的ispermitted 或hasrol...
Shiro認證和授權學習記錄
subject 使用者主體 操作扔給securitymanager securitymanager 安全管理器 關聯realm realm shiro和資料庫打交道的 org.apache.shirogroupid shiro springartifactid 1.4.0version depend...