Shiro安全框架的说明及配置入门
Shiro是什么?
Shiro是一个非常强大的,易于使用的,开源的,权限框架。它包括了权限校验,权限授予,会话管理,安全加密等组件
什么时候使用它呢?
如果你是设计RBAC基础系统,需要编写大量用于权限控制的代码时,使用Shiro开源大大减少工作量。
使用到的包:

下载路径:
http://shiro.apache.org/download.html
Shiro结构图:

入门配置:
1.配置流程图

配置文件代码:
注意:配置文件的后缀一定要是 .ini
##[users] #用于配置用户名信息
## 用户名= 密码, 角色1, 角色2, …, 角色N
##[roles] #用于配置角色信息
## 角色名= 权限1, 权限2, …, 权限N #全部权限使用 * (星号)
[users]
admin=123456,role_admin,role_edu
[roles]
role_admin = user:list,user:create,user:edit,user:delete
role_edu = edu:list,edu:create
测试代码:
package cn.gzsxt.shiro; 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; public class ShiroTest { public static void main(String[] args) {
//第一步:创建SecurityManager
//shiro框架读取配置文件,如果在源码夹下,需要在前缀加上classpath
IniSecurityManagerFactory imf=new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = imf.createInstance(); //第二步:创建一个身份对象Subject
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token =new UsernamePasswordToken("admin1","123456");
//第三步:校验参数
try {
Subject resultSubject = securityManager.login(subject,token );
// System.out.println(resultSubject.getPrincipal());
// boolean hasRole = resultSubject.hasRole("role_admin");
// System.out.println(hasRole); boolean authenticated = resultSubject.isAuthenticated();
System.out.println(authenticated); } catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Realm机制的必要性:
Shiro是通过Realm机制,实现将配置用户信息存放在数据库等储存系统中
通过Realm可以从数据库里面获得用户的验证信息

配置文件代码:
[main]
#声明realm对象
myRealm=cn.gzsxt.realm.MyRealm #指定securityManager的realm对象
securityManager.realms=$myRealm
编写Realm
package cn.gzsxt.realm; import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection; public class MyRealm extends AuthorizingRealm{
/**
* 权限校验
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// TODO Auto-generated method stub
System.out.println("账号名:"+token.getPrincipal());
if ("admin".equals(token.getPrincipal())) {
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(token.getPrincipal(),"123",this.getName());
return info;
}
return null;
}
/**
* 权限授权
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection token) {
// TODO Auto-generated method stub
return null;
} }
测试代码:
package cn.gzsxt.shiro; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject; public class ShiroTest { public static void main(String[] args) { IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:Shiro.ini");
SecurityManager securityManager = factory.createInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject(); AuthenticationToken token = new UsernamePasswordToken("admin","123");
Subject resultSubject = securityManager.login(subject, token ); boolean authenticated = resultSubject.isAuthenticated();
System.out.println(authenticated);
}
}
附:加密
先创建一个Md5帮助类,得到加密后的密码,便于测试
md5帮助类代码:
package cn.gzsxt.utils; import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource; public class Md5Util { public static void main(String[] args) {
ByteSource salt = ByteSource.Util.bytes("123");
SimpleHash sh = new SimpleHash("md5", "123", salt,1); String target = sh.toString();
System.out.println(target);
} }
Realm处代码

Shiro安全框架的说明及配置入门的更多相关文章
- Shiro安全框架【快速入门】就这一篇!
Shiro 简介 照例又去官网扒了扒介绍: Apache Shiro™ is a powerful and easy-to-use Java security framework that perfo ...
- Shiro安全框架入门使用方法
详见:https://blog.csdn.net/qq_32651225/article/details/77199464 框架介绍Apache Shiro是一个强大且易用的Java安全框架,执行身份 ...
- shiro安全框架
原文:http://blog.csdn.net/boonya/article/details/8233303 可能大家早先会见过 J-security,这个是 Shiro 的前身.在 2009 年 3 ...
- Shiro 安全框架详解二(概念+权限案例实现)
Shiro 安全框架详解二 总结内容 一.登录认证 二.Shiro 授权 1. 概念 2. 授权流程图 三.基于 ini 的授权认证案例实现 1. 实现原理图 2. 实现代码 2.1 添加 maven ...
- Shiro 安全框架详解一(概念+登录案例实现)
shiro 安全框架详细教程 总结内容 一.RBAC 的概念 二.两种常用的权限管理框架 1. Apache Shiro 2. Spring Security 3. Shiro 和 Spring Se ...
- Shiro 权限框架使用总结
我们首先了解下什么是shiro ,Shiro 是 JAVA 世界中新近出现的权限框架,较之 JAAS 和 Spring Security,Shiro 在保持强大功能的同时,还在简单性和灵活性方面拥有巨 ...
- thymeleaf模板引擎shiro集成框架
shiro权限框架.前端验证jsp设计.间tag它只能用于jsp系列模板引擎. 使用最近项目thymeleaf作为前端模板引擎,采用HTML档,未出台shiro的tag lib,假设你想利用这段时间s ...
- SpringMVC整合Shiro权限框架
尊重原创:http://blog.csdn.net/donggua3694857/article/details/52157313 最近在学习Shiro,首先非常感谢开涛大神的<跟我学Shiro ...
- SpringBoot集成Shiro安全框架
跟着我的步骤:先运行起来再说 Spring集成Shiro的GitHub:https://github.com/yueshutong/shiro-imooc 一:导包 <!-- Shiro安全框架 ...
随机推荐
- 匿名函数 sorted() filter() map() 递归函数
一. lambda() 匿名函数 说白了,从字面理解匿名函数就是看不见的函数,那么他的看不见表现在哪里呢? 其实就是在查询的时候他们的类型都是lambda的类型所以叫匿名,只要是用匿名函数写的大家 ...
- MYSQL数据库中的查询语句
查询的方法 *简单查询:select * from 表名 (* = 所有的) *读取特定列:select 字段一,字段二 from 表名 *条件查询:select * from 表名 where (多 ...
- httpclient模拟服务器请求
// 创建默认的httpClient实例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httppost Ht ...
- 《JAVA设计模式》之访问者模式(Visitor)
在阎宏博士的<JAVA与模式>一书中开头是这样描述访问者(Visitor)模式的: 访问者模式是对象的行为模式.访问者模式的目的是封装一些施加于某种数据结构元素之上的操作.一旦这些操作需要 ...
- Struts2之获取ServletAPI
1.通过ServletActionContext类 //获取request对象 HttpServletRequest request = ServletActionContext.getRequest ...
- for in 和for of的区别
for in 和for of的区别:https://www.jianshu.com/p/c43f418d6bf0 1 遍历数组通常用for循环 ES5的话也可以使用forEach,ES5具有遍历数组功 ...
- 【洛谷p2089】 烤鸡
烤鸡[题目链接] 感觉我超废 关于算法:有很多很多算法吧,但自我感觉最重要的是递归的算法: SOLUTION: 首先忍不住要吐槽这个神仙数据: 嗯???定睛一看,它这数据范围莫不是白瞎了,因为每种配料 ...
- 【洛谷新手村】简单字符串 p1055 ISBN号码
p1055 ISBN号码[传送门] 算法标签什么的: 思路:直接以字符串的形式读入这一串数字,然后for循环对字符串进行处理,字符串中的数字存进数组中(如果是X,存为10):然后再根据要求判断是否是正 ...
- Codeforces - 1195D1 - Submarine in the Rybinsk Sea (easy edition) - 水题
https://codeforc.es/contest/1195/problem/D1 给\(n\)个等长的十进制数串,定义操作\(f(x,y)\)的结果是"从\(y\)的末尾开始一个一个交 ...
- shuoj 1 + 2 = 3? (二分+数位dp)
题目传送门 1 + 2 = 3? 发布时间: 2018年4月15日 22:46 最后更新: 2018年4月15日 23:25 时间限制: 1000ms 内存限制: 128M 描述 埃森哲是 ...