Maven的的依赖和第一篇是一样的副本直接用
上两篇讲的无论是内部类的封装领域还是配置文件的认证源,这篇开始讲最关心的数据库作为境界的认证源
这里使用的是四郎给我们提供的另一个内置的类JdbcRealm这个是连接数据库的一个内部类,话不多说了,
具体的代码如下:
import com.alibaba.druid.pool.DruidDataSource; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.mgt.DefaultSecurityManager; import org.apache.shiro.realm.jdbc.JdbcRealm; import org.apache.shiro.realm.text.IniRealm; import org.apache.shiro.subject.Subject; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class JdbcRealmTest { DruidDataSource druidDataSource = new DruidDataSource(); { druidDataSource.setUrl("jdbc:mysql://localhost:3306/test"); druidDataSource.setUsername("root"); druidDataSource.setPassword("root"); } @Test public void testAuthenticationTest() { // 设置内置的jdbc JdbcRealm jdbcRealm = new JdbcRealm(); jdbcRealm.setDataSource(druidDataSource); // 权限数据的开关 jdbcRealm.setPermissionsLookupEnabled(true); String sql = "select password from test_user where username = ?"; jdbcRealm.setAuthenticationQuery(sql); String roleSql = "select role_name from test_user_role where user_name = ?"; jdbcRealm.setUserRolesQuery(roleSql); // 1.构建SecurityManager环境 DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager(); defaultSecurityManager.setRealm(jdbcRealm); // 2.主体提交认证请求 SecurityUtils.setSecurityManager(defaultSecurityManager); Subject subject = SecurityUtils.getSubject(); // 这个是需要认证的信息 UsernamePasswordToken token = new UsernamePasswordToken("xiaoming","654321"); subject.login(token); System.out.println("isAuthenticated="+subject.isAuthenticated()); //subject.logout(); /*subject.checkRole("admin"); subject.checkPermission("user:select");*/ subject.checkRole("user"); } } 具体的数据库文件:
AI 代码解读
/ *
Navicat Premium数据传输
源服务器:根源
服务器类型:MySQL的的
源服务器版本:50722
源主机:本地主机:3306
源架构:测试
目标服务器类型:MySQL的的
目标服务器版本:50722
文件编码:65001
日期:28/07/2018 08:18:03
* /
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
- ----------------------------
- roles_permissions的表结构
- ------------ --- -------------
DROP TABLE IF EXISTS`coole_permissions`;
CREATE TABLE`colces_permissions`(
`id` int(11)NOT NULL AUTO_INCREMENT,
`role_name` varchar(255)CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`permission` varchar(255)CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
PRIMARY KEY(`id`)使用BTREE
)ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
- ----------------------------
- roles_permissions的记录
- ------------- --- ------------
插入`roles_permissions` VALUES(1,'admin','user:select');
SET FOREIGN_KEY_CHECKS = 1;
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------
/ *
Navicat Premium数据传输
源服务器:根源
服务器类型:MySQL的的
源服务器版本:50722
源主机:本地主机:3306
源架构:测试
目标服务器类型:MySQL的的
目标服务器版本:50722
文件编码:65001
日期:28/07/2018 08:18:17
* /
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
- ----------------------------
- test_user的表结构
- ------------ --- -------------
DROP TABLE IF EXISTS`test_user`;
CREATE TABLE`test_user`(
`username` varchar(255)CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`password` varchar(255)CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL
)ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT =动态;
- ----------------------------
- test_user的记录
- ------------- --- ------------
插入`test_user` VALUES('xiaoming','654321');
SET FOREIGN_KEY_CHECKS = 1;
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -----------------
/ *
Navicat Premium数据传输
源服务器:根源
服务器类型:MySQL的的
源服务器版本:50722
源主机:本地主机:3306
源架构:测试
目标服务器类型:MySQL的的
目标服务器版本:50722
文件编码:65001
日期:28/07/2018 08:18:26
* /
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
- ----------------------------
- test_user_role的表结构
- ------------ --- -------------
DROP TABLE IF EXISTS`test_user_role`;
CREATE TABLE`test_user_role`(
`user_name` varchar(255)CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`role_name` varchar(255)CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL
)ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT =动态;
- ----------------------------
- test_user_role的记录
- ------------- --- ------------
插入`test_user_role` VALUES('xiaoming','user');
SET FOREIGN_KEY_CHECKS = 1;
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----------------
/ *
Navicat Premium数据传输
源服务器:根源
服务器类型:MySQL的
源服务器版本:50722
源主机:本地主机:3306
源架构:测试
目标服务器类型:MySQL的
目标服务器版本:50722
文件编码:65001
日期:28/07/2018 08:18:32
* /
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
- ----------------------------
- user_roles的表结构
- ------------ ----------------
DROP TABLE IF EXISTS`user_roles`;
CREATE
TABLE`user_roles` (`id` int(11)NOT NULL AUTO_INCREMENT,
`username` varchar(255)CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`role_name` varchar(255)CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
PRIMARY KEY(`id`)使用BTREE
)ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
- ----------------------------
- user_roles的记录
- ------------- ---------------
INSERT INTO`user_roles` VALUES(1,'Mark','admin');
SET FOREIGN_KEY_CHECKS = 1;
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------
/ *
Navicat Premium数据传输
源服务器:根源
服务器类型:MySQL
源服务器版本:50722
源主机:localhost:3306
源架构:测试
目标服务器类型:MySQL
目标服务器版本:50722
文件编码:65001
日期:28/07/2018 08:18:41
* /
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
- ----------------------------
- 用户表结构
- ------------ ---- ------------
DROP TABLE IF EXISTS`users`;
CREATE
TABLE`user`(`id` int(11)NOT NULL AUTO_INCREMENT,
`username` varchar(255)CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`password` varchar(255)CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
PRIMARY KEY(`id`)使用BTREE
)ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
- ----------------------------
- 用户记录
- ------------- ---- -----------
插入`users` VALUES(1,'Mark','123456');
SET FOREIGN_KEY_CHECKS = 1;
执行流程和前两篇一样。