下面内容是在看了涛哥的《跟我一起学shiro》 和 视频《一头扎入进shiro》 后整理出来备忘和方便自己和其它人学习。

个人主页:http://www.itit123.cn/ 很多其它干货等你来拿

授权相关概念了解

权限认证:什么样的用户拥有什么样的权限做什么样的事。

三要素:权限,角色,用户。

角色:权限的集合。一个角色能够拥有多个权限

用户:角色的集合。一个用户能够拥有多个角色。也就是Subject

上代码:

为了方便函数的调用,将用户登录代码封装一下:

package com.shiro.utils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory; public class ShiroUtil { public static Subject login(String config , String userName ,String password){
// 1.读取配置文件,初始化SecurityManager工厂
Factory<SecurityManager> factory=new IniSecurityManagerFactory(config);
// 2.获取securityManager实例
SecurityManager securityManager=factory.getInstance();
// 3.把securityManager实例绑定到SecurityUtils
SecurityUtils.setSecurityManager(securityManager);
// 4.获取当前运行的用户
Subject currentUser=SecurityUtils.getSubject();
// 5.创建token令牌。username/密码
UsernamePasswordToken token=new UsernamePasswordToken(userName, password);
try{
// 6.登录时认证身份
currentUser.login(token);
System.out.println("身份认证成功! ");
}catch(AuthenticationException e){
e.printStackTrace();
System.out.println("身份认证失败!");
}
return currentUser;
} }

基于角色的訪问控制

在之前的基础上新建shiro_role.ini

[users]
ITDragon=123456,role1,role2
other=123456,role1
another=123456,role2

单元測试类:

hasRole拥有什么权限,checkRole检查有什么权限

// 基于角色
@Test
public void hasRoleTest(){
Subject user = ShiroUtil.login("classpath:shiro_role.ini", "other", "123456");
System.out.println(user.hasRole("role1")? "是role1角色":"不是role1角色");
// 測试时不能同一时候存在。由于另外一个还没有退出
//Subject user2 = ShiroUtil.login("classpath:shiro_role.ini", "ITDragon", "123456");
boolean[] result = user.hasRoles(Arrays.asList("role1","role2"));
for (boolean role : result) {
System.out.println(role);
}
System.out.println(user.hasAllRoles(Arrays.asList("role1","role2"))?"都拥有role1。role2角色":"不全拥有role1,role2角色");
user.logout();
} @Test
public void checkRoleTest(){
Subject user = ShiroUtil.login("classpath:shiro_role.ini", "ITDragon", "123456");
// 验证不通过报错
user.checkRole("role1");
user.checkRoles(Arrays.asList("role1","role2"));
user.checkRoles("role1","role2");
user.logout();
}

基于权限的訪问控制:

[users]
ITDragon=123456,role1,role2
other=123456,role1
another=123456,role2
[roles]
role1=user:select
role2=user:update,user:delete

单元測试类:

// 基于权限
@Test
public void isPermittedTest(){
Subject user = ShiroUtil.login("classpath:shiro_permission.ini", "other", "123456");
System.out.println(user.isPermitted("user:select")?"拥有select权限":"没有select权限");
boolean[] result = user.isPermitted("user:select","user:update","user:delete");
for (boolean role : result) {
System.out.println(role);
}
System.out.println(user.isPermittedAll("user:select","user:update")?"都拥有select,updata权限":"不全拥有select,updata权限");
user.logout();
} @Test
public void checkPermittedTest(){
Subject user = ShiroUtil.login("classpath:shiro_permission.ini", "other", "123456");
user.checkPermission("user:select");
user.checkPermissions("user:select","user:update","user:delete");
user.logout();
}

犯的错误:

在一个測试方法中登入了两个用户,并没有做用户退出操作。影响:权限推断错误。

Apache shiro 笔记整理之编程式授权的更多相关文章

  1. Apache shiro 笔记整理之web整合一

    下面内容是在看了涛哥的<跟我一起学shiro> 和 视频<一头扎入进shiro> 后整理出来备忘和方便自己和其它人学习. 个人主页:http://www.itit123.cn/ ...

  2. Shiro基础知识03----shiro授权(编程式授权),Permission详解,授权流程(zz)

    授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).  在权限认证中,最核心的是:主体/用户(Subject).权限(Permission).角色(Role).资源 ...

  3. Shiro-权限认证(授权)-编程式授权

    权限认证 权限认证也就是访问控制,即在应用中控制谁能访问哪些资源 权限认证核心要素 权限 : 即操作资源的权利,比如访问某个页面,以及对某个模块的数据的添加,修改,删除,查看的权利 角色 : 是权限的 ...

  4. 使用shiro做权限管理的学习笔记整理

    Shiro权限管理 参考:https://www.cnblogs.com/jpfss/p/8352031.html Shiro解决的问题 授权和鉴别的问题:Authenrization(授权) Aut ...

  5. apache shiro内置过滤器 标签 注解

    内置过滤器 anon(匿名)  org.apache.shiro.web.filter.authc.AnonymousFilter authc(身份验证)       org.apache.shiro ...

  6. Shiro笔记(三)授权

    Shiro笔记(三)授权 一.授权方式 1.编程式: Subject subject=SecurityUtils.getSubject(); if(subject.hasRole("root ...

  7. Apache Shiro安全(权限框架)学习笔记二

    课程目标 通过学习本课程掌握权限管理的设计思想及方法,使用Shiro框架完成权限管理功能开发. 1.  理解基于资源的权限管理方法. 2.  掌握权限管理的数据模型. 3.  掌握不使用shiro开发 ...

  8. Apache Shiro安全(权限框架)学习笔记一

    1. 授权需要继承 AuthorizingRealm 类, 并实现其 doGetAuthorizationInfo 方法 2. AuthorizingRealm 类继承自 Authenticating ...

  9. Apache Shiro 使用手册(三)Shiro 授权

    授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限,以及是否拥有打印的权限等等. 一.授权的三要素 授权有着三 ...

随机推荐

  1. Linux内核project导论——网络:Netfilter概览

    简单介绍 最早的内核包过滤机制是ipfwadm.后来是ipchains.再后来就是iptables/netfilter了. 再往后,也就是如今是nftables. 只是nftables与iptable ...

  2. JavaScript的那些坑之变量提升

    想总结一下JS的变量提升特性,都是由于一道题.先上题. var name = 'World!'; (function () { if (typeof name === 'undefined') { v ...

  3. 石子合并(区间dp)

    石子合并(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述     有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程仅仅能每次将相邻 ...

  4. 项目部署在windows下的tomcat里

    打包放在webapps 目录下,web的改成ROOT ok!!!

  5. java.lang.NoClassDefFoundError: javax/wsdl/extensions/ElementExtensible

    转自:https://blog.csdn.net/zt13258579889/article/details/82688723 严重: Context initialization failed or ...

  6. SQL Server 获取两个日期间的日期

    declare @start datetime declare @end datetime set @start = '2018-01-25' set @end = '2018-02-03' sele ...

  7. 洛谷P4015 运输问题(费用流)

    题目描述 WW 公司有 mm 个仓库和 nn 个零售商店.第 ii 个仓库有 a_iai​ 个单位的货物:第 jj 个零售商店需要 b_jbj​ 个单位的货物. 货物供需平衡,即\sum\limits ...

  8. 订购一套Arduino UNO r3入门套件

    若需要arduino套件经济版请点击以下链接跳转: http://item.taobao.com/item.htm?id=36759198826 这就开始了吗?希望有所收获吧-!

  9. 【AnjularJS系列3 】 — 数据的双向绑定

    第三篇,双向的数据绑定 数据绑定是AnguarJS的特性之一,避免书写大量的初始代码从而节约开发时间 数据绑定指令提供了你的Model投射到view的方法.这些投射可以无缝的,毫不影响的应用到web应 ...

  10. 页面定制CSS代码初探(四):cnblogs使用Github引用样式

    前言 对于用惯了Github的人来说,眼里的引用应该是这样的 "Talk is cheap. Show me the code" -- Linus Torvalds 然而实际上cn ...