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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值