受權的例子就是是否能夠訪問某個頁面,能夠操做某個按鈕,是否能夠編緝對應的數據等。html
如何在shiro中使用受權apache
判斷是否有管理員角色編程
if (currentUser.hasRole("admin")) {jsp
判斷用戶是否有打印的權限ui
Permission printPermission =new PrinterPermission(「laserjet3000n」,「print」);spa
If (currentUser.isPermitted(printPermission)) {htm
//do one thing (show the print button?)字符串
} else {it
//don’t show the button?io
}
也可使用字符串的方式驗證
String perm = 「printer:print:laserjet4400n」;
if(currentUser.isPermitted(perm)){
//show the print button?
}else {
//don’t show the button?
}
判斷用戶是否有 建立帳戶權限
//Will throw an AuthorizationException if none
//of the caller’s roles imply the Account
//'create' permission\u000B
@RequiresPermissions(「account:create」)
public void openAccount( Account acct ) {
//create the account
}
判斷用戶角色,若是符合角色,可使用對應方法
//Throws an AuthorizationException if the caller
//doesn’t have the ‘teller’ role:
@RequiresRoles( 「teller」 )
public void openAccount( Account acct ) {
//do something in here that only a teller
//should do
}
判斷用戶是否有管理權限
<%@ taglib prefix=「shiro」 uri=http://shiro.apache.org/tags %>
<html>
<body>
<shiro:hasPermission name=「users:manage」>
<a href=「manageUsers.jsp」>
Click here to manage users
</a>
</shiro:hasPermission>
<shiro:lacksPermission name=「users:manage」>
No user managementfor you!
</shiro:lacksPermission>
</body>
</html>