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

来源:互联网 发布:淘宝使用的第三方平台 编辑:程序博客网 时间:2024/06/06 19:32

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>
0 0
原创粉丝点击