2.1 认证流程
2.2 授权流程
2.3 SimpleAccountRealm
创建maven项目,添加如下依赖
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version> </dependency>
代码测试:(认证和角色校验)
@Test
public void simple(){
//1.创建Realm
SimpleAccountRealm realm = new SimpleAccountRealm();
realm.addAccount("admin","admin","超级管理员");
//2.SecurityManager
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//3. 建立SecurityManager 与 Realm之间的连接
securityManager.setRealm(realm);
//4. subject和SecurityManager建立联系
SecurityUtils.setSecurityManager(securityManager);
//5. 声明Subject
Subject subject = SecurityUtils.getSubject();
//6.发起认证
subject.login(new UsernamePasswordToken("admin","admin"));
System.out.println(subject.isAuthenticated());
//7.认证成功后角色校验
System.out.println(subject.hasRole("管理员"));
//8. 退出登录
subject.logout();
}
结果 :
2.4 IniRealm
基于文件存储用户名、密码,角色等信息,支持权限校验。
shiro.ini文件
[users] admin=admin,超级管理员,管理员,普通用户 [roles] 超级管理员=user:add,user:delete,user:update
代码:
@Test
public void authen(){
IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
DefaultSecurityManager securityManager = new DefaultSecurityManager();
securityManager.setRealm(iniRealm);
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
subject.login(new UsernamePasswordToken("admin","admin"));
System.out.println(subject.isAuthenticated());
subject.checkRole("超级管理员");
System.out.println(subject.isPermitted("user:update"));
subject.checkPermission("user:delete");
}
结果:
2.5 JdbcRealm
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency>
实现权限校验时,库表设计方案, 经典五张表(用户认证,授权)
实际上使用三张表也可以。
@Test
public void test01(){
JdbcRealm jdbcRealm = new JdbcRealm();
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost/shiro");
dataSource.setUsername("root");
dataSource.setPassword("123456");
jdbcRealm.setDataSource(dataSource);
jdbcRealm.setPermissionsLookupEnabled(true);
DefaultSecurityManager securityManager = new DefaultSecurityManager();
securityManager.setRealm(jdbcRealm);
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
subject.login(new UsernamePasswordToken("admin","admin"));
System.out.println(subject.isAuthenticated());
System.out.println(subject.hasRole("超级1管理员"));
System.out.println(subject.isPermitted("user:add"));
}
注:在实现权限校验时,必须先开启,默认是false。realm.setPermissionsLookupEnabled(true);
构建的Sql代码
/*
Navicat MySQL Data Transfer
Source Server : localhost_yan
Source Server Type : MySQL
Source Server Version : 80034
Source Host : localhost:3306
Source Schema : shiro
Target Server Type : MySQL
Target Server Version : 80034
File Encoding : 65001
Date: 07/11/2023 22:06:28
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for roles_permissions
-- ----------------------------
DROP TABLE IF EXISTS `roles_permissions`;
CREATE TABLE `roles_permissions` (
`id` int NOT NULL AUTO_INCREMENT,
`permission` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`role_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of roles_permissions
-- ----------------------------
INSERT INTO `roles_permissions` VALUES (1, 'user:add', '超级管理员');
INSERT INTO `roles_permissions` VALUES (2, 'user:update', '超级管理员');
INSERT INTO `roles_permissions` VALUES (3, 'user:select', '运营');
-- ----------------------------
-- Table structure for user_roles
-- ----------------------------
DROP TABLE IF EXISTS `user_roles`;
CREATE TABLE `user_roles` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`role_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user_roles
-- ----------------------------
INSERT INTO `user_roles` VALUES (1, 'admin', '超级管理员');
INSERT INTO `user_roles` VALUES (2, 'admin', '运营');
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES (1, 'admin', 'admin');
SET FOREIGN_KEY_CHECKS = 1;