权限认证

权限认证也就是访问控制,即在应用中控制谁能访问哪些资源

权限认证核心要素

  • 权限 : 即操作资源的权利,比如访问某个页面,以及对某个模块的数据的添加,修改,删除,查看的权利
  • 角色 : 是权限的集合,一种角色可以包含多种权限
  • 用户 : 在 Shiro 中,代表访问系统的用户,即Subject

授权方式

  • 编程式授权

    • 基于角色的访问控制
    • 基于权限的访问控制
  • 注解式授权
  • Jsp 标签授权

编程式授权实现

抽取公共代码生成 ShiroUtil

package com.zhen.common;

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 configFile,String userName,String password){
//读取配置文件,初始化SecurityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory(configFile);
//获取securityManager实例
SecurityManager securityManager = factory.getInstance();
//把securityManager绑定到SecurityUtils
SecurityUtils.setSecurityManager(securityManager);
//获取当前用户
Subject currentUser = SecurityUtils.getSubject();
//创建token令牌,用户名/密码
UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
try {
//身份认证
currentUser.login(token);
System.out.println("身份认证成功!");
} catch (AuthenticationException e) {
e.printStackTrace();
System.out.println("身份认证失败!");
} return currentUser;
} }

  

基于角色的访问控制
  • 新建 shiro_role.ini文件,两个用户,两种角色

    [users]
    zhen=123,role1,role2
    jack=jack,role1
  • 新建测试类
    package com.zhen.shiro;
    
    import java.util.ArrayList;
    import java.util.List;
    import org.apache.shiro.subject.Subject;
    import org.junit.Test;
    import com.zhen.common.ShiroUtil;
    import junit.framework.TestCase; //基于角色的
    public class RoleTest extends TestCase { @Test
    public void testHasRole(){
    String configFile = "classpath:shiro_role.ini";
    String userName = "jack";
    String password = "jack";
    Subject currentUser = ShiroUtil.login(configFile, userName, password);
    if (currentUser.hasRole("role2")) {
    System.out.println(userName+"有 role2 权限");
    }else{
    System.out.println(userName+"没有 role2 权限");
    }
    currentUser.logout();
    } @Test
    public void testHasRoles(){
    String configFile = "classpath:shiro_role.ini";
    String userName = "jack";
    String password = "jack";
    Subject currentUser = ShiroUtil.login(configFile, userName, password);
    List<String> roles = new ArrayList<String>();
    roles.add("role1");
    roles.add("role2"); //返回一个boolean数组
    boolean[] results = currentUser.hasRoles(roles);
    for (int i = 0; i < results.length; i++) {
    if(results[i]){
    System.out.println(userName+"有 "+roles.get(i)+" 权限");
    }else{
    System.out.println(userName+"没有 "+roles.get(i)+" 权限");
    }
    }
    currentUser.logout();
    } @Test
    public void testHasAllRoles(){
    String configFile = "classpath:shiro_role.ini";
    String userName = "zhen";
    String password = "123";
    Subject currentUser = ShiroUtil.login(configFile, userName, password);
    List<String> roles = new ArrayList<String>();
    roles.add("role1");
    roles.add("role2"); //是否拥有所有权限
    boolean result = currentUser.hasAllRoles(roles);
    if(result){
    System.out.println(userName+"有 所有权限");
    }else{
    System.out.println(userName+"没有 所有权限");
    }
    currentUser.logout();
    } @Test
    public void testCheckRoles(){
    //check 没有返回值,没有该权限的话就会抛异常
    String configFile = "classpath:shiro_role.ini";
    String userName = "jack";
    String password = "jack";
    Subject currentUser = ShiroUtil.login(configFile, userName, password);
    List<String> roles = new ArrayList<String>();
    roles.add("role1");
    roles.add("role2");
    currentUser.checkRole(roles.get(1));
    currentUser.logout();
    } }
基于权限的访问控制
  • 新建 Shiro_permission.ini文件,内容如下:

    [users]
    zhen=123,role1,role2
    jack=jack,role1
    [roles]
    role1=user:select
    role2=user:add,user:update,user:delete

    role1 对应有 user:select 权限
    role2 对应有 user:add , user:update , user:delete 权限

  • 新建测试类,代码如下:
    package com.zhen.shiro;
    
    import org.apache.shiro.subject.Subject;
    import org.junit.Test; import com.zhen.common.ShiroUtil; import junit.framework.TestCase; //基于权限的
    public class PermissionTest extends TestCase { @Test
    public void testIsPermission(){
    String configFile = "classpath:shiro_permission.ini";
    String userName = "zhen";
    String password = "123";
    Subject currentUser = ShiroUtil.login(configFile, userName, password);
    System.out.println(currentUser.isPermitted("user:add")?"有add权限":"没有add权限");
    System.out.println(currentUser.isPermitted("user:select")?"有select权限":"没有select权限");
    boolean[] results = currentUser.isPermitted("user:add","user:select");
    System.out.println(results[0]?"有add权限":"没有add权限");
    System.out.println(results[1]?"有select权限":"没有select权限");
    System.out.println(currentUser.isPermittedAll("user:add","user:select")?"有user:add&user:select权限":"user:add&user:select权限不全有");
    currentUser.logout();
    } @Test
    public void testCheckPermission(){
    String configFile = "classpath:shiro_permission.ini";
    String userName = "zhen";
    String password = "123";
    Subject currentUser = ShiroUtil.login(configFile, userName, password);
    currentUser.checkPermission("user:add");
    currentUser.checkPermission("user:select");
    currentUser.checkPermissions("user:add","user:select");
    currentUser.logout();
    } }

      

Shiro-权限认证(授权)-编程式授权的更多相关文章

  1. Apache shiro 笔记整理之编程式授权

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

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

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

  3. Shiro入门之一 -------- Shiro权限认证与授权

    一  将Shirojar包导入web项目 二 在web.xml中配置shiro代理过滤器 注意: 该过滤器需要配置在struts2过滤器之前 <!-- 配置Shiro的代理过滤器 -->  ...

  4. shiro权限认证与授权

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

  5. (转)shiro权限框架详解05-shiro授权

    http://blog.csdn.net/facekbook/article/details/54910606 本文介绍 授权流程 授权方式 授权测试 自定义授权realm 授权流程 开始构造Secu ...

  6. 学习Spring Boot:(十三)配置 Shiro 权限认证

    经过前面学习 Apache Shiro ,现在结合 Spring Boot 使用在项目里,进行相关配置. 正文 添加依赖 在 pom.xml 文件中添加 shiro-spring 的依赖: <d ...

  7. shiro权限认证Realm的四大用法

    一.SimpleAccountRealm public class AuthenticationTest {          SimpleAccountRealm sar=new SimpleAcc ...

  8. springboot+mybatis+shiro——登录认证和权限控制

    转载:https://z77z.oschina.io/ 一.引入依赖 shiro-all包含shiro所有的包.shiro-core是核心包.shiro-web是与web整合.shiro-spring ...

  9. spring-boot整合shiro作权限认证

    spring-shiro属于轻量级权限框架,即使spring-security更新换代,市场上大多数企业还是选择shiro 废话不多说  引入pom文件 <!--shiro集成spring--& ...

随机推荐

  1. Linux基础ls命令

    ls 命令是linux下最常用的命令,通过ls 命令不仅可以查看linux文件夹包含的文件而且可以查看文件权限(包括目录.文件夹.文件权限)查看目录信息等等.ls 命令在日常的linux操作中用的 ...

  2. u3D大场景的优化

    首先介绍下draw call(这个东西越少你的游戏跑的越快): 在游戏中每一个被展示的独立的部分都被放在了一个特别的包中,我们称之为“描绘指令”(draw call),然后这个包传递到3D部分在屏幕上 ...

  3. /var/log目录下的Linux日志文件功能详解_转

    摘自:http://www.niaoyun.com/help/application/386.html 学习linux应该知道日志文件的所在位置以及它们包含的内容,在系统运行正常的情况下学习了解这些不 ...

  4. IIS 实现一个主机部署多个网站 共享80端口

    如果一个主机只是建立一个80端口的网站就有点浪费了,通过本文你就可以实现,在一个主机上建立多个80端口的站点,并通过不同的域名进行访问. 打开iis软件:控制面板-->管理工具-->Int ...

  5. es6 初始化样式加载到head中

    Demo1:function loadCss(css) { css.forEach((path)=>{ console.log($('<link/>', { rel: 'styles ...

  6. 【SQLServer2008】之Telnet以及1433端口设置

    Telnet步骤: 一.首先进入Win7控制面板,可以从开始里找到或者在桌面上找到计算机,点击进入里面也可以找到控制面板,如下图: 二.进入控制面板后,我们再找到“程序和功能”并点击进入,如下图所示: ...

  7. 微服务网关哪家强?一文看懂Zuul, Nginx, Spring Cloud, Linkerd性能差异

      导语:API Gateway是实现微服务重要的组件之一.面对诸多的开源API Gateway,如何进行选择也是架构师需要关注的焦点.本文作者对几个较大的开源API Gateway进行了压力测试,对 ...

  8. python入门课程 第一章 课程介绍

    1-1 Python入门课程介绍特点:    优雅.明确.简单适合领域:    web网站和各种网络服务    系统工具和脚本    作为"胶水"语言把其他语言开发的模块包装起来方 ...

  9. 7月份计划-----dream

    梦想还是要有的,万一实现了呢? 数学 150[total] 专业课 150[total] 英语 100[total] 政治 100[total] 第一轮复习计划开始执行 1.专业课: 通过课件把所有的 ...

  10. poj1861 最小生成树 prim &amp; kruskal

    // poj1861 最小生成树 prim & kruskal // // 一个水题,为的仅仅是回味一下模板.日后好有个照顾不是 #include <cstdio> #includ ...