+ * 通用分页参数返回 + *
+ * + * @package: com.xkcoding.rbac.security.common + * @description: 通用分页参数返回 + * @author: yangkai.shen + * @date: Created in 2018-12-11 20:26 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class PageResult+ * 监控 Controller,在线用户,手动踢出用户等功能 + *
+ * + * @package: com.xkcoding.rbac.security.controller + * @description: 监控 Controller,在线用户,手动踢出用户等功能 + * @author: yangkai.shen + * @date: Created in 2018-12-11 20:55 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Slf4j +@RestController +@RequestMapping("/api/monitor") +public class MonitorController { + @Autowired + private MonitorService monitorService; + + /** + * 在线用户列表 + * + * @param page 当前页码 + * @param size 每页条数 + */ + @PostMapping("/online/user/{page}/{size}") + public ApiResponse onlineUser(@PathVariable Integer page, @PathVariable Integer size) { + PageResult+ * 监控 Service + *
+ * + * @package: com.xkcoding.rbac.security.service + * @description: 监控 Service + * @author: yangkai.shen + * @date: Created in 2018-12-12 00:55 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Service +public class MonitorService { + @Autowired + private RedisUtil redisUtil; + + @Autowired + private UserDao userDao; + + /** + * 在线用户分页列表 + * + * @param page 当前页 + * @param size 每页条数 + * @return 在线用户分页列表 + */ + public PageResult+ * Redis工具类 + *
+ * + * @package: com.xkcoding.rbac.security.util + * @description: Redis工具类 + * @author: yangkai.shen + * @date: Created in 2018-12-11 20:24 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Component +@Slf4j +public class RedisUtil { + @Autowired + private StringRedisTemplate stringRedisTemplate; + + /** + * 分页获取指定格式key,使用 scan 命令代替 keys 命令,在大数据量的情况下可以提高查询效率 + * + * @param patternKey key格式 + * @param currentPage 当前页码 + * @param pageSize 每页条数 + * @return 分页获取指定格式key + */ + public PageResult+ * 在线用户 VO + *
+ * + * @package: com.xkcoding.rbac.security.vo + * @description: 在线用户 VO + * @author: yangkai.shen + * @date: Created in 2018-12-12 00:58 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +public class OnlineUser { + + /** + * 主键 + */ + private Long id; + + /** + * 用户名 + */ + private String username; + + /** + * 昵称 + */ + private String nickname; + + /** + * 手机 + */ + private String phone; + + /** + * 邮箱 + */ + private String email; + + /** + * 生日 + */ + private Long birthday; + + /** + * 性别,男-1,女-2 + */ + private Integer sex; + + public static OnlineUser create(User user) { + OnlineUser onlineUser = new OnlineUser(); + BeanUtil.copyProperties(user, onlineUser); + // 脱敏 + onlineUser.setPhone(StrUtil.hide(user.getPhone(), 3, 7)); + onlineUser.setEmail(StrUtil.hide(user.getEmail(), 1, StrUtil.indexOfIgnoreCase(user.getEmail(), Consts.SYMBOL_EMAIL))); + return onlineUser; + } +} diff --git a/spring-boot-demo-rbac-security/src/test/java/com/xkcoding/rbac/security/repository/DataInitTest.java b/spring-boot-demo-rbac-security/src/test/java/com/xkcoding/rbac/security/repository/DataInitTest.java index 74d1d94..8068414 100644 --- a/spring-boot-demo-rbac-security/src/test/java/com/xkcoding/rbac/security/repository/DataInitTest.java +++ b/spring-boot-demo-rbac-security/src/test/java/com/xkcoding/rbac/security/repository/DataInitTest.java @@ -62,16 +62,21 @@ public class DataInitTest extends SpringBootDemoRbacSecurityApplicationTests { createUserRoleRelation(user.getId(), roleUser.getId()); // 页面权限 - Permission pagePerm = createPermission("/test", "测试页面", 1, "page:test", null, 1, 0L); + Permission testPagePerm = createPermission("/test", "测试页面", 1, "page:test", null, 1, 0L); // 按钮权限 - Permission btnQueryPerm = createPermission("/**/test", "测试页面-查询", 2, "btn:test:query", "GET", 1, pagePerm.getId()); - Permission btnPermInsert = createPermission("/**/test", "测试页面-添加", 2, "btn:test:insert", "POST", 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()); + Permission testBtnQueryPerm = createPermission("/**/test", "测试页面-查询", 2, "btn:test:query", "GET", 1, testPagePerm.getId()); + Permission testBtnPermInsert = createPermission("/**/test", "测试页面-添加", 2, "btn:test:insert", "POST", 2, testPagePerm.getId()); + + Permission monitorOnlinePagePerm = createPermission("/monitor", "监控在线用户页面", 1, "page:monitor:online", null, 2, 0L); + Permission monitorOnlineBtnQueryPerm = createPermission("/**/api/monitor/online/user/**", "在线用户页面-查询", 2, "btn:monitor:online:query", "POST", 1, monitorOnlinePagePerm.getId()); + + createRolePermissionRelation(roleAdmin.getId(), testPagePerm.getId()); + createRolePermissionRelation(roleUser.getId(), testPagePerm.getId()); + createRolePermissionRelation(roleAdmin.getId(), testBtnQueryPerm.getId()); + createRolePermissionRelation(roleUser.getId(), testBtnQueryPerm.getId()); + createRolePermissionRelation(roleAdmin.getId(), testBtnPermInsert.getId()); + createRolePermissionRelation(roleAdmin.getId(), monitorOnlinePagePerm.getId()); + createRolePermissionRelation(roleAdmin.getId(), monitorOnlineBtnQueryPerm.getId()); } private void createRolePermissionRelation(Long roleId, Long permissionId) { diff --git a/spring-boot-demo-rbac-security/src/test/java/com/xkcoding/rbac/security/repository/UserDaoTest.java b/spring-boot-demo-rbac-security/src/test/java/com/xkcoding/rbac/security/repository/UserDaoTest.java new file mode 100644 index 0000000..1c9bdb1 --- /dev/null +++ b/spring-boot-demo-rbac-security/src/test/java/com/xkcoding/rbac/security/repository/UserDaoTest.java @@ -0,0 +1,39 @@ +package com.xkcoding.rbac.security.repository; + +import com.xkcoding.rbac.security.SpringBootDemoRbacSecurityApplicationTests; +import com.xkcoding.rbac.security.model.User; +import lombok.extern.slf4j.Slf4j; +import org.assertj.core.util.Lists; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; +import java.util.Optional; + +/** + *+ * UserDao 测试 + *
+ * + * @package: com.xkcoding.rbac.security.repository + * @description: UserDao 测试 + * @author: yangkai.shen + * @date: Created in 2018-12-12 01:10 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Slf4j +public class UserDaoTest extends SpringBootDemoRbacSecurityApplicationTests { + @Autowired + private UserDao userDao; + + @Test + public void findByUsernameIn() { + List+ * 测试RedisUtil + *
+ * + * @package: com.xkcoding.rbac.security.util + * @description: 测试RedisUtil + * @author: yangkai.shen + * @date: Created in 2018-12-11 20:44 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Slf4j +public class RedisUtilTest extends SpringBootDemoRbacSecurityApplicationTests { + @Autowired + private RedisUtil redisUtil; + + @Test + public void findKeysForPage() { + PageResult pageResult = redisUtil.findKeysForPage(Consts.REDIS_JWT_KEY_PREFIX + Consts.SYMBOL_STAR, 2, 1); + log.info("【pageResult】= {}", JSONUtil.toJsonStr(pageResult)); + } +} \ No newline at end of file