shiro框架学习-4- Shiro内置JdbcRealm
1. JdbcRealm 数据库准备
JdbcRealm就是用户的角色,权限都从数据库中读取,也就是用来进行用户认证授权的安全数据源更换为从数据库中读取,其他没有差别,首先在数据库创建三张表:
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`password_salt` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_users_username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `user_roles` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(100) DEFAULT NULL,
`role_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_user_roles` (`username`,`role_name`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
CREATE TABLE `roles_permissions` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`role_name` varchar(100) DEFAULT NULL,
`permission` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_roles_permissions` (`role_name`,`permission`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
插入数据:
INSERT INTO `users` VALUES (1,'jack','123',NULL),(2,'xdclass','456',NULL);
INSERT INTO `roles_permissions` VALUES (4,'admin','video:*'),(3,'role1','video:buy'),(2,'role1','video:find'),(5,'role2','*'),(1,'root','*');
INSERT INTO `user_roles` VALUES (1,'jack','role1'),(2,'jack','role2'),(4,'xdclass','admin'),(3,'xdclass','root');
2. JdbcRealm 配置文件
#注意 文件格式必须为ini,编码为ANSI
#声明Realm,指定realm类型
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm #配置数据源
#dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
dataSource=com.alibaba.druid.pool.DruidDataSource # mysql-connector-java 5 用的驱动url是com.mysql.jdbc.Driver,mysql-connector-java6以后用的是com.mysql.cj.jdbc.Driver
dataSource.driverClassName=com.mysql.cj.jdbc.Driver #避免安全警告
dataSource.url=jdbc:mysql://localhost:3306/xdclass_shiro?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
dataSource.username=root
dataSource.password=lchadmin #指定数据源
jdbcRealm.dataSource=$dataSource #开启查找权限,否则不会自动查询角色对应的权限,造成实际有权限,调用subject.isPermitted()返回false
jdbcRealm.permissionsLookupEnabled=true #指定SecurityManager的Realms实现,设置realms,可以有多个,用逗号隔开
securityManager.realms=$jdbcRealm
测试代码
package net.xdclass.xdclassshiro; import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test; /**
* jdbcRealm操作
*/
public class QuicksStratTest5_3 { @Test
public void testAuthentication() {
//通过配置文件创建SecurityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:jdbcrealm.ini");
// 获取SecurityManager实例
SecurityManager securityManager = factory.getInstance();
//设置当前上下文
SecurityUtils.setSecurityManager(securityManager); //获取当前subject(application应用的user)
Subject subject = SecurityUtils.getSubject();
// 模拟用户输入
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jack","123");
//
subject.login(usernamePasswordToken);
System.out.println("认证结果(是否已授权):" + subject.isAuthenticated());
//最终调用的是org.apache.shiro.authz.ModularRealmAuthorizer.hasRole方法
System.out.println("是否有role1角色:" + subject.hasRole("role1"));
System.out.println("是否有role2角色:" + subject.hasRole("role2"));
System.out.println("是否有root角色:" + subject.hasRole("root"));
//获取登录 账号
System.out.println("getPrincipal():" + subject.getPrincipal());
//校验角色,没有返回值,校验不通过,直接跑出异常
subject.checkRole("role1");
System.out.println("=======subject.checkRole(\"role1\") passed=====" );
// user jack有video的find权限,执行通过
subject.checkPermission("video:find");
// 是否有video:find权限:true
System.out.println("是否有video:find权限:" + subject.isPermitted("video:find"));
// 是否有video:delete权限:false
System.out.println("是否有video:delete权限:" + subject.isPermitted("video:delete"));
//user jack没有video的删除权限,执行会报错:org.apache.shiro.authz.UnauthorizedException: Subject does not have permission [video:delete]
subject.checkPermission("video:delete");
subject.logout();
System.out.println("logout后认证结果:" + subject.isAuthenticated()); /* org.apache.shiro.realm.jdbc.JdbcRealm源码
* 1. class JdbcRealm extends AuthorizingRealm
* 2. 预置了默认的查询语句,因此创建数据库时字段名字要与这里定义的一致!!!
* protected static final String DEFAULT_AUTHENTICATION_QUERY = "select password from users where username = ?";
protected static final String DEFAULT_SALTED_AUTHENTICATION_QUERY = "select password, password_salt from users where username = ?";
#根据用户名称查角色
protected static final String DEFAULT_USER_ROLES_QUERY = "select role_name from user_roles where username = ?";
* #根据用户名称查权限
protected static final String DEFAULT_PERMISSIONS_QUERY = "select permission from roles_permissions where role_name = ?";
protected String authenticationQuery = "select password from users where username = ?";
protected String userRolesQuery = "select role_name from user_roles where username = ?";
* #根据角色查询权限
protected String permissionsQuery = "select permission from roles_permissions where role_name = ?";
*
* 3. protected boolean permissionsLookupEnabled = false; 这个开关默认是关闭的,需要手动打开
* 4.
* protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
if (principals == null) {
throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
} else {
String username = (String)this.getAvailablePrincipal(principals);
Connection conn = null;
Set<String> roleNames = null;
Set permissions = null; try {
conn = this.dataSource.getConnection();
roleNames = this.getRoleNamesForUser(conn, username);
if (this.permissionsLookupEnabled) {
permissions = this.getPermissions(conn, username, roleNames);
}
} catch (SQLException var11) {
String message = "There was a SQL error while authorizing user [" + username + "]";
if (log.isErrorEnabled()) {
log.error(message, var11);
} throw new AuthorizationException(message, var11);
} finally {
JdbcUtils.closeConnection(conn);
} SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
info.setStringPermissions(permissions);
return info;
}
* */
} @Test
public void test2(){
// 不使用配置文件的情况下:
DefaultSecurityManager securityManager = new DefaultSecurityManager();
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/xdclass_shiro?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false");
ds.setUsername("root");
ds.setPassword("lchadmin");
JdbcRealm jdbcRealm = new JdbcRealm();
jdbcRealm.setPermissionsLookupEnabled(true);
jdbcRealm.setDataSource(ds);
securityManager.setRealm(jdbcRealm);
// 将securityManager设置到当前运行环境中
SecurityUtils.setSecurityManager(securityManager);
//获取当前subject(application应用的user)
Subject subject = SecurityUtils.getSubject();
// 模拟用户输入
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jack","123");
//
subject.login(usernamePasswordToken);
System.out.println("认证结果(是否已授权):" + subject.isAuthenticated());
//最终调用的是org.apache.shiro.authz.ModularRealmAuthorizer.hasRole方法
System.out.println("是否有role1角色:" + subject.hasRole("role1"));
System.out.println("是否有role2角色:" + subject.hasRole("role2"));
System.out.println("是否有root角色:" + subject.hasRole("root"));
//获取登录 账号
System.out.println("getPrincipal():" + subject.getPrincipal());
//校验角色,没有返回值,校验不通过,直接抛出异常
subject.checkRole("role1");
System.out.println("=======subject.checkRole(\"role1\") passed=====" );
// user jack有video的find权限,执行通过
subject.checkPermission("video:find");
// 是否有video:find权限:true
System.out.println("是否有video:find权限:" + subject.isPermitted("video:find"));
// 是否有video:delete权限:false
System.out.println("是否有video:delete权限:" + subject.isPermitted("video:delete"));
//user jack没有video的删除权限,执行会报错:org.apache.shiro.authz.UnauthorizedException: Subject does not have permission [video:delete]
subject.checkPermission("video:delete");
} }
shiro框架学习-4- Shiro内置JdbcRealm的更多相关文章
- shiro框架学习-3- Shiro内置realm
1. shiro默认自带的realm和常见使用方法 realm作用:Shiro 从 Realm 获取安全数据 默认自带的realm:idae查看realm继承关系,有默认实现和自定义继承的realm ...
- shiro框架学习-6-Shiro内置的Filter过滤器及数据加解密
1. shiro的核心过滤器定义在枚举类DefaultFilter 中,一共有11个 ,配置哪个路径对应哪个拦截器进行处理 // // Source code recreated from a .c ...
- shiro框架学习-5-自定义Realm
1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...
- shiro框架学习-1-shiro基本概念
1. 什么是权限控制 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能访问自己被授权的资源, ...
- Python学习第二阶段day1 内置函数,序列化,软件目录开发规范
内置函数 1.abs() 求绝对值 2.all() 所有元素为真才返回真 all( [1,1,2,3,-1] ) 值为True 3.any() 所有元素为假才返回假 any([0,0,0 ...
- shiro基础学习(二)—shiro认证
一.shiro简介 shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证.权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架. 以下 ...
- python自动化测试学习笔记-4内置函数,处理json
函数.全局变量 写代码时注意的几点事项: 1.一般写代码的时候尽量少用或不用全局变量,首先全局变量不安全,大家协作的情况下,代码公用容易被篡改,其次全局变量会一直占用系统内容. 2.函数里如果有多个r ...
- js学习---常用的内置对象(API)小结 :
内置对象(API): 日期 Date: getFullYear() 返回完整的4位的年份 如:2016 getMonth() 返回月份,从0开始 getDate() 返回当前月的第几天,当 ...
- 学习笔记——Maven 内置变量
Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...
随机推荐
- python 并发编程 多线程 线程queue
线程queue 线程之间已经是共享数据的,为什么还使用线程queue? 线程需要自己加锁,线程queue帮我们处理好加锁的问题 有三种不同的用法 第一种方法: class queue.Queue(ma ...
- 2019JAVA第八次实验报告
班级 计科二班 学号 20188442 姓名 吴怡君 完成时间 2019.11.1 评分等级 课程作业: 将奇数位小写字母改写为大写字母(用文件输出) 实验代码: package Domon7; im ...
- Magic Potion(网络流)
原题链接 2018南京的铜牌题,听说学长他们上来就A了,我这个图论选手也就上手做了做,结果一言难尽...... 发此篇博客希望自己能牢记自己的菜... 本题大意:有n个heros和m个monsters ...
- 移除list里面的值
public class IteratorTest { public static void main(String[] args) { List<String> list = new A ...
- goods商品类
- java线程捕获异常
java多线程程序中,所有线程都不允许抛出未捕获的checked exception(比如sleep时的InterruptedException),也就是说各个线程需要自己把自己的checked ex ...
- mysql数据库常见错误代码列表
mysql出错代码列表1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导致删除数据库失败 1 ...
- phpmyadmin导入大容量.sql文件
phpmyadmin导入大容量.sql文件 在phpmyadmin目录文件夹下建立一个文件夹,如importSqlFile 将想要导入的sql文件放入importSqlFile文件夹中 打开confi ...
- php学习第一天(记录注意事项)
- 设置SVC模式
清0:bic 置1:orr 访问cpsr和spdr要用到mrs和msr指令 mrs是把状态寄存器的值赋给通用寄存器 msr是把通用寄存器的值赋给状态寄存器 .text .global _start _ ...