shiro授权过程

article/2025/10/12 8:58:11

一、授权的核心概念

授权,也就是权限认证或访问控制,即在应用中控制谁能访问哪些资源
授权中的核心要素:
1 用户,在shiro中代表访问系统的任何客户端,即subject
2 角色,是权限的集合,或字符串值表示的一种能力。
3 权限,即操作资源的权利。比如访问某个页面,以及对某功能模块的添加、修改、删除、查看的权利
(http://shiro.apache.org/authorization.html)

二、授权方式

1、编程式授权

基于角色(role)的编程式授权、基于权限(permission)的编程式授权

1.1 基于角色(role)的编程式授权

public class RoleTest {@Testpublic void testHasRole() {Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "zhangsan", "zs123456");System.out.println(currentUser.hasRole("role1")?"有role1这个角色":"没有role1这个角色");boolean []results=currentUser.hasRoles(Arrays.asList("role1","role2","role3"));System.out.println(results[0]?"有role1这个角色":"没有role1这个角色");System.out.println(results[1]?"有role2这个角色":"没有role2这个角色");System.out.println(results[2]?"有role3这个角色":"没有role3这个角色");System.out.println(currentUser.hasAllRoles(Arrays.asList("role1","role2"))?"role1,role2这两个角色都有":"role1,role2这个两个角色不全有");currentUser.logout();}@Testpublic void testCheckRole() {Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "zhangsan","zs123456");currentUser.checkRole("role1");currentUser.checkRoles(Arrays.asList("role1","role2"));currentUser.checkRoles("role1","role2","role3");currentUser.logout();}
}

hasRole()和checkRole()的区别在于,前者是有相应角色则返回布尔值,后者是没有角色则会抛出异常。

1.2 基于权限(permission)的编程式授权

public class PermissionTest {@Testpublic void testIsPermitted() {Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "zhangsan", "zs123456");System.out.println(currentUser.isPermitted("user:select")?"有user:select这个权限":"没有user:select这个权限");System.out.println(currentUser.isPermitted("user:update")?"有user:update这个权限":"没有user:update这个权限");boolean results[]=currentUser.isPermitted("user:select","user:update","user:delete");System.out.println(results[0]?"有user:select这个权限":"没有user:select这个权限");System.out.println(results[1]?"有user:update这个权限":"没有user:update这个权限");System.out.println(results[2]?"有user:delete这个权限":"没有user:delete这个权限");System.out.println(currentUser.isPermittedAll("user:select","user:update")?"有user:select,update这两个权限":"user:select,update这两个权限不全有");currentUser.logout();}@Testpublic void testCheckPermitted() {Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "zhangsan", "zs123456");currentUser.checkPermission("user:select");currentUser.checkPermissions("user:select","user:update","user:delete");currentUser.logout();}
}

isPermmitted()和checkPermitted()的区别在于:前者是若用户没有相应权限则返回布尔值false,而后者则会抛出异常。

2、注解授权

@RequiresAuthentication 要求subjejct已经通过了身份认证
@RequiresGuest 要求当前subject是一个"guest",即subject必须是没有被记住且没有通过身份认证
@RequiresUser 要求当前subject是一个用户,可以是被记住(rememberMe)的,或者是通过了身份认证的
@RequiresRoles 要求当前subject必须拥有相应角色,value可为一个String集合,默认是And关系。可以标记在类和方法上。
@RequiresPermissions 要求当前subject必须拥有相应权限,value可为一个String集合,默认是And关系。可以标记在类和方法上。

@RequiresRoles注解:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresRoles {/*** A single String role name or multiple comma-delimitted role names required in order for the method* invocation to be allowed.*/String[] value();/*** The logical operation for the permission check in case multiple roles are specified. AND is the default* @since 1.1.0*/Logical logical() default Logical.AND;
}

@RequiresPermission注解:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresPermissions {/*** The permission string which will be passed to {@link org.apache.shiro.subject.Subject#isPermitted(String)}* to determine if the user is allowed to invoke the code protected by this annotation.*/String[] value();/*** The logical operation for the permission checks in case multiple roles are specified. AND is the default* @since 1.1.0*/Logical logical() default Logical.AND;}

3、jsp标签授权

3.1 在pom.xml中引入shiro-web.jar

3.2 在jsp页面中引入shiro的自定义标签库

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

3.3 在jsp页面中使用shiro:xxx的标签

<shiro:guest>用户没有被RememberMe,也没有通过身份认证时显示  </shiro:guest><shiro:user> 用户被RememberMe,或者通过了身份认证时显示</shiro:user><shiro:authenticated>用户通过了身份认证时显示 </shiro:authenticated>
<shiro:notAuthenticated>用户没有通过身份认证时显示 </shiro:notAuthenticated><shiro:hasRole>用户拥有相应角色则显示  </shiro:hasRole>
<shiro:lacksRole> 用户缺少相应角色则显示 </shiro:lacksRole>
<shiro:hasAnyRole>用户拥有其中任意一个角色则显示  </shiro:hasAnyRole><shiro:hasPermission> 用户拥有相应权限则显示</shiro:hasPermission>
<shiro:lacksPermission>用户缺少相应权限则显示 </shiro:lacksPermission>

三、授权过程

图片1

Step 1: Application or framework code invokes any of the Subject hasRole*, checkRole*, isPermitted*, or checkPermission* method variants, passing in whatever permission or role representation is required.

Step 2: The Subject instance, typically a DelegatingSubject (or a subclass) delegates to the application’s SecurityManager by calling the securityManager’s nearly identical respective hasRole*, checkRole*, isPermitted*, or checkPermission* method variants (the securityManager implements the org.apache.shiro.authz.Authorizer interface, which defines all Subject-specific authorization methods).

Step 3: The SecurityManager, being a basic ‘umbrella’ component, relays/delegates to its internal org.apache.shiro.authz.Authorizer instance by calling the authorizer’s respective hasRole*, checkRole*, isPermitted*, or checkPermission* method. The authorizer instance is by default a ModularRealmAuthorizer instance, which supports coordinating one or more Realm instances during any authorization operation.

Step 4: Each configured Realm is checked to see if it implements the same Authorizer interface. If so, the Realm’s own respective hasRole*, checkRole*, isPermitted*, or checkPermission* method is called.

四、权限粒度

资源级别
单个权限:query
单个资源多个权限: user:query user:add 多值user:query,add
单个资源所有权限: user:query,user:add,user:update,user:delete 所有user:*
所有资源的某个权限: *:view

实例级别
单个实例的单个权限 printer:query:lp7200 printer:print:epsoncolor
所有实例的单个权限 printer:print:*
所有实例的所有权限 printer::
单个实例的所有权限 printer:*:lp7200
单个实例的多个权限 printer:query,print:lp7200

printer 等价于 printer::
printer:print 等价与 printer:print:*


http://chatgpt.dhexx.cn/article/HyTWoNRT.shtml

相关文章

安全-认证授权、数据脱敏

一、认证授权 JWT &#xff1a;JWT&#xff08;JSON Web Token&#xff09;是一种身份认证的方式&#xff0c;JWT 本质上就一段签名的 JSON 格式的数据。由于它是带有签名的&#xff0c;因此接收者便可以验证它的真实性。 SSO(单点登录) &#xff1a;SSO(Single Sign On) 即单…

认证 (authentication) 和授权 (authorization) 的区别

以前一直傻傻分不清各种网际应用中 authentication 和 authorization, 其实很简单: 这两个术语通常在安全性方面相互结合使用&#xff0c;尤其是在获得对系统的访问权限时。两者都是非常重要的主题&#xff0c;通常与网络相关联&#xff0c;作为其服务基础架构的关键部分。然而…

Authorization—权限控制流程

本篇是对Shiro体系架构的介绍&#xff0c;本栏目大部分内容来自于Shiro官网。翻译过程中已经尽量保证用词的准确性和易懂性&#xff0c;如有不准确或者不确切的地方&#xff0c;请联系作者加以修改。本篇内容翻译自Authorization特征与Authorization官方指南。 Authorization&…

认证 (Authentication) 和授权 (Authorization)的区别是什么?

说简单点就是&#xff1a; 认证 (Authentication)&#xff1a; 你是谁。 授权 (Authorization)&#xff1a; 你有哪些权限 干什么事情。 稍微正式点&#xff08;啰嗦点&#xff09;的说法就是&#xff1a; Authentication&#xff08;认证&#xff09; 是验证您的身份的凭据&a…

变量定义与类型

命名 保留字与关键字 关键字是系统已经用的&#xff0c;保留字是系统自带的的保留 为以后使用做准备 查看关键字方法 import keyword #引入关键字模块 print &#xff08;keyword.kwlist&#xff09; #打印系统全部关键字 变量声明 三种格 #格式1 s1 &#xff02;北京图灵学…

Java变量定义时候的注意事项

常量定义的基本注意事项 在JAVA语言中&#xff0c;主要利用final关键字&#xff0c;&#xff08;在java类中灵活使用Static关键字&#xff09;来定义常量。 当常量被设定后&#xff0c;一般情况下就不允许在进行修改&#xff0c;如可以利用以下形式来定义一个常量:final doubl…

变量的定义和使用

目录 一、变量的定义 二、变量的组成 1.标识 2.类型 3.值 三、变量的多次赋值 一、变量的定义 变量名(name) 赋值运算符() 值(小王) name 小王 二、变量的组成 1.标识 表示对象所存储的物理地址&#xff0c;使用内置函数 id(obj) 来获取。 print(id(name)) 运行…

C语言中变量声明和变量定义的区别

本文转载至CSDN博客JeanCheng 变量声明和变量定义 变量定义&#xff1a;用于为变量分配存储空间&#xff0c;还可为变量指定初始值。程序中&#xff0c;变量有且仅有一个定义。变量声明&#xff1a;用于向程序表明变量的类型和名字。定义也是声明&#xff0c;extern声明不是定义…

C++ 中的变量定义

变量定义就是告诉编译器在何处创建变量的存储&#xff0c;以及如何创建变量的存储。 变量定义指定一个数据类型&#xff0c;并包含了该类型的一个或多个变量的列表&#xff0c;如下所示&#xff1a; type variable_list; 在这里&#xff0c;type 必须是一个有效的 C 数据类型…

十:变量的定义和声明的区别?

1. 变量的声明&#xff1a; 声明是用来告诉编译器变量的名称和类型&#xff0c;而不分配内存。变量的声明有两重含义&#xff1a; 告诉编译器&#xff0c;这个名字已经匹配到一块内存上&#xff0c;下面的代码用到变量或者对象是在别的地方定义的。声明可以出现多次。 告诉编译…

C语言基础教程 之 如何定义变量!

变量定义就是告诉编译器在何处创建变量的存储&#xff0c;以及如何创建变量的存储。变量定义指定一个数据类型&#xff0c;并包含了该类型的一个或多个变量的列表&#xff0c;如下所示&#xff1a; type variable_list; 在这里&#xff0c;type 必须是一个有效的 C 数据类型&…

变量的定义

变量 变量用于存储编程所使用的数据和方法。 声明一般变量的关键字&#xff1a;var,let,const.其中let和const是es6的语法。 声明其他特殊变量的关键字&#xff1a;function,class,improt&#xff08;先了解&#xff09;等 声明变量 变量用于存储数据,因此可以把变量实际上就…

03-变量的定义

一、变量的定义 1.变量是什么&#xff1f; 一句话概括&#xff1a;变量是用来临时保存数据的&#xff0c;该数据是可以变化的数据。 2.什么时候需要定义变量&#xff1f; 如果某个内容需要多次使用&#xff0c;并且在代码中重复出现&#xff0c;那么可以用变量代表该内容。…

【论文阅读】ICRA2021: VDB-EDT An Efficient Euclidean Distance Transform Algorithm Based on VDB Data Struct

参考与前言 Summary: 浩哥推荐的一篇 无人机下的建图 and planning实验 Type: ICRA Year: 2021 论文链接&#xff1a;https://arxiv.org/abs/2105.04419 youtube presentation video&#xff1a;https://youtu.be/Bojh6ylYUOo 代码链接&#xff1a;https://github.com/zhud…

scipy.ndimage.distance_transform_edt 和 cv2.distanceTransform用法

scipy.ndimage.distance_transform_edt 和 cv2.distanceTransform 的作用都是计算一张图上每个前景像素点到背景的最近距离。 import cv2 import numpy as np from scipy.ndimage import distance_transform_edta np.array(([0, 1, 1, 1, 1],[0, 0, 1, 1, 1],[0, 1, 1, 1, 1]…

java edt,java并发之EDT测试

测试代码如下&#xff1a; 1、耗时计算没有单独起线程处理&#xff0c;耗时计算在EDT线程执行&#xff0c;导致界面没有响应&#xff0c;处于卡死状态 package thread; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.concurrent…

linux服务端修改EDT为东八区,EDT开发环境配置

1 安装条件 512MB内存或更高 Win XP/Win Vista/Win 7/RedHat Linux 32位或者64位操作系统(推荐32位) 安装IE7/8/9、FireFoxLatest Version、Chrome等浏览器中的一种 OracleJRE 1.6或更高版本 2 安装步骤 EDT 0.8.0已经发布发布。用户现在可以在http://www.eclipse.org/edt/#d…

修改linux系统的时间EDT为CST

问题&#xff1a; Centos 系统时间下午时间显示为12小时制 分析&#xff1a; 开始以为是要设置为24小时制 后来执行date命令发现是EDT&#xff0c;EDT 是北美东部夏令时间&#xff0c;比UTC落后4个小时 解决&#xff1a; # mv /etc/localtime /etc/localtime.bak # ln -s …