1、授权实现方式

1.1、什么是授权

授权包含4个元素(一个比较流行通用的权限模型)

Resources:资源
  各种需要访问控制的资源

Permissions:权限
  安全策略控制原子元素
  基于资源和动作
  控制力度

Roles:角色
   行为的集合

User:用户主体
  Subject,关联Role或Permission

简单来说,可以这样理解:我们登录进系统,我们就是一个【用户】;【用户】可以是一个或多个【角色】,一个【角色】可以有多种【权限】,这些【权限】代表了我们可以访问哪些【资源】。当然【用户】也可以直接跳过【角色】,直接给【用户】分配【权限】,表明这个【用户】可以访问的【资源】。

1.2、授权方式

a、编程模型

  基于Role

    Role校验
      API:
        hasRole(String roleName)
        hasRoles(List<String> roleNames)
        hasAllRoles(Collection<String> roleNames)

Subject currentUser = SecurityUtils.getSubject();
if(currentUser.hasRole("admin")){
...
} else{
...
}

    Role断言(Assertion)
      失败会抛出异常AuthorizationException
      API
        checkRole(String roleName)
        checkRoles(Collection<String> roleNames)
        checkRoles(String... roleNames)

Subject currentUser = SecurityUtils.getSubject();
currentUser.checkRole("bankTeller");
openBankAccount();

  基于Permission

    Permission校验
      基于对象的Permission校验
        应用场景:显式控制、类型安全
        API
          isPermiited(Permission p)
          isPermiited(List<Permission> perms)
          isPermiitedAll(Collection<Permission> perms)

Permission printPermission = new PrinterPermission("hp","print");
Subject currentUser = SecurityUtils.getSubject();
if(currentUser.isPermitted(printPermission)){
...
} else {
...
}

      基于String的Permission校验
        应用场景:轻量级、简单
        API
          isPermiited(String perm)
          isPermiited(String... perms)
          isPermiitedAll(String... perms)

Subject currentUser = SecurityUtils.getSubject();
if(currentUser.isPermitted("printer:print:hp")){
...
} else {
...
}

    Permission断言(Assertion)
      失败会抛出异常AuthorizationException
      API
        checkPermission(Permission p))
        checkPermission(String perm)
        checkPermissions(Collection<Permission> perms)
        checkPermissions(String... perms)

Subject currentUser = SecurityUtils.getSubject();

Permission p = new AccountPermission("open");
current.checkPermission(p);
openBankAccount();

b、JDK注解

@RequiresAuthentication

用于判断是否已认证,未认证访问该资源会抛异常,下面代码效果相同

@RequiresAuthentication
public void updateAccount(Account userAccount){
...
} public void updateAccount(Account userAccount){
if(!SecurityUtils.getSubject().isAuthenticated()){
throw new AuthorizationException(...);
}
}

@RequiresGuest

用于判断是否为游客,如果非游客会抛异常,下面代码效果相同

@RequiresGuest
public void signUp(User newUser){
...
} public void signUp(User newUser){
Subject currentUser = SecurityUtils.getSubject();
PrincipalCollection principals = currentUser.getPrincipals();
if(principals != null && !principals.isEmpty()){
throw new AuthorizationException(...);
}
}

@RequiresPermissions

用于判断有该权限才能访问,下面代码效果相同

@ReruiresPermissions("account:create")
public void creatAccount(Account account){
...
} public void creatAccount(Account account){
Subject currentUser = SecurityUtils.getSubject();
if(!subject.isPermitted("account:create")){
throw new AuthorizationException(...);
}
}

@RequiresRoles

用于判断有该角色才能访问,下面代码效果相同

@RequiresRoles("admin")
public void deleteUser(User user){
...
} public void deleteUser(User user){
Subject currentUser = SecurityUtils.getSubject();
if(!subject.hasRole("admin")){
throw new AuthorizationException(...);
}
}

@RequiresUser

用于判断非游客才能访问,下面代码效果相同

@RequiresUser
public void updateAccount(Account account){
...
} public void updateAccount(Account account){
Subject currentUser = SecurityUtils.getSubject();
PrincipalCollection principals = currentUser.getPrincipals();
if(principals == null || principals.isEmpty()){
throw new AuthorizationException(...);
}
}

c、JSP/GSP TagLibs

偏向web,不介绍

2、授权架构

1、调用Subject的isPermitted或HasRole方法

2、找Security Manager(门面模式)

3、调用Authorizer组件

4、通过Realm访问数据库等获取数据,用于判断是否有授权

Authorizer

默认实现ModularRealmAuthorizer

  迭代授权多个Realm

策略

  如果一个Realm不实现Authorizer,不校验

  如果一个Realm实现Authorizer

    一旦校验失败,马上抛AuthorizationException

    一旦校验成功,马上返回true

PermissionResolver权限解析器

  用于将Permission字符串解析成Permission对象,Shiro内部都是使用Permission对象来进行验证

  默认WildcardPermissionResolver(通配符权限解析器)

  可以自定义解析器

RolePermissionResolver

  用于将Role字符串转换为Permission对象

  可以自定义解析器

  

【Shiro】四、Apache Shiro授权的更多相关文章

  1. 【Shiro】Apache Shiro架构之权限认证(Authorization)

    Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之集成web [Shiro]Apache Shir ...

  2. 【Shiro】Apache Shiro架构之身份认证(Authentication)

    Shiro系列文章: [Shiro]Apache Shiro架构之权限认证(Authorization) [Shiro]Apache Shiro架构之集成web [Shiro]Apache Shiro ...

  3. 【Shiro】Apache Shiro架构之自定义realm

    [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之权限认证(Authorization) [Shiro]Apache S ...

  4. 【Shiro】Apache Shiro架构之集成web

    Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之权限认证(Authorization) [Shi ...

  5. SpringBoot整合Shiro 四:认证+授权

    搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 shiro整合Mybatis见:SpringBoot整合 ...

  6. [转载] 【Shiro】Apache Shiro架构之实际运用(整合到Spring中)

    写在前面:前面陆陆续续对Shiro的使用做了一些总结,如题,这篇博文主要是总结一下如何将Shiro运用到实际项目中,也就是将Shiro整到Spring中进行开发.后来想想既然要整,就索性把Spring ...

  7. 让Apache Shiro保护你的应用

    在尝试保护你的应用时,你是否有过挫败感?是否觉得现有的Java安全解决方案难以使用,只会让你更糊涂?本文介绍的Apache Shiro,是一个不同寻常的Java安全框架,为保护应用提供了简单而强大的方 ...

  8. Apache Shiro 手册

    (一)Shiro架构介绍 一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户"登录 ...

  9. Apache Shiro 使用手册---转载

    原文地址:http://www.360doc.com/content/12/0104/13/834950_177177202.shtml (一)Shiro架构介绍 一.什么是Shiro Apache ...

  10. Apache Shiro 使用手册

    http://kdboy.iteye.com/blog/1154644 (一)Shiro架构介绍 一.什么是Shiro  Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加 ...

随机推荐

  1. PyQuery爬取历史天气信息

    1.准备工作: 网址:https://lishi.tianqi.com/xian/index.html 爬虫类库:PyQuery,requests 2.网页分析: 红线部分可更改为需要爬取的城市名,如 ...

  2. 踩坑记之字体图标在webpack中

    首先介绍一个还不错的字体图标库: font-awesome 是在发现bootstrap在npm下载后没有font包以后重新找的字体库,内容还挺丰富的,好像还有动态图,感兴趣的可以看一下 我的环境是we ...

  3. sql查询某个时间内的数据

    hour) 七天之前的数据 SELECT * FROM commodity_order where create_time <= (now()-INTERVAL 7 DAY) order by ...

  4. [题解]Shorten IPv6 Address-模拟(2019牛客多校第六场B题)

    题目链接:https://ac.nowcoder.com/acm/contest/886/B 题意: 您将获得一个IPv6地址,该地址是128位二进制字符串.请根据以下规则确定其最短的表示: 以十六进 ...

  5. 基于全局地址池的DHCP

    一.实验目的 二.实验拓扑图 .三.实验编址 四.实验步骤 1.配置IP 2.配置基于全局地址池的DHCP server 使用IP pool命令创建一个新的全局地址池,名称为hjt1 配置hjt1可动 ...

  6. 在虚拟机的Linux系统下安装wineqq

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 本文参考教程:http://www.ubuntukylin.com/ukylin/forum.php?mod=viewthread& ...

  7. ==和equal的区别

    1.“==”运算符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的数值是否相同,要比较两个基本类型的数据或两个引用变量是否相等,只能用==操作符. 如果一个变量指向的数据是对 ...

  8. 如何使用 VLD 检测程序中的内存泄漏?

    下载地址:https://kinddragon.github.io/vld/ 下载 windows 安装包,进行安装即可,它会给你设置好 vs 的环境变量,使用时,直接在 vs ide 中包含即可. ...

  9. 试试监听输入框的值 (eq:在未输入前,按钮为灰色,输入内容后,按钮变蓝色)

    参考网址:https://blog.csdn.net/tel13259437538/article/details/78927071

  10. C常量与变量

    /** * C中的常量与变量 * 常量的值在程序中是不可变化的,其在定义时必须给一个初始值 * 常量的定义方式: * 1.#define 定义宏常量 * 2.const 定义const常量 * 对于# ...