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. python的os.path.join()

    在python中,os.path.join()是用来拼接目录路径得.同类型得还有join(),os.path.spilt(),spilt()三个函数.1,os.path.join(),将join()里 ...

  2. (转载)解决vmware上安装ubuntu不能联网的问题

    在vmware中安装Ubuntu之后,我们希望基本的功能如上网.传输文件等功能都是可用的,但是经常遇到不能上网的情况.使用笔记本时,我们经常希望能通过无线网卡上网,但是在做嵌入式开发时,我们还希望虚拟 ...

  3. APIO2010 特别行动队 & 斜率优化DP算法笔记

    做完此题之后 自己应该算是真正理解了斜率优化DP 根据状态转移方程$f[i]=max(f[j]+ax^2+bx+c),x=sum[i]-sum[j]$ 可以变形为 $f[i]=max((a*sum[j ...

  4. 【SpringBoot】 理解Spirng中的IOC原理

    前言 前文已经介绍了Spring Bean的生命周期,在这个周期内有一个重要的概念就是: IOC容器 大家也知道IOC是Sping 的重要核心之一,那么如何理解它呢,它又是产生什么作用呢?本文就IOC ...

  5. smartGit的使用

    合理管理自己的代码,对程序员来说是一件很重要的事,今天我也走上了github托管之路 要在Git上托管代码,首先你要要github官网创建一个代码仓库,用来放你的代码. 一,要托管到github,那你 ...

  6. 基于Java Agent的premain方式实现方法耗时监控(转),为了找到结论执行:premain在jvm启动的时候执行,所有方法前,会执行MyAgent的premain方法

    Java Agent是依附于java应用程序并能对其字节码做相关更改的一项技术,它也是一个Jar包,但并不能独立运行,有点像寄生虫的感觉.当今的许多开源工具尤其是监控和诊断工具,很多都是基于Java ...

  7. Reciteing(first)

      it is sybmbolically portrayed in this cartoon,when a teacher assigns her student to read a literat ...

  8. 《JavaScript DOM 编程艺术》学习成果

    (在线演示地址)[http://thqy39.github.io/works/03.Js%20DOM%20website/index.html]

  9. ASP.NET Core 2.1 JWT token (一) - 简书

    原文:ASP.NET Core 2.1 JWT token (一) - 简书 JwtBearer认证是一种标准的,通用的,无状态的,与语言无关的认证方式.Bearer验证属于HTTP协议标准验证. 如 ...

  10. ArcGIS 面要素缝隙孔洞检查代码 C# GP

    public class PolygonGapChecker : CheckProgressMessageSender, IChecker, ICheckProgressChangeEvent { p ...