【授权概念】

访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等)。

在授权中需了解的几个关键对象:主体(Subject)、资源(Resource)、权限(Permission)、角色(Role)。

授权的前提是认证通过。

(简单说,我只有进系统了我才能知道我可以做什么,系统都进不了怎么授权)

【关键对象】

1,主体

主体,即访问应用的用户,在Shiro中使用Subject代表该用户。

用户只有授权后才允许访问相应的资源。

2,资源

在应用中用户可以访问的任何东西,

比如访问JSP 页面、查看/编辑某些数据、访问某个业务方法、打印文本等等都是资源。

用户只要授权后才能访问。

3,权限

安全策略中的原子授权单位,通过权限我们可以表示在应用中用户有没有操作某个资源的权力。

即权限表示在应用中用户能不能访问某个资源,

如:访问用户列表页面查看/新增/修改/删除用户数据(即很多时候都是CRUD(增查改删)式权限控制)打印文档等等。。。

4,角色

角色代表了操作集合,可以理解为权限的集合,

一般情况下我们会赋予用户角色而不是权限,即这样用户可以拥有一组权限,赋予权限时比较方便。

典型的如:项目经理、技术总监、CTO、开发工程师等都是角色,不同的角色拥有一组不同的权限。

【授权流程】

【相关方法】

1 subject.hasRole("");
判断是否有角色 2 subject.hashRoles(List);
分别判断用户是否具有List中每个内容 3 subject.hasAllRoles(Collection);
返回boolean,要求参数中所有角色用户都需要具有. 4 subject.isPermitted("");
判断是否具有权限.

【shiro.ini配置】

#配置用户
[users]
admin=123456,role1
user01=123456,role2
user02=123456,role3
user03=123456,role2,role3 #声明角色
[roles]
role1=user:query,user:add,user:update,user:delete,user:export
role2=user:query,user:add
role3=user:query,user:export

测试类编写:

    // 日志输出工具
private static final transient Logger log = LoggerFactory.getLogger(AuthenticationTest.class); @SuppressWarnings("deprecation")
public static void main(String[] args) {
String username = "admin";
String password = "123456";
log.info("My First Apache Shiro Application"); DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(new IniRealm("classpath:shiro.ini"));
SecurityUtils.setSecurityManager(defaultSecurityManager); Subject subject = SecurityUtils.getSubject(); try {
subject.login(new UsernamePasswordToken(username, password));
log.info("登陆成功");
} catch (AuthenticationException authenticationException) {
authenticationException.printStackTrace();
log.error("用户名称或者密码不符合");
} // 是否认证通过
boolean authenticated = subject.isAuthenticated();
log.info("是否认证通过:"+authenticated); // 角色验证
boolean hasRole1 = subject.hasRole("role1");
log.info("是否有role1的角色:"+hasRole1); //分别判断集合里面的角色 返回数组
List<String> roleIdentifiers= Arrays.asList("role1","role2","role3");
boolean[] hasRoles = subject.hasRoles(roleIdentifiers);
for (boolean hasRole : hasRoles) {
log.info(String.valueOf(hasRole));
} //判断当前用户是否有roleIdentifiers集合里面的所有角色
boolean hasAllRoles = subject.hasAllRoles(roleIdentifiers);
log.info(String.valueOf(hasAllRoles)); // ---------------------------------------------------------- //权限判断
boolean permitted = subject.isPermitted("user:query");
log.info("判断当前用户是否有user:query的权限 "+permitted); boolean[] permitted2 = subject.isPermitted("user:query","user:add","user:export");
for (boolean b : permitted2) {
log.info(String.valueOf(b));
} boolean permittedAll = subject.isPermittedAll("user:query","user:add","user:export");
log.info(String.valueOf(permittedAll)); }

测试结果:

[main] INFO AuthenticationTest - My First Apache Shiro Application
[main] INFO org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...
[main] INFO AuthenticationTest - 登陆成功
[main] INFO AuthenticationTest - 是否认证通过:true
[main] INFO AuthenticationTest - 是否有role1的角色:true
[main] INFO AuthenticationTest - true
[main] INFO AuthenticationTest - false
[main] INFO AuthenticationTest - false
[main] INFO AuthenticationTest - false
[main] INFO AuthenticationTest - 判断当前用户是否有user:query的权限 true
[main] INFO AuthenticationTest - true
[main] INFO AuthenticationTest - true
[main] INFO AuthenticationTest - true
[main] INFO AuthenticationTest - true Process finished with exit code 0

【Shiro】04 ini授权实现的更多相关文章

  1. Apache shiro集群实现 (二) shiro 的INI配置

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  2. Shiro中的授权问题(二)

    上篇博客(Shiro中的授权问题 )我们介绍了Shiro中最最基本的授权问题,以及常见的权限字符的匹配问题.但是这里边还有许多细节需要我们继续介绍,本节我们就来看看Shiro中授权的一些细节问题. 验 ...

  3. Shiro中的授权问题

    在初识Shiro一文中,我们对Shiro的基本使用已经做了简单的介绍,不懂的小伙伴们可以先阅读上文,今天我们就来看看Shiro中的授权问题. Shiro中的授权,大体上可以分为两大类,一类是隐式角色, ...

  4. 【shiro】(4)---Shiro认证、授权案例讲解

    Shiro认证.授权案例讲解 一.认证  1. 认证流程     2.用户密码已经加密.加盐的用户认证 (1)测试类 // 用户登陆和退出,这里我自定了一个realm(开发肯定需要自定义realm获取 ...

  5. shiro入门学习--授权(Authorization)|筑基初期

    写在前面 经过前面的学习,我们了解了shiro中的认证流程,并且学会了如何通过自定义Realm实现应用程序的用户认证.在这篇文章当中,我们将学习shiro中的授权流程. 授权概述 这里的授权指的是授予 ...

  6. 第四章:shiro的INI配置

    4.1 根对象SecurityManager 从之前的Shiro架构图可以看出,Shiro是从根对象SecurityManager进行身份验证和授权的:也就是所有操作都是自它开始的,这个对象是线程安全 ...

  7. shiro中INI配置

    4.1 根对象SecurityManager 从之前的Shiro架构图可以看出,Shiro是从根对象SecurityManager进行身份验证和授权的:也就是所有操作都是自它开始的,这个对象是线程安全 ...

  8. Shiro身份认证授权原理

    shiro在应用程序中的使用是用Subject为入口的, 最终subject委托给真正的管理者ShiroSecurityMannager Realm是Shiro获得身份认证信息和来源信息的地方(所以这 ...

  9. Apache Shiro 标签方式授权

    Shiro提供了一套JSP标签库来实现页面级的授权控制. 在使用Shiro标签库前,首先需要在JSP引入shiro标签: <%@ taglib prefix="shiro"  ...

  10. shiro源码篇 - shiro认证与授权,你值得拥有

    前言 开心一刻 我和儿子有个共同的心愿,出国旅游.昨天儿子考试得了全班第一,我跟媳妇合计着带他出国见见世面,吃晚饭的时候,一家人开始了讨论这个.我:“儿子,你的心愿是什么?”,儿子:“吃汉堡包”,我: ...

随机推荐

  1. Android Media Framework(三)OpenMAX API阅读与分析

    这篇文章我们将聚焦Control API的功能与用法,为实现OMX Core.Component打下坚实的基础. 1.OMX_Core.h OMX Core在OpenMAX IL架构中的位置位于IL ...

  2. 对pta的总结_1

    前言 这三次pta难度在不断上升的同时,要求我们线上慕课+自主学习来了解更多的java中的各种方法,如:正则表达式 List Map等.与此同时要求我们展开尝试并熟练类的构造,类的引用,链表的基本运用 ...

  3. m3u8文件转换mp4 ffmpeg

    m3u8文件转换mp4 ffmpeg 命令行执行下面语句: ffmpeg -i input.m3u8 -c copy output.mp4 ffmpeg.exe 和 input.m3u8 放在同一目录 ...

  4. Spring源码——详细流程图(超详细)

    Spring源码流程图

  5. Vue微前端架构与Qiankun实践理论指南

    title: Vue微前端架构与Qiankun实践理论指南 date: 2024/6/15 updated: 2024/6/15 author: cmdragon excerpt: 这篇文章介绍了微前 ...

  6. .NET 中使用RabbitMQ初体验

    在.NET Core中使用RabbitMQ 前言 逛园子的时候看到一篇.NET 学习RabbitMq的文章(视频地址和文章地址放在文章底部了),写的不错,我也来实现一下. 我是把RabbitMQ放在服 ...

  7. markdown语法支持测试

    latex 公式 \(v, w, \nu, \omega\) \[\iiint, \oiiint \] \(\Set{ x | x<\frac 1 2 }\) \(\displaystyle \ ...

  8. 【论文阅读】Learning to drive from a world on rails

    引用与参考 代码地址:https://github.com/dotchen/WorldOnRails 论文地址:https://arxiv.org/abs/2105.00636 论文部分 已看完 写在 ...

  9. 3568F-Linux应用开发手册

       

  10. 使用jsp+servlet+mysql用户管理系统之用户注册-----------使用简单三层结构分析页面显示层(view),业务逻辑层(service),数据持久层(dao)

    View层:jsp+servlet: jsp: <%@ page language="java" contentType="text/html; charset=U ...