1. shiro默认自带的realm和常见使用方法

  • realm作用:Shiro 从 Realm 获取安全数据
  • 默认自带的realm:idae查看realm继承关系,有默认实现和自定义继承的realm
  • 两个概念
    • principal : 主体的标示,可以有多个,但是需要具有唯一性,常见的有用户名,手机号,邮箱等
    • credential:凭证, 一般就是密码
    • 所以一般我们说 principal + credential 就账号 + 密码
  • 开发中,往往是自定义realm , 即集成 AuthorizingRealm,重写AuthorizingRealm的getAuthorizationInfo方法

2.  Shiro内置 ini realm 操作

在springboot的resources资源目录下创建shiro.ini 配置文件,将下面内容复制进去

 [users]
# 格式 name=password,role1,role2,..roleN
jack = 456, user
xdclass = 123, root, admin
# 格式 role=permission1,permission2...permissionN 也可以用通配符
# 下面配置user的权限为所有video:find,video:buy,如果需要配置video全部操作crud 则 user = video:*
[roles]
user = video:find,video:buy
# 下面定义了游客角色具有商品模块的查询,购买权限以及评论模块的所有权限
visitor= good:find,good:buy,comment:*
# 'admin' role has all permissions, indicated by the wildcard '*'
admin = *
xdclass = 123, root, admin  表示,xdclass这个用户密码是123,具有 root 和admin两个角色,
user = video:find,video:buy 表示,普通用户角色具有视频的查看,购买权限,
admin = *  表示admin角色具有所有的权限

测试代码:

 package net.xdclass.xdclassshiro;

 import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Before;
import org.junit.Test; /**
* iniRealm操作
*/
public class QuicksStratTest5_2 { @Test
public void testAuthentication() {
//通过配置文件创建SecurityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
// 获取SecurityManager实例
SecurityManager securityManager = factory.getInstance();
//设置当前上下文
SecurityUtils.setSecurityManager(securityManager); //获取当前subject(application应用的user)
Subject subject = SecurityUtils.getSubject();
// 模拟用户输入
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jack","456");
//
subject.login(usernamePasswordToken);
System.out.println("认证结果(是否已授权):" + subject.isAuthenticated()); //认证结果(是否已授权):true
//最终调用的是org.apache.shiro.authz.ModularRealmAuthorizer.hasRole方法
System.out.println("是否有对应的角色:" + subject.hasRole("root")); //是否有对应的角色:false
//获取登录 账号
System.out.println("getPrincipal():" + subject.getPrincipal()); //getPrincipal():jack
//校验角色,没有返回值,校验不通过,直接跑出异常
subject.checkRole("user");
// user jack有video的find权限,执行通过
subject.checkPermission("video:find");
// 是否有video:find权限:true
System.out.println("是否有video:find权限:" + subject.isPermitted("video:find"));
// 是否有video:delete权限:false
System.out.println("是否有video:delete权限:" + subject.isPermitted("video:delete"));
//user jack没有video的删除权限,执行会报错:org.apache.shiro.authz.UnauthorizedException: Subject does not have permission [video:delete]
subject.checkPermission("video:delete");
// subject.logout();
// System.out.println("logout后认证结果:" + subject.isAuthenticated());
} @Test
public void testAuthentication2() {
//通过配置文件创建SecurityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
// 获取SecurityManager实例
SecurityManager securityManager = factory.getInstance();
//设置当前上下文
SecurityUtils.setSecurityManager(securityManager);
//获取当前subject(application应用的user)
Subject subject = SecurityUtils.getSubject();
// 模拟用户输入
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("xdclass","123");
subject.login(usernamePasswordToken);
System.out.println("认证结果(是否已授权):" + subject.isAuthenticated()); // 认证结果(是否已授权):true
//最终调用的是org.apache.shiro.authz.ModularRealmAuthorizer.hasRole方法
System.out.println("是否有admin角色:" + subject.hasRole("admin")); //是否有admin角色:true
System.out.println("是否有root角色:" + subject.hasRole("root")); //是否有root角色:true
//获取登录 账号
System.out.println("getPrincipal():" + subject.getPrincipal()); //getPrincipal():xdclass
// admin角色具有所有权限
subject.checkPermission("video:find");
// 是否有video:find权限:true
System.out.println("是否有video:find权限:" + subject.isPermitted("video:find")); //是否有video:find权限:true
// 是否有video:delete权限:true
System.out.println("是否有video:delete权限:" + subject.isPermitted("video:delete")); // 是否有video:find权限:true
// 结果为true,如果subject.checkPermission校验不通过,则抛出异常
subject.checkPermission("video:delete");
}
}

shiro框架学习-3- Shiro内置realm的更多相关文章

  1. shiro框架学习-6-Shiro内置的Filter过滤器及数据加解密

    1.  shiro的核心过滤器定义在枚举类DefaultFilter 中,一共有11个 ,配置哪个路径对应哪个拦截器进行处理 // // Source code recreated from a .c ...

  2. shiro框架学习-5-自定义Realm

    1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...

  3. shiro框架学习-1-shiro基本概念

    1. 什么是权限控制 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能访问自己被授权的资源, ...

  4. shiro基础学习(二)—shiro认证

    一.shiro简介      shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证.权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架. 以下 ...

  5. Python学习第二阶段day1 内置函数,序列化,软件目录开发规范

    内置函数 1.abs()  求绝对值 2.all()    所有元素为真才返回真 all( [1,1,2,3,-1] ) 值为True 3.any()   所有元素为假才返回假  any([0,0,0 ...

  6. shiro基础学习(四)—shiro与项目整合

    一.认证 1.配置web.xml   2.配置applicationContext.xml      在applicationContext.xml中配置一个bean,ID和上面的过滤器的名称一致. ...

  7. python自动化测试学习笔记-4内置函数,处理json

    函数.全局变量 写代码时注意的几点事项: 1.一般写代码的时候尽量少用或不用全局变量,首先全局变量不安全,大家协作的情况下,代码公用容易被篡改,其次全局变量会一直占用系统内容. 2.函数里如果有多个r ...

  8. js学习---常用的内置对象(API)小结 :

    内置对象(API): 日期 Date: getFullYear() 返回完整的4位的年份  如:2016 getMonth()    返回月份,从0开始 getDate()   返回当前月的第几天,当 ...

  9. 学习笔记——Maven 内置变量

    Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...

随机推荐

  1. python--008文件处理

    一.文件操作 1.打开文件,获得文件句柄,并将句柄赋值给一个变量 2.通过句柄对文件操作 3.关闭文件 f=open('sg',encoding='utf-8') da=f.read() print( ...

  2. DJANGO MODEL FORMSETS IN DETAIL AND THEIR ADVANCED USAGE

    Similar to the regular formsets, Django also provides model formset that makes it easy to work with ...

  3. 【FICO系列】SAP FICO折旧记账时出现错误:没有找到与所做选择一致的数据

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP FICO折旧记账时出现错 ...

  4. Unity Shader 基础

    推荐: https://www.cnblogs.com/nanwei/p/7277417.html 上面链接作者的整个系列都写的不错 https://www.cnblogs.com/nanwei/ca ...

  5. GCC+Make 自动生成 Makefile 依赖

    目录 BASIS wildcard .PHONY 静态模式 常用自动变量 自动生成依赖(GCC) -M 参数 编写 Makefile Makefile 细节说明 其他 本文内容基于 GNU MAKE. ...

  6. python 爬取网页内的代理服务器列表(需调整优化)

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-30 20:38:23 # @Author : EnderZhou (z ...

  7. LOJ167 康托展开 题解

    题面 康托展开: 康托展开是一个全排列到一个自然数的双射,常用于构建哈希表时的空间压缩. 康托展开的实质是计算当前排列在所有由小到大全排列中的名次,因此是可逆的. X = A[0] * (n-1)! ...

  8. 赛道修建 NOIP 2018 d1t3

    题目大意 最小值最大 考虑二分 二分答案 判断能不能构成m条路径 很明显满足单调性 可行 思考如何判断 对于一个节点 它的儿子会传上来一些路径 这些路径只有三种处理方式 一.传上去(只能传一条) 二. ...

  9. yii框架製作簡易RBAC權限管理

    控制器源碼 <?php namespace app\controllers; use yii; use yii\web\Controller; class PowerController ext ...

  10. Linux的环境变量.bash_profile .bashrc profile文件

    Shell变量有局部变量.环境变量之分.局部变量就是指在某个Shell中生效的变量,只在此次登录中有效.环境变量通常又称“全局变量”,虽然在Shell中变量默认就是全局的,但是为了让子Shall继承当 ...