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 在它的帮助下我们可以非常方便的将一个命令行程序升级成一个图形 ...
随机推荐
- SpringBoot使用注解实现事务管理
conttoller controller和普通的controller类一样, 不用改变 @RequestMapping(value = "/path/{id}", method ...
- ASP.NET基础知识汇总之WebConfig各节点介绍
web.config虽然一直用,接触最多的也就是节点appSettings和connectionSettings,今天系统的梳理一下,了解一下webconfig各个节点的含义,先简单的浏览一下具体的w ...
- loss函数和cost函数
loss函数指单个样本的预测值和真值的偏差 cost函数指整体样本的预测值和真值的偏差
- 基于aws api gateway的asp.net core验证
本文是介绍aws 作为api gateway,用asp.net core用web应用,.net core作为aws lambda function. api gateway和asp.net core的 ...
- Elastic Stack-Elasticsearch使用介绍(一)
一.前言 Elasticsearch对外提供RESTful API,下面的演示我们主要使用Postman,进行一系列的Demo演示,这款工具方便各位前端大大或者对接口调试的神器: 安装过于简单 ...
- jeecg开发环境搭建
Maven安装 步骤见:https://www.cnblogs.com/dyh004/p/8523260.html 修改Maven仓库 1.修改maven仓库存放位置 修改maven仓库存放位置:找到 ...
- 关于childNodes的删除
在使用childNodes时,发现需要删除的元素多于1时,会出现无法全部删除的情况.谷歌以后发现,该属性返回的子节点集合是实时更新的,也就是说,在for循环中,当删除第一个子节点之后,第二次删除的是原 ...
- swipe.js实现支持手拔与自动切换的图片轮播
一.Html代码如下: <div id='mySwipe' style='max-width:500px;margin:0 auto' class='swipe'> <div cla ...
- openstack搭建之-创建实例(13)
一. 创建flat网络的实例 #运行admin环境变量,创建网络类型为flat . admin-openrc openstack network create --share \ --provider ...
- 【学习总结】GirlsInAI ML-diary day-12-for循环
[学习总结]GirlsInAI ML-diary 总 原博github链接-day12 认识for循环执行 ps: range()函数 python range() 函数可创建一个整数列表,一般用在 ...