Shiro实现授权的三种操作方式

1. 你可以使用Apache Shiro帮你做:

  • 验证用户来核实他们的身份
  • 对用户执行访问控制,如:
    • 判断用户是否被分配了一个确定的安全角色
    • 判断用户是否被允许做某事
  • 在任何环境下使用 Session API,即使没有 Web 或 EJB 容器
  • 在身份验证,访问控制期间或在会话的生命周期,对事件作出反应
  • 聚集一个或多个用户安全数据的数据源,并作为一个单一的复合用户“视图”
  • 启用单点登录(SSO)功能
  • 为没有关联到登录的用户启用”Remember Me”服务
  • ……

2. 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TechGenius

文章很值,请作者喝一杯咖啡吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值