shiro實現授權的三種操作
授權的例子就是是否可以訪問某個頁面,可以操作某個按鈕,是否可以編緝對應的數據等。
如何在shiro中使用授權
1,使用編程方式
判斷是否有管理員角色
if (currentUser.hasRole("admin")) {
判斷用戶是否有打印的權限
Permission printPermission =new PrinterPermission(“laserjet3000n”,“print”);
If (currentUser.isPermitted(printPermission)) {
//do one thing (show the print button?)?
} else {
//don’t show the button?
}
也可以使用字符串的方式驗證
String perm = “printer:print:laserjet4400n”;
if(currentUser.isPermitted(perm)){
//show the print button?
}else {
//don’t show the button?
}
2,使用注釋方式
判斷用戶是否有 創建賬戶權限
//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
}
3,使用jsp taglib
判斷用戶是否有管理權限
<%@ 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>