Shiro 系列: 简单命令行程序示例
在本示例中, 使用 INI 文件来定义用户和角色. 首先学习一下 INI 文件的规范.
=======================
Shiro INI 的基本规范
=======================
[main]
# 在这里定义 SecurityManager 和 Realms 等
[users]
# 每一行定义一个用户, 格式是 username = password, role1, role2, ..., roleN
[roles]
# 角色在这里定义, 格式是 roleName = perm1, perm2, ..., permN
# 说明1: 权限名可以使用带有层次的命名方式, 使用冒号来分割层次关系, 比如 user:create 或 user:poweruser:update 权限.
# 说明2: user:* 这样的权限, 代表具有 user:create 和 user:poweruser:update 权限.
[urls]
# 对于web系统, 可在这里定义url的权限配置.
==========================
pom
==========================
Shiro jar需要 slf4j 依赖项.
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
# =======================
shiro.ini 示例文件的内容
# =======================
# =======================
shiro.ini 示例文件的内容
# =======================
# -----------------------------------------------------------------------------
# Users and their (optional) assigned roles
# username = password, role1, role2, ..., roleN
# -----------------------------------------------------------------------------
[users]
root = secret, admin
guest = guest, guest
presidentskroob = 12345, president
darkhelmet = ludicrousspeed, darklord, schwartz
aihe = aihe, goodguy, client # -----------------------------------------------------------------------------
# Roles with assigned permissions
# roleName = perm1, perm2, ..., permN
# -----------------------------------------------------------------------------
[roles]
admin = *
client = look:*
goodguy = winnebago:drive:eagle5
==========================
API 代码示例
==========================
@Override
public void run(String... args) throws Exception {
// 创建sessionFactory,使用ini配置文件初始化
IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
// 创建securityManager实例
SecurityManager securityManager = factory.getInstance(); // 将securityManager配置在当前运行环境中
SecurityUtils.setSecurityManager(securityManager); // 获取当前的subject
Subject currentUser = SecurityUtils.getSubject(); // session 操作
Session session = currentUser.getSession();
System.out.println("Id:" + session.getId()); session.setAttribute("name", "value");
System.out.println(session.getAttribute("name")); if (!currentUser.isAuthenticated()) {
// 登录需要一个 token
UsernamePasswordToken token = new UsernamePasswordToken("root", "secret"); // 在 token 上设置 RememberMe
// token.setRememberMe(true); // 登录
currentUser.login(token); // 登录后可获取认证身份(一个或多个)
PrincipalCollection principals = currentUser.getPrincipals();
for (Object principal : principals) {
System.out.println(principal.toString());
} // 角色检查
boolean granted1 = currentUser.hasRole("admin");
System.out.println("hasRole('admin'):" + granted1); boolean granted2 = currentUser.hasRole("winnebago:drive");
System.out.println("hasRole('winnebago:drive'):" + granted1); // 角色检查断言, 如果没有对应的角色, 会抛出 AuthorizationExceptions
currentUser.checkRole("admin"); // 权限检查
boolean granted3 = currentUser.isPermitted("winnebago:drive");
System.out.println("isPermitted('winnebago:drive'):" + granted2); // 权限检查断言, 如果没有对应的权限, 会抛出 AuthorizationExceptions
currentUser.checkPermission("winnebago:drive"); // 登出
currentUser.logout(); } else {
System.out.println("you have login");
}
}
结果输出为:
Id:71b126e5-a79c-416d-9abb-1b5430eaf5c3
value
root
hasRole('admin'):true
hasRole('winnebago:drive'):true
isPermitted('winnebago:drive'):false
==========================
参考
==========================
https://www.jianshu.com/p/5a35d0100a71
Shiro 系列: 简单命令行程序示例的更多相关文章
- 用什么库写 Python 命令行程序?看这一篇就够了
作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...
- Apache Commons CLI 开发命令行工具示例
概念说明Apache Commons CLI 简介 虽然各种人机交互技术飞速发展,但最传统的命令行模式依然被广泛应用于各个领域:从编译代码到系统管理,命令行因其简洁高效而备受宠爱.各种工具和系统都 提 ...
- 使用 Apache Commons CLI 开发命令行工具示例
Apache Commons CLI 简介 Apache Commons CLI 是 Apache 下面的一个解析命令行输入的工具包,该工具包还提供了自动生成输出帮助文档的功能. Apache Com ...
- 使用 Apache Commons CLI 解析命令行参数示例
很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apa ...
- 命令行程序增加 GUI 外壳
Conmajia © 2012 Updated on Feb. 21, 2018 命令行大家都用过: 图 1 命令行程序工作界面 现在想办法为它做一个 GUI 外壳,实际效果参考图 2. 图 2 带 ...
- c#词频统计命令行程序
这里将用c#写一个关于词频统计的命令行程序. 预计时间分配:输入处理3h.词条排序打印2h.测试3h. 实际时间分配:输入处理1h.词条排序打印2h.测试3h.程序改进优化6h. 下面将讲解程序的完成 ...
- GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载
GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载 轉載請註明出處:https://www.cnblogs.com/funnyzpc/p/11721978.html 内容提要: ...
- Node: 开发命令行程序
CLI 的全称是 Command-line Interface (命令行界面),即在命令行接受用户的键盘输入并作出响应和执行的程序. 在 Node.js 中,全局安装的包一般都具有命令行界面的功能,例 ...
- 手写笔记变PDF-几行代码变命令行程序为图形化界面
前言 最近发现了一个非常不错的Python类库----Gooey, https://github.com/chriskiehl/Gooey 在它的帮助下我们可以非常方便的将一个命令行程序升级成一个图形 ...
随机推荐
- LeetCode算法题-Two Sum IV - Input is a BST(Java实现)
这是悦乐书的第280次更新,第296篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第148题(顺位题号是653).给定二进制搜索树和目标数,如果BST中存在两个元素,使得 ...
- telnet操作memcache
1.使用方法 1. 连接到memcached telnet 192.168.1.100 11211 add name 0 60 5 [说明 add 是指令名 name 是key的名字 (是以 ...
- Python安装包:协程(gevent)
- perf + Flame Graph火焰图分析程序性能
1.perf命令简要介绍 性能调优时,我们通常需要分析查找到程序百分比高的热点代码片段,这便需要使用 perf record 记录单个函数级别的统计信息,并使用 perf report 来显示统计结果 ...
- HNOI2019:My Dream
反正这次的目标也不是进省队,目标就是做到最好吧-- 下面都是流水账~ Day -INF ~ Day -3 专题交流没什么好说的,模拟赛详见3.11-3.27省选前多校联考乱记和3.28-4.2CJ大毒 ...
- 基于微服务的DevOps落地指南 交付效率提升40%
基于微服务的DevOps落地指南 交付效率提升40% 2015-2016年,珍爱线下门店已新增覆盖城市9个,与此同时,CRM系统大小故障却发生了数十起... ... 珍爱网是以“网络征选+人工红娘”模 ...
- MariaDB第三章:数据库设计与备份--小白博客
数据库设计 1.第一范式(确保每列保持原子性) 第一范式是最基本的范式.如果数据库表中的所有字段值都是不可分解的原子值,就说明该数据库表满足了第一范式. 2.第二范式(确保表中的每列都和主键相关) 第 ...
- hdu-1728(贪心&&bfs的灵活运用吧)
链接 [https://vjudge.net/contest/256476#problem/D] 题意 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到 ...
- 如何将多个C文件链接在一起----Makefile编写及make指令
需使用GCC编译器,关于MinGW的安装指南:https://people.eng.unimelb.edu.au/ammoffat/teaching/20005/Install-MinGW.pdf 单 ...
- 【转】Python爬虫:抓取新浪新闻数据
案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...