shiro實現授權的三種操作

jopen 11年前發布 | 197K 次閱讀 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>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!