什么是Shiro?

Apache Shiro 是Java的一个权限安全框架

一些功能:认证、授权、加密、会话管理、与Web 集成、缓存等
 
Shiro官网地址:[ 点击访问 ]
http://shiro.apache.org/

4个主要功能:

- Authentication 

  登陆,身份认证。完成用户登陆的密码匹配

- Authorization

  授权,权限验证。判断一个请求和一些事件触发是否可以被允许

- Session Management

  会话管理

- Cryptography

  信息加密,对密码的安全加密处理

其他功能:

WebSupport,Web支持,集成JavaEE

Concurrency,高并发支持,多线程情况下的授权和认证

Testing,测试

Caching,缓存模块

RunAs,让已经登陆的用户以其他用户身份操作管理

RememberMe,记住我

Hello Shiro?

演示HelloShiro需要的一些核心组件

导入工程lib目录

导入Shiro配置文件和日志配置文件

没有Maven管理,配置文件就直接放在src目录下

对Shiro的快速入门源码解析:

1、创建Shiro安全管理器实例

        // The easiest way to create a Shiro SecurityManager with configured
// 创建Shiro安全管理器最简单的方式是使用配置 // realms, users, roles and permissions is to use the simple INI config.
// 访问域,用户,角色(权限),行为,都放在这个简单的ini配置文件中 // We'll do that by using a factory that can ingest a .ini file and
// 我们将会使用工厂实例注入ini配置文件, // return a SecurityManager instance:
// 并且返回Shiro安全管理器的实例 // Use the shiro.ini file at the root of the classpath
// shiro.ini配置文件放在类路径的根下面 // (file: and url: prefixes load from files and urls respectively):
// 分别从文件和url加载前缀 Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();

2、继续配置安全管理器对象

        // for this simple example quickstart, make the SecurityManager accessible as a JVM singleton.
// 在这个简单的快速入门案例中,使安全管理器实例在JVM中是一个可访问的单利对象 // Most applications wouldn't do this
// 但是在大多数应用中不会这么来干 // and instead rely on their container configuration or web.xml for webapps.
// 并且对于webapps而言,是依赖于它们的容器配置文件或web.xml文件 // That is outside the scope of this simple quickstart, so
// 这已经超出了我们shiro快速入门的范畴了,所以 // we'll just do the bare minimum so you can continue to get a feel for things.
// 我们将只做一些你能继续体验的最基础的事情 SecurityUtils.setSecurityManager(securityManager); // Now that a simple Shiro environment is set up, let's see what you can do:
// 现在Shiro的环境已经部署出来了,瞧瞧我们能干点啥

3、获取当前执行的用户

// get the currently executing user:
// 获取当前正在执行的用户
Subject currentUser = SecurityUtils.getSubject();

获取当前的Subject对象

4、通过Session存储的认证信息来处理授权安全

获取 & 简单的判断

        // Do some stuff with a Session (no need for a web or EJB container!!!)
// 用会话做些事情(不需要web或EJB容器!!!)【测试Session】 // 获取Session
Session session = currentUser.getSession(); // 在Session对象中注入某一属性
session.setAttribute("someKey", "aValue"); // 然后又获取这个属性值
String value = (String) session.getAttribute("someKey"); if (value.equals("aValue")) {
       //是否匹配
log.info("---> Retrieved the correct value! [" + value + "]");
}

5、权限详细的操作

        // let's login the current user so we can check against roles and permissions:
// 让我们登录当前用户,以便检查角色和权限: // 测试当前的用户是否已经认证,即是否登陆 调用isAuthenticated()方法判断
if (!currentUser.isAuthenticated()) {
// 如果不是则把认证信息封装在一个令牌对象中
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
// 对这个令牌对象设置 记住我
token.setRememberMe(true);
try {
// 执行登陆指令,能否成功取决于在shiro.ini中是否配置令牌中认证信息
currentUser.login(token);
}
// 未知账户异常 ,没有此用户抛出
catch (UnknownAccountException uae) {
log.info("----> There is no user with username of " + token.getPrincipal());
return;
}
// 不正确的资格证书,账户的密码错误
catch (IncorrectCredentialsException ice) {
log.info("----> Password for account " + token.getPrincipal() + " was incorrect!");
return;
}
//锁定的账户,该用户的账号已经上锁,请联系你的管理员解锁
catch (LockedAccountException lae) {
log.info("The account for username " + token.getPrincipal() + " is locked. " +
"Please contact your administrator to unlock it.");
}
// ... catch more exceptions here (maybe custom ones specific to your application?
// 更多的授权异常问题,翻阅shiro文档详细
catch (AuthenticationException ae) {
//unexpected condition? error?
}
} //say who they are:
// 说明 这是谁的用户 // print their identifying principal (in this case, a username):
// 打印它们的标识主体,在这个演示案例中,只有一个用户名称
log.info("----> User [" + currentUser.getPrincipal() + "] logged in successfully."); //test a role:
// 测试权限 // hasRole,判断是否存在这样一个角色权限?
if (currentUser.hasRole("schwartz")) {
// 存在,愿施瓦兹与你同在
log.info("----> May the Schwartz be with you!");
} else {
// 你好,凡人
log.info("----> Hello, mere mortal.");
return;
} //test a typed permission (not instance-level)
// 测试一用户是否具备这个行为 isPermitted()
if (currentUser.isPermitted("lightsaber:weild")) {
// 你有这个行为,请明智的使用
log.info("----> You may use a lightsaber ring. Use it wisely.");
} else {
// 对卟住,这只是大师才能用的行为
log.info("Sorry, lightsaber rings are for schwartz masters only.");
} //a (very powerful) Instance Level permission:
// 一个非常强大的实例等级行为 // 判断这个用户是否被允许了
if (currentUser.isPermitted("user:delete:zhangsan")) { log.info("----> You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +
"Here are the keys - have fun!");
} else { log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
}

6、完成安全授权,退出

        // 判断是否完成了授权验证

        System.out.println("---->" + currentUser.isAuthenticated());

        //all done - log out!
// 全部搞定,退出
currentUser.logout(); System.out.println("---->" + currentUser.isAuthenticated()); System.exit(0);

【Shiro】01 概述 & 快速上手的更多相关文章

  1. ESFramework 4.0 快速上手(01) -- Rapid引擎

    (在阅读该文之前,请先阅读 ESFramework 4.0 概述 ,会对本文的理解更有帮助.) ESFramework/ESPlatform 4.0 的终极目标是为百万级的用户同时在线提供支持,因为强 ...

  2. 从零开始学 Web 之 Ajax(三)Ajax 概述,快速上手

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  3. ESFramework 4.0 快速上手(06) -- Rapid引擎(续)

    <ESFramework 4.0 快速上手>系列介绍的都是如何使用Rapid引擎(快速引擎) -- RapidServerEngine 和 RapidPassiveEngine.其实,大家 ...

  4. python的requests快速上手、高级用法和身份认证

    https://blog.csdn.net/qq_25134989/article/details/78800209 快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其 ...

  5. 【Python五篇慢慢弹】快速上手学python

    快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...

  6. Netron开发快速上手(一):GraphControl,Shape,Connector和Connection

    版权所有,引用请注明出处:<<http://www.cnblogs.com/dragon/p/5203663.html >> 本文所用示例下载FlowChart.zip 一个用 ...

  7. 聊天系统Demo,增加Silverlight客户端(附源码)-- ESFramework 4.0 快速上手(09)

    在ESFramework 4.0 快速上手 -- 入门Demo,一个简单的IM系统(附源码)一文中,我们介绍了使用ESFramework的Rapid引擎开发的winform聊天程序,本文我们将在之前d ...

  8. EF Core 快速上手——EF Core 入门

    EF Core 快速上手--EF Core 介绍 本章导航 从本书你能学到什么 对EF6.x 程序员的一些话 EF Core 概述 1.3.1 ORM框架的缺点 第一个EF Core应用   本文是对 ...

  9. Java开发快速上手

    Java开发快速上手 前言 1.我的大学 2.对初学者的建议 3.大牛的三大特点 4.与他人的差距 第一章 了解Java开发语言 前言 基础常识 1.1 什么是Java 1.1.1 跨平台性 1.2 ...

  10. 【技术文章】《快速上手nodejs》

    本文地址:http://www.cnblogs.com/aiweixiao/p/8294814.html 原文地址: 扫码关注微信公众号 1.写在前面   nodejs快速上手   nodejs使ja ...

随机推荐

  1. react this指向问题

    在JSX事件函数方法中的 this,默认不会绑定 this指向.如果你忘记绑定,当你调用这个函数的时候 this 的值为 undefined.所以使用时一定要绑定好this的指向. 构造方法中绑定 c ...

  2. Mysql行转列,列转行 WITH ROLLUP 统计

    Mysql行转列,列转行 WITH ROLLUP 统计 DROP TABLE IF EXISTS tb_score; CREATE TABLE tb_score( id INT(11) NOT NUL ...

  3. 支撑阻力指标,庄家成本价是可靠的支撑位(无未来,DLL加密)

    本指标依据庄家的成本价设计的,庄家成本价是可靠的支撑位.底层逻辑:庄家是有内幕的, 庄家能在价格低位时抄底,庄家控股时,庄家不会让散户获取低价的筹码,所以当股价到达到支撑位时,会有比较大的反弹.庄家也 ...

  4. mysql的varchar和oracle的varchar2比较

    首先说结论: 1.mysql存储的是字符数(不分语言) 2.oracle存储的需要看定义,如果定义为varchar2(n),则默认是n个字节,如果是varchar2(n char)则是n个字节. 3. ...

  5. Kubernetes(一)Overview

    1. Kubernetes介绍 要了解Kubernetes,首先我们需要了解Container与Orchestration. Docker Docker的出现是为了解决:部署依赖.以及兼容性.以及繁琐 ...

  6. SpringMVC原理(1)-文件上传请求

    我们文件上传接口只需要在方法参数上写MultipartFile类,mvc就可以帮我们把上传的文件封装为这个类的对 象供我们非常方便的操作,那它是怎么做的呢?我们一起来看看 我们发的请求默认都是由Dis ...

  7. 【换源】git命令行迁移仓库

    直接git clone的话,查看本地分支,会只有默认主分支,可能是master,也可以能是设置的. 查看所有分支 git branch -a * master remotes/origin/HEAD ...

  8. 【简单总结】SLAM 算法的 Benchmark 及相关数据集的结果对比

    前言与参考 主要是copy一下总结,方便自己后续找方案特定使用,所有的出处均在标题处和原链接跳转,此处仅做各个benchmark收集使用,如果有原作者觉得侵权,请联系我 将全力配合相关内容和链接删除 ...

  9. .NET CORE 部署提示 An error occurred while starting the application.

    错误提示: 解决方法 检查一下nuget引用包 是否更新了版本,如果升级或者降级了版本,需要将新的dll文件更新一下

  10. yb课堂之高并发项目必备利器之分布式缓存和本地缓存 《十九》

    什么是缓存? 程序经常要调用的对象存储在内存中,方便其使用时可以快速调用,不必去数据库或者其他持久化设备中查询,主要就是提高性能 DNS.前端缓存.代理服务器缓存Nginx.应用程序缓存(本地缓存.分 ...