@@ -1,65 +1,161 @@ | |||||
# 用户表 | |||||
CREATE TABLE IF NOT EXISTS `sec_user` | |||||
/* | |||||
Navicat Premium Data Transfer | |||||
Source Server : 本机 | |||||
Source Server Type : MySQL | |||||
Source Server Version : 50718 | |||||
Source Host : localhost:3306 | |||||
Source Schema : spring-boot-demo | |||||
Target Server Type : MySQL | |||||
Target Server Version : 50718 | |||||
File Encoding : 65001 | |||||
Date: 10/12/2018 14:46:38 | |||||
*/ | |||||
SET NAMES utf8mb4; | |||||
SET FOREIGN_KEY_CHECKS = 0; | |||||
-- ---------------------------- | |||||
-- Table structure for sec_permission | |||||
-- ---------------------------- | |||||
DROP TABLE IF EXISTS `sec_permission`; | |||||
CREATE TABLE `sec_permission` | |||||
( | ( | ||||
`id` BIGINT(64) NOT NULL COMMENT '主键', | |||||
`username` VARCHAR(50) NOT NULL COMMENT '用户名', | |||||
`password` VARCHAR(60) NOT NULL COMMENT '密码', | |||||
`nickname` VARCHAR(255) DEFAULT NULL COMMENT '昵称', | |||||
`phone` VARCHAR(11) DEFAULT NULL COMMENT '手机', | |||||
`email` VARCHAR(50) DEFAULT NULL COMMENT '邮箱', | |||||
`birthday` BIGINT(13) DEFAULT NULL COMMENT '生日', | |||||
`sex` TINYINT(1) DEFAULT NULL COMMENT '性别,男-1,女-2', | |||||
`status` TINYINT(1) NOT NULL DEFAULT '1' COMMENT '状态,启用-1,禁用-0', | |||||
`create_time` BIGINT(13) NOT NULL COMMENT '创建时间', | |||||
`update_time` BIGINT(13) NOT NULL COMMENT '更新时间', | |||||
PRIMARY KEY (`id`), | |||||
UNIQUE KEY `username` (`username`), | |||||
UNIQUE KEY `phone` (`phone`), | |||||
UNIQUE KEY `email` (`email`) | |||||
`id` bigint(64) NOT NULL COMMENT '主键', | |||||
`name` varchar(50) NOT NULL COMMENT '权限名', | |||||
`href` varchar(1000) DEFAULT NULL COMMENT '页面地址', | |||||
`type` int(2) NOT NULL COMMENT '权限类型,页面-1,按钮-2', | |||||
`permission` varchar(50) DEFAULT NULL COMMENT '权限表达式', | |||||
`sort` int(11) NOT NULL COMMENT '排序', | |||||
`parent_id` bigint(64) NOT NULL COMMENT '父级id', | |||||
PRIMARY KEY (`id`) | |||||
) ENGINE = InnoDB | ) ENGINE = InnoDB | ||||
DEFAULT CHARSET = utf8 COMMENT '用户表'; | |||||
DEFAULT CHARSET = utf8 COMMENT ='权限表'; | |||||
# 角色表 | |||||
CREATE TABLE IF NOT EXISTS `sec_role` | |||||
-- ---------------------------- | |||||
-- Records of sec_permission | |||||
-- ---------------------------- | |||||
BEGIN; | |||||
INSERT INTO `sec_permission` | |||||
VALUES (1072019441543417856, '测试页面', '/test', 1, NULL, 1, 0); | |||||
INSERT INTO `sec_permission` | |||||
VALUES (1072019441564389376, '测试页面-查询', NULL, 2, 'test:query', 1, 1072019441543417856); | |||||
INSERT INTO `sec_permission` | |||||
VALUES (1072019441576972288, '测试页面-添加', NULL, 2, 'test:insert', 2, 1072019441543417856); | |||||
COMMIT; | |||||
-- ---------------------------- | |||||
-- Table structure for sec_role | |||||
-- ---------------------------- | |||||
DROP TABLE IF EXISTS `sec_role`; | |||||
CREATE TABLE `sec_role` | |||||
( | ( | ||||
`id` BIGINT(64) NOT NULL COMMENT '主键', | |||||
`name` VARCHAR(50) NOT NULL COMMENT '角色名', | |||||
`description` VARCHAR(100) DEFAULT NULL COMMENT '描述', | |||||
`create_time` BIGINT(13) NOT NULL COMMENT '创建时间', | |||||
`update_time` BIGINT(13) NOT NULL COMMENT '更新时间', | |||||
`id` bigint(64) NOT NULL COMMENT '主键', | |||||
`name` varchar(50) NOT NULL COMMENT '角色名', | |||||
`description` varchar(100) DEFAULT NULL COMMENT '描述', | |||||
`create_time` bigint(13) NOT NULL COMMENT '创建时间', | |||||
`update_time` bigint(13) NOT NULL COMMENT '更新时间', | |||||
PRIMARY KEY (`id`), | PRIMARY KEY (`id`), | ||||
UNIQUE KEY `name` (`name`) | UNIQUE KEY `name` (`name`) | ||||
) ENGINE = InnoDB | ) ENGINE = InnoDB | ||||
DEFAULT CHARSET = utf8 COMMENT '角色表'; | |||||
DEFAULT CHARSET = utf8 COMMENT ='角色表'; | |||||
-- ---------------------------- | |||||
-- Records of sec_role | |||||
-- ---------------------------- | |||||
BEGIN; | |||||
INSERT INTO `sec_role` | |||||
VALUES (1072019441480503296, '管理员', '超级管理员', 1544424326664, 1544424326664); | |||||
INSERT INTO `sec_role` | |||||
VALUES (1072019441501474816, '普通用户', '普通用户', 1544424326669, 1544424326669); | |||||
COMMIT; | |||||
# 权限表 | |||||
CREATE TABLE IF NOT EXISTS `sec_permission` | |||||
-- ---------------------------- | |||||
-- Table structure for sec_role_permission | |||||
-- ---------------------------- | |||||
DROP TABLE IF EXISTS `sec_role_permission`; | |||||
CREATE TABLE `sec_role_permission` | |||||
( | ( | ||||
`id` BIGINT(64) NOT NULL COMMENT '主键', | |||||
`name` VARCHAR(50) NOT NULL COMMENT '权限名', | |||||
`href` VARCHAR(1000) DEFAULT NULL COMMENT '页面地址', | |||||
`type` TINYINT(1) NOT NULL COMMENT '权限类型,页面-1,按钮-2', | |||||
`permission` VARCHAR(50) DEFAULT NULL COMMENT '权限表达式', | |||||
`sort` INT(11) NOT NULL COMMENT '排序', | |||||
`parent_id` BIGINT(64) NOT NULL COMMENT '父级id', | |||||
PRIMARY KEY (`id`) | |||||
`role_id` bigint(64) NOT NULL COMMENT '角色主键', | |||||
`permission_id` bigint(64) NOT NULL COMMENT '权限主键', | |||||
PRIMARY KEY (`role_id`, `permission_id`) | |||||
) ENGINE = InnoDB | ) ENGINE = InnoDB | ||||
DEFAULT CHARSET = utf8 COMMENT '权限表'; | |||||
DEFAULT CHARSET = utf8 COMMENT ='角色权限关系表'; | |||||
-- ---------------------------- | |||||
-- Records of sec_role_permission | |||||
-- ---------------------------- | |||||
BEGIN; | |||||
INSERT INTO `sec_role_permission` | |||||
VALUES (1072019441480503296, 1072019441543417856); | |||||
INSERT INTO `sec_role_permission` | |||||
VALUES (1072019441480503296, 1072019441564389376); | |||||
INSERT INTO `sec_role_permission` | |||||
VALUES (1072019441480503296, 1072019441576972288); | |||||
INSERT INTO `sec_role_permission` | |||||
VALUES (1072019441501474816, 1072019441543417856); | |||||
INSERT INTO `sec_role_permission` | |||||
VALUES (1072019441501474816, 1072019441564389376); | |||||
COMMIT; | |||||
# 用户角色关系表 | |||||
CREATE TABLE IF NOT EXISTS `sec_user_role` | |||||
-- ---------------------------- | |||||
-- Table structure for sec_user | |||||
-- ---------------------------- | |||||
DROP TABLE IF EXISTS `sec_user`; | |||||
CREATE TABLE `sec_user` | |||||
( | ( | ||||
`user_id` BIGINT(64) NOT NULL COMMENT '用户主键', | |||||
`role_id` BIGINT(64) NOT NULL COMMENT '角色主键', | |||||
PRIMARY KEY (`user_id`, `role_id`) | |||||
`id` bigint(64) NOT NULL COMMENT '主键', | |||||
`username` varchar(50) NOT NULL COMMENT '用户名', | |||||
`password` varchar(60) NOT NULL COMMENT '密码', | |||||
`nickname` varchar(255) DEFAULT NULL COMMENT '昵称', | |||||
`phone` varchar(11) DEFAULT NULL COMMENT '手机', | |||||
`email` varchar(50) DEFAULT NULL COMMENT '邮箱', | |||||
`birthday` bigint(13) DEFAULT NULL COMMENT '生日', | |||||
`sex` int(2) DEFAULT NULL COMMENT '性别,男-1,女-2', | |||||
`status` int(2) NOT NULL DEFAULT '1' COMMENT '状态,启用-1,禁用-0', | |||||
`create_time` bigint(13) NOT NULL COMMENT '创建时间', | |||||
`update_time` bigint(13) NOT NULL COMMENT '更新时间', | |||||
PRIMARY KEY (`id`), | |||||
UNIQUE KEY `username` (`username`), | |||||
UNIQUE KEY `phone` (`phone`), | |||||
UNIQUE KEY `email` (`email`) | |||||
) ENGINE = InnoDB | ) ENGINE = InnoDB | ||||
DEFAULT CHARSET = utf8 COMMENT '用户角色关系表'; | |||||
DEFAULT CHARSET = utf8 COMMENT ='用户表'; | |||||
-- ---------------------------- | |||||
-- Records of sec_user | |||||
-- ---------------------------- | |||||
BEGIN; | |||||
INSERT INTO `sec_user` | |||||
VALUES (1072019440205434880, 'role', '$2a$10$TwdumjYKUYRQvc3VC8dleOlWr4Q2TysQtfmCMplOWygOzyfrKQee2', '管理员', | |||||
'17300000000', 'role@xkcoding.com', 785433600000, 1, 1, 1544424326483, 1544424326483); | |||||
INSERT INTO `sec_user` | |||||
VALUES (1072019441035907072, 'user', '$2a$10$8hL7INOxQCzDzI08GGYNCOFKV6mjDcOqhJ/7c1VeF9agO.wBS3ylq', '普通用户', | |||||
'17300001111', 'user@xkcoding.com', 785433600000, 1, 1, 1544424326659, 1544424326659); | |||||
COMMIT; | |||||
# 角色权限关系表 | |||||
CREATE TABLE IF NOT EXISTS `sec_role_permission` | |||||
-- ---------------------------- | |||||
-- Table structure for sec_user_role | |||||
-- ---------------------------- | |||||
DROP TABLE IF EXISTS `sec_user_role`; | |||||
CREATE TABLE `sec_user_role` | |||||
( | ( | ||||
`role_id` BIGINT(64) NOT NULL COMMENT '角色主键', | |||||
`permission_id` BIGINT(64) NOT NULL COMMENT '权限主键', | |||||
PRIMARY KEY (`role_id`, `permission_id`) | |||||
`user_id` bigint(64) NOT NULL COMMENT '用户主键', | |||||
`role_id` bigint(64) NOT NULL COMMENT '角色主键', | |||||
PRIMARY KEY (`user_id`, `role_id`) | |||||
) ENGINE = InnoDB | ) ENGINE = InnoDB | ||||
DEFAULT CHARSET = utf8 COMMENT '角色权限关系表'; | |||||
DEFAULT CHARSET = utf8 COMMENT ='用户角色关系表'; | |||||
-- ---------------------------- | |||||
-- Records of sec_user_role | |||||
-- ---------------------------- | |||||
BEGIN; | |||||
INSERT INTO `sec_user_role` | |||||
VALUES (1072019440205434880, 1072019441480503296); | |||||
INSERT INTO `sec_user_role` | |||||
VALUES (1072019441035907072, 1072019441501474816); | |||||
COMMIT; | |||||
SET FOREIGN_KEY_CHECKS = 1; |
@@ -3,6 +3,19 @@ package com.xkcoding.rbac.security; | |||||
import org.springframework.boot.SpringApplication; | import org.springframework.boot.SpringApplication; | ||||
import org.springframework.boot.autoconfigure.SpringBootApplication; | import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
/** | |||||
* <p> | |||||
* 启动器 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.rbac.security | |||||
* @description: 启动器 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018-12-10 11:28 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@SpringBootApplication | @SpringBootApplication | ||||
public class SpringBootDemoRbacSecurityApplication { | public class SpringBootDemoRbacSecurityApplication { | ||||
@@ -0,0 +1,30 @@ | |||||
package com.xkcoding.rbac.security.config; | |||||
import cn.hutool.core.lang.Snowflake; | |||||
import cn.hutool.core.util.IdUtil; | |||||
import org.springframework.context.annotation.Bean; | |||||
import org.springframework.context.annotation.Configuration; | |||||
/** | |||||
* <p> | |||||
* 雪花主键生成器 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.rbac.security.config | |||||
* @description: 雪花主键生成器 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018-12-10 11:28 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Configuration | |||||
public class IdConfig { | |||||
/** | |||||
* 雪花生成器 | |||||
*/ | |||||
@Bean | |||||
public Snowflake snowflake() { | |||||
return IdUtil.createSnowflake(1, 1); | |||||
} | |||||
} |
@@ -43,7 +43,7 @@ public class Permission { | |||||
/** | /** | ||||
* 权限类型,页面-1,按钮-2 | * 权限类型,页面-1,按钮-2 | ||||
*/ | */ | ||||
private String type; | |||||
private Integer type; | |||||
/** | /** | ||||
* 权限表达式 | * 权限表达式 | ||||
@@ -53,11 +53,11 @@ public class Permission { | |||||
/** | /** | ||||
* 排序 | * 排序 | ||||
*/ | */ | ||||
private String sort; | |||||
private Integer sort; | |||||
/** | /** | ||||
* 父级id | * 父级id | ||||
*/ | */ | ||||
@Column(name = "parent_id") | @Column(name = "parent_id") | ||||
private String parentId; | |||||
private Long parentId; | |||||
} | } |
@@ -0,0 +1,32 @@ | |||||
package com.xkcoding.rbac.security.model; | |||||
import com.xkcoding.rbac.security.model.unionkey.RolePermissionKey; | |||||
import lombok.Data; | |||||
import javax.persistence.EmbeddedId; | |||||
import javax.persistence.Entity; | |||||
import javax.persistence.Table; | |||||
/** | |||||
* <p> | |||||
* 角色-权限 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.rbac.security.model | |||||
* @description: 角色-权限 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018-12-10 13:46 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Data | |||||
@Entity | |||||
@Table(name = "sec_role_permission") | |||||
public class RolePermission { | |||||
/** | |||||
* 主键 | |||||
*/ | |||||
@EmbeddedId | |||||
private RolePermissionKey id; | |||||
} |
@@ -0,0 +1,32 @@ | |||||
package com.xkcoding.rbac.security.model; | |||||
import com.xkcoding.rbac.security.model.unionkey.UserRoleKey; | |||||
import lombok.Data; | |||||
import javax.persistence.EmbeddedId; | |||||
import javax.persistence.Entity; | |||||
import javax.persistence.Table; | |||||
/** | |||||
* <p> | |||||
* 用户角色关联 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.rbac.security.model | |||||
* @description: 用户角色关联 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018-12-10 11:18 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Data | |||||
@Entity | |||||
@Table(name = "sec_user_role") | |||||
public class UserRole { | |||||
/** | |||||
* 主键 | |||||
*/ | |||||
@EmbeddedId | |||||
private UserRoleKey id; | |||||
} |
@@ -0,0 +1,38 @@ | |||||
package com.xkcoding.rbac.security.model.unionkey; | |||||
import lombok.Data; | |||||
import javax.persistence.Column; | |||||
import javax.persistence.Embeddable; | |||||
import java.io.Serializable; | |||||
/** | |||||
* <p> | |||||
* 角色-权限联合主键 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.rbac.security.model.unionkey | |||||
* @description: 角色-权限联合主键 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018-12-10 13:47 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Data | |||||
@Embeddable | |||||
public class RolePermissionKey implements Serializable { | |||||
private static final long serialVersionUID = 6850974328279713855L; | |||||
/** | |||||
* 角色id | |||||
*/ | |||||
@Column(name = "role_id") | |||||
private Long roleId; | |||||
/** | |||||
* 权限id | |||||
*/ | |||||
@Column(name = "permission_id") | |||||
private Long permissionId; | |||||
} |
@@ -0,0 +1,38 @@ | |||||
package com.xkcoding.rbac.security.model.unionkey; | |||||
import lombok.Data; | |||||
import javax.persistence.Column; | |||||
import javax.persistence.Embeddable; | |||||
import java.io.Serializable; | |||||
/** | |||||
* <p> | |||||
* 用户-角色联合主键 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.rbac.security.model.unionkey | |||||
* @description: 用户-角色联合主键 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018-12-10 11:20 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Embeddable | |||||
@Data | |||||
public class UserRoleKey implements Serializable { | |||||
private static final long serialVersionUID = 5633412144183654743L; | |||||
/** | |||||
* 用户id | |||||
*/ | |||||
@Column(name = "user_id") | |||||
private Long userId; | |||||
/** | |||||
* 角色id | |||||
*/ | |||||
@Column(name = "role_id") | |||||
private Long roleId; | |||||
} |
@@ -1,8 +1,11 @@ | |||||
package com.xkcoding.rbac.security.repository; | package com.xkcoding.rbac.security.repository; | ||||
import com.xkcoding.rbac.security.model.Permission; | import com.xkcoding.rbac.security.model.Permission; | ||||
import org.springframework.data.jpa.domain.Specification; | |||||
import org.springframework.data.jpa.repository.JpaRepository; | import org.springframework.data.jpa.repository.JpaRepository; | ||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | |||||
import org.springframework.data.jpa.repository.Query; | |||||
import java.util.List; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
@@ -17,5 +20,14 @@ import org.springframework.data.jpa.repository.JpaRepository; | |||||
* @version: V1.0 | * @version: V1.0 | ||||
* @modified: yangkai.shen | * @modified: yangkai.shen | ||||
*/ | */ | ||||
public interface PermissionDao extends JpaRepository<Permission, Long>, Specification<Permission> { | |||||
public interface PermissionDao extends JpaRepository<Permission, Long>, JpaSpecificationExecutor<Permission> { | |||||
/** | |||||
* 根据角色列表查询权限列表 | |||||
* | |||||
* @param ids 角色id列表 | |||||
* @return 权限列表 | |||||
*/ | |||||
@Query(value = "SELECT DISTINCT sec_permission.* FROM sec_permission,sec_role,sec_role_permission WHERE sec_role.id = sec_role_permission.role_id AND sec_permission.id = sec_role_permission.permission_id AND sec_role.id IN (:ids)", nativeQuery = true) | |||||
List<Permission> selectByRoleIdList(List<Long> ids); | |||||
} | } |
@@ -1,8 +1,11 @@ | |||||
package com.xkcoding.rbac.security.repository; | package com.xkcoding.rbac.security.repository; | ||||
import com.xkcoding.rbac.security.model.Role; | import com.xkcoding.rbac.security.model.Role; | ||||
import org.springframework.data.jpa.domain.Specification; | |||||
import org.springframework.data.jpa.repository.JpaRepository; | import org.springframework.data.jpa.repository.JpaRepository; | ||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | |||||
import org.springframework.data.jpa.repository.Query; | |||||
import java.util.List; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
@@ -17,5 +20,13 @@ import org.springframework.data.jpa.repository.JpaRepository; | |||||
* @version: V1.0 | * @version: V1.0 | ||||
* @modified: yangkai.shen | * @modified: yangkai.shen | ||||
*/ | */ | ||||
public interface RoleDao extends JpaRepository<Role, Long>, Specification<Role> { | |||||
public interface RoleDao extends JpaRepository<Role, Long>, JpaSpecificationExecutor<Role> { | |||||
/** | |||||
* 根据用户id 查询角色列表 | |||||
* | |||||
* @param userId 用户id | |||||
* @return 角色列表 | |||||
*/ | |||||
@Query(value = "SELECT sec_role.* FROM sec_role,sec_user,sec_user_role WHERE sec_user.id = sec_user_role.user_id AND sec_role.id = sec_user_role.role_id AND sec_user.id = :userId", nativeQuery = true) | |||||
List<Role> selectByUserId(Long userId); | |||||
} | } |
@@ -0,0 +1,22 @@ | |||||
package com.xkcoding.rbac.security.repository; | |||||
import com.xkcoding.rbac.security.model.RolePermission; | |||||
import com.xkcoding.rbac.security.model.unionkey.RolePermissionKey; | |||||
import org.springframework.data.jpa.repository.JpaRepository; | |||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | |||||
/** | |||||
* <p> | |||||
* 角色-权限 DAO | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.rbac.security.repository | |||||
* @description: 角色-权限 DAO | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018-12-10 13:45 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
public interface RolePermissionDao extends JpaRepository<RolePermission, RolePermissionKey>, JpaSpecificationExecutor<RolePermission> { | |||||
} |
@@ -1,8 +1,11 @@ | |||||
package com.xkcoding.rbac.security.repository; | package com.xkcoding.rbac.security.repository; | ||||
import com.xkcoding.rbac.security.model.Permission; | |||||
import com.xkcoding.rbac.security.model.User; | import com.xkcoding.rbac.security.model.User; | ||||
import org.springframework.data.jpa.domain.Specification; | |||||
import org.springframework.data.jpa.repository.JpaRepository; | import org.springframework.data.jpa.repository.JpaRepository; | ||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | |||||
import java.util.Optional; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
@@ -17,5 +20,14 @@ import org.springframework.data.jpa.repository.JpaRepository; | |||||
* @version: V1.0 | * @version: V1.0 | ||||
* @modified: yangkai.shen | * @modified: yangkai.shen | ||||
*/ | */ | ||||
public interface UserDao extends JpaRepository<User, Long>, Specification<User> { | |||||
public interface UserDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> { | |||||
/** | |||||
* 根据用户名、邮箱、手机号查询用户 | |||||
* | |||||
* @param username 用户名 | |||||
* @param email 邮箱 | |||||
* @param phone 手机号 | |||||
* @return 用户信息 | |||||
*/ | |||||
Optional<User> findByUsernameOrEmailOrPhone(String username, String email, String phone); | |||||
} | } |
@@ -0,0 +1,23 @@ | |||||
package com.xkcoding.rbac.security.repository; | |||||
import com.xkcoding.rbac.security.model.UserRole; | |||||
import com.xkcoding.rbac.security.model.unionkey.UserRoleKey; | |||||
import org.springframework.data.jpa.repository.JpaRepository; | |||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | |||||
/** | |||||
* <p> | |||||
* 用户角色 DAO | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.rbac.security.repository | |||||
* @description: 用户角色 DAO | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018-12-10 11:24 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
public interface UserRoleDao extends JpaRepository<UserRole, UserRoleKey>, JpaSpecificationExecutor<UserRole> { | |||||
} |
@@ -5,10 +5,10 @@ server: | |||||
spring: | spring: | ||||
datasource: | datasource: | ||||
hikari: | hikari: | ||||
jdbc-url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 | |||||
username: root | username: root | ||||
password: root | password: root | ||||
driver-class-name: com.mysql.cj.jdbc.Driver | |||||
driver-class-name: com.mysql.cj.jdbc.Driver | |||||
url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 | |||||
jpa: | jpa: | ||||
show-sql: true | show-sql: true | ||||
generate-ddl: false | generate-ddl: false | ||||
@@ -0,0 +1,137 @@ | |||||
package com.xkcoding.rbac.security.repository; | |||||
import cn.hutool.core.date.DateTime; | |||||
import cn.hutool.core.date.DateUtil; | |||||
import cn.hutool.core.lang.Snowflake; | |||||
import com.xkcoding.rbac.security.SpringBootDemoRbacSecurityApplicationTests; | |||||
import com.xkcoding.rbac.security.model.*; | |||||
import com.xkcoding.rbac.security.model.unionkey.RolePermissionKey; | |||||
import com.xkcoding.rbac.security.model.unionkey.UserRoleKey; | |||||
import org.junit.Test; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | |||||
/** | |||||
* <p> | |||||
* 数据初始化测试 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.rbac.security.repository | |||||
* @description: 数据初始化测试 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018-12-10 11:26 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
public class DataInitTest extends SpringBootDemoRbacSecurityApplicationTests { | |||||
@Autowired | |||||
private UserDao userDao; | |||||
@Autowired | |||||
private RoleDao roleDao; | |||||
@Autowired | |||||
private PermissionDao permissionDao; | |||||
@Autowired | |||||
private UserRoleDao userRoleDao; | |||||
@Autowired | |||||
private RolePermissionDao rolePermissionDao; | |||||
@Autowired | |||||
private Snowflake snowflake; | |||||
@Autowired | |||||
private BCryptPasswordEncoder encoder; | |||||
@Test | |||||
public void initTest() { | |||||
init(); | |||||
} | |||||
private void init() { | |||||
User admin = createUser(true); | |||||
User user = createUser(false); | |||||
Role roleAdmin = createRole(true); | |||||
Role roleUser = createRole(false); | |||||
createUserRoleRelation(admin.getId(), roleAdmin.getId()); | |||||
createUserRoleRelation(user.getId(), roleUser.getId()); | |||||
// 页面权限 | |||||
Permission pagePerm = createPermission("/test", "测试页面", 1, null, 1, 0L); | |||||
// 按钮权限 | |||||
Permission btnQueryPerm = createPermission(null, "测试页面-查询", 2, "test:query", 1, pagePerm.getId()); | |||||
Permission btnPermInsert = createPermission(null, "测试页面-添加", 2, "test:insert", 2, pagePerm.getId()); | |||||
createRolePermissionRelation(roleAdmin.getId(), pagePerm.getId()); | |||||
createRolePermissionRelation(roleUser.getId(), pagePerm.getId()); | |||||
createRolePermissionRelation(roleAdmin.getId(), btnQueryPerm.getId()); | |||||
createRolePermissionRelation(roleUser.getId(), btnQueryPerm.getId()); | |||||
createRolePermissionRelation(roleAdmin.getId(), btnPermInsert.getId()); | |||||
} | |||||
private void createRolePermissionRelation(Long roleId, Long permissionId) { | |||||
RolePermission adminPage = new RolePermission(); | |||||
RolePermissionKey adminPageKey = new RolePermissionKey(); | |||||
adminPageKey.setRoleId(roleId); | |||||
adminPageKey.setPermissionId(permissionId); | |||||
adminPage.setId(adminPageKey); | |||||
rolePermissionDao.save(adminPage); | |||||
} | |||||
private Permission createPermission(String href, String name, Integer type, String permission, Integer sort, Long parentId) { | |||||
// 页面权限 | |||||
Permission perm = new Permission(); | |||||
perm.setId(snowflake.nextId()); | |||||
perm.setHref(href); | |||||
perm.setName(name); | |||||
perm.setType(type); | |||||
perm.setPermission(permission); | |||||
perm.setSort(sort); | |||||
perm.setParentId(parentId); | |||||
permissionDao.save(perm); | |||||
return perm; | |||||
} | |||||
private void createUserRoleRelation(Long userId, Long roleId) { | |||||
UserRole userRole = new UserRole(); | |||||
UserRoleKey key = new UserRoleKey(); | |||||
key.setUserId(userId); | |||||
key.setRoleId(roleId); | |||||
userRole.setId(key); | |||||
userRoleDao.save(userRole); | |||||
} | |||||
private Role createRole(boolean isAdmin) { | |||||
Role role = new Role(); | |||||
role.setId(snowflake.nextId()); | |||||
role.setName(isAdmin ? "管理员" : "普通用户"); | |||||
role.setDescription(isAdmin ? "超级管理员" : "普通用户"); | |||||
role.setCreateTime(DateUtil.current(false)); | |||||
role.setUpdateTime(DateUtil.current(false)); | |||||
roleDao.save(role); | |||||
return role; | |||||
} | |||||
private User createUser(boolean isAdmin) { | |||||
User user = new User(); | |||||
user.setId(snowflake.nextId()); | |||||
user.setUsername(isAdmin ? "role" : "user"); | |||||
user.setNickname(isAdmin ? "管理员" : "普通用户"); | |||||
user.setPassword(encoder.encode("123456")); | |||||
user.setBirthday(DateTime.of("1994-11-22", "yyyy-MM-dd").getTime()); | |||||
user.setEmail((isAdmin ? "role" : "user") + "@xkcoding.com"); | |||||
user.setPhone(isAdmin ? "17300000000" : "17300001111"); | |||||
user.setSex(1); | |||||
user.setStatus(1); | |||||
user.setCreateTime(DateUtil.current(false)); | |||||
user.setUpdateTime(DateUtil.current(false)); | |||||
userDao.save(user); | |||||
return user; | |||||
} | |||||
} |