From 5b723d02b23f7be2dd78760892af19228ad85e01 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 10 Oct 2021 01:34:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20yudao-core-service=20?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=8F=90=E4=BE=9B=E5=85=B1=E4=BA=AB?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + yudao-admin-server/pom.xml | 5 + .../adminserver/AdminServerApplication.java | 3 +- .../auth/SysUserSessionController.java | 7 +- .../system/convert/auth/SysAuthConvert.java | 12 +- .../convert/auth/SysUserSessionConvert.java | 2 +- .../dal/mysql/auth/SysUserSessionMapper.java | 5 +- .../system/dal/redis/RedisKeyConstants.java | 0 .../dal/redis/SysRedisKeyConstants.java | 4 - .../service/auth/SysUserSessionService.java | 46 +--- .../service/auth/impl/SysAuthServiceImpl.java | 20 +- .../auth/impl/SysUserSessionServiceImpl.java | 86 +------ .../src/main/resources/application.yaml | 4 +- .../yudao/adminserver/BaseRedisUnitTest.java | 2 +- .../service/file/InfFileServiceTest.java | 4 +- .../service/auth/SysAuthServiceImplTest.java | 12 +- .../auth/SysUserSessionServiceImplTest.java | 243 ++++++------------ .../logger/SysLoginLogServiceImplTest.java | 4 +- .../test/resources/application-unit-test.yaml | 25 -- .../src/test/resources/sql/create_tables.sql | 3 + yudao-core-service/pom.xml | 62 +++++ .../modules/infra/package-info.java | 7 + .../modules/member/package-info.java | 7 + .../dal/dataobject/auth/SysUserSessionDO.java | 6 +- .../system/dal/dataobject/package-info.java | 1 + .../mysql/auth/SysUserSessionCoreMapper.java | 10 + .../system/dal/mysql/package-info.java | 1 + .../dal/redis/SysRedisKeyCoreConstants.java | 19 ++ .../redis/auth/SysLoginUserCoreRedisDAO.java | 16 +- .../modules/system/package-info.java | 7 + .../auth/SysUserSessionCoreService.java | 13 +- .../impl/SysUserSessionCoreServiceImpl.java | 93 +++++++ .../modules/system/service/package-info.java | 1 + .../modules/tool/package-info.java | 7 + .../coreservice/BaseDbAndRedisUnitTest.java | 48 ++++ .../yudao/coreservice/BaseDbUnitTest.java | 39 +++ .../yudao/coreservice/BaseRedisUnitTest.java | 32 +++ .../config/RedisTestConfiguration.java | 30 +++ .../auth/SysUserSessionCoreServiceTest.java | 122 +++++++++ .../modules/system/service/package-info.java | 1 + .../test/resources/application-unit-test.yaml | 44 ++++ .../src/test/resources/application.yaml | 35 +++ .../src/test/resources/logback-spring.xml | 4 + .../src/test/resources/sql/clean.sql | 4 + .../src/test/resources/sql/create_tables.sql | 20 ++ yudao-dependencies/pom.xml | 5 + .../config/YudaoMybatisAutoConfiguration.java | 2 +- .../framework/security/core/LoginUser.java | 7 + .../userserver/modules/package-info.java | 1 - .../auth/impl/SysUserSessionServiceImpl.java | 47 ---- 50 files changed, 766 insertions(+), 413 deletions(-) delete mode 100644 yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/RedisKeyConstants.java create mode 100644 yudao-core-service/pom.xml create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/infra/package-info.java create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/member/package-info.java rename {yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver => yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice}/modules/system/dal/dataobject/auth/SysUserSessionDO.java (89%) create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/dataobject/package-info.java create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/auth/SysUserSessionCoreMapper.java create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/package-info.java create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java rename yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/auth/SysLoginUserRedisDAO.java => yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/auth/SysLoginUserCoreRedisDAO.java (69%) create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/package-info.java rename yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysUserSessionService.java => yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/SysUserSessionCoreService.java (77%) create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/impl/SysUserSessionCoreServiceImpl.java create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/package-info.java create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/tool/package-info.java create mode 100644 yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseDbAndRedisUnitTest.java create mode 100644 yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseDbUnitTest.java create mode 100644 yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseRedisUnitTest.java create mode 100644 yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/config/RedisTestConfiguration.java create mode 100644 yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/SysUserSessionCoreServiceTest.java create mode 100644 yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/modules/system/service/package-info.java create mode 100644 yudao-core-service/src/test/resources/application-unit-test.yaml create mode 100644 yudao-core-service/src/test/resources/application.yaml create mode 100644 yudao-core-service/src/test/resources/logback-spring.xml create mode 100644 yudao-core-service/src/test/resources/sql/clean.sql create mode 100644 yudao-core-service/src/test/resources/sql/create_tables.sql delete mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/package-info.java delete mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysUserSessionServiceImpl.java diff --git a/pom.xml b/pom.xml index 84e886116..1ca158c9f 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ yudao-framework yudao-admin-server yudao-user-server + yudao-core-service ${artifactId} diff --git a/yudao-admin-server/pom.xml b/yudao-admin-server/pom.xml index 8fad2d92c..120d3e926 100644 --- a/yudao-admin-server/pom.xml +++ b/yudao-admin-server/pom.xml @@ -18,6 +18,11 @@ + + cn.iocoder.boot + yudao-core-service + + cn.iocoder.boot yudao-spring-boot-starter-biz-operatelog diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/AdminServerApplication.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/AdminServerApplication.java index ba791871d..810869768 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/AdminServerApplication.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/AdminServerApplication.java @@ -3,7 +3,8 @@ package cn.iocoder.yudao.adminserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -@SpringBootApplication +@SuppressWarnings("SpringComponentScan") // 忽略 IDEA 无法识别 ${yudao.info.base-package} 和 ${yudao.core-service.base-package} +@SpringBootApplication(scanBasePackages = {"${yudao.info.base-package}", "${yudao.core-service.base-package}"}) public class AdminServerApplication { public static void main(String[] args) { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysUserSessionController.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysUserSessionController.java index 253b25126..ee5cebfda 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysUserSessionController.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysUserSessionController.java @@ -1,11 +1,12 @@ package cn.iocoder.yudao.adminserver.modules.system.controller.auth; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.auth.SysUserSessionDO; +import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.session.SysUserSessionPageItemRespVO; import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.session.SysUserSessionPageReqVO; import cn.iocoder.yudao.adminserver.modules.system.convert.auth.SysUserSessionConvert; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.auth.SysUserSessionDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysDeptDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysUserSessionService; @@ -35,6 +36,8 @@ public class SysUserSessionController { @Resource private SysUserSessionService userSessionService; @Resource + private SysUserSessionCoreService userSessionCoreService; + @Resource private SysUserService userService; @Resource private SysDeptService deptService; @@ -72,7 +75,7 @@ public class SysUserSessionController { example = "fe50b9f6-d177-44b1-8da9-72ea34f63db7") @PreAuthorize("@ss.hasPermission('system:user-session:delete')") public CommonResult deleteUserSession(@RequestParam("id") String id) { - userSessionService.deleteUserSession(id); + userSessionCoreService.deleteUserSession(id); return success(true); } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/auth/SysAuthConvert.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/auth/SysAuthConvert.java index 65d79a898..37ce51fec 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/auth/SysAuthConvert.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/auth/SysAuthConvert.java @@ -10,6 +10,7 @@ import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.Sys import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysRoleDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.yudao.adminserver.modules.system.enums.permission.MenuIdEnum; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.security.core.LoginUser; import me.zhyd.oauth.model.AuthCallback; @@ -27,7 +28,12 @@ public interface SysAuthConvert { @Mapping(source = "updateTime", target = "updateTime", ignore = true) // 字段相同,但是含义不同,忽略 - LoginUser convert(SysUserDO bean); + LoginUser convert0(SysUserDO bean); + + default LoginUser convert(SysUserDO bean) { + // 目的,为了设置 UserTypeEnum.ADMIN.getValue() + return convert0(bean).setUserType(UserTypeEnum.ADMIN.getValue()); + } default SysAuthPermissionInfoRespVO convert(SysUserDO user, List roleList, List menuList) { return SysAuthPermissionInfoRespVO.builder() @@ -39,10 +45,6 @@ public interface SysAuthConvert { SysAuthMenuRespVO convertTreeNode(SysMenuDO menu); - LoginUser convert(SysUserProfileUpdateReqVO reqVO); - - LoginUser convert(SysUserProfileUpdatePasswordReqVO reqVO); - /** * 将菜单列表,构建成菜单树 * diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/auth/SysUserSessionConvert.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/auth/SysUserSessionConvert.java index 473fa911c..0defeed97 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/auth/SysUserSessionConvert.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/auth/SysUserSessionConvert.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.adminserver.modules.system.convert.auth; import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.session.SysUserSessionPageItemRespVO; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.auth.SysUserSessionDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.auth.SysUserSessionDO; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/auth/SysUserSessionMapper.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/auth/SysUserSessionMapper.java index bc6e31c83..f5ae670b0 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/auth/SysUserSessionMapper.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/auth/SysUserSessionMapper.java @@ -1,10 +1,10 @@ package cn.iocoder.yudao.adminserver.modules.system.dal.mysql.auth; +import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.session.SysUserSessionPageReqVO; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.auth.SysUserSessionDO; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; -import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.session.SysUserSessionPageReqVO; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.auth.SysUserSessionDO; import org.apache.ibatis.annotations.Mapper; import java.util.Collection; @@ -23,4 +23,5 @@ public interface SysUserSessionMapper extends BaseMapperX { default List selectListBySessionTimoutLt() { return selectList(new QueryWrapperX().lt("session_timeout",new Date())); } + } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/RedisKeyConstants.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/RedisKeyConstants.java deleted file mode 100644 index e69de29bb..000000000 diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/SysRedisKeyConstants.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/SysRedisKeyConstants.java index 78109c73c..e7c484bb0 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/SysRedisKeyConstants.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/SysRedisKeyConstants.java @@ -15,10 +15,6 @@ import static cn.iocoder.yudao.framework.redis.core.RedisKeyDefine.KeyTypeEnum.S */ public interface SysRedisKeyConstants { - RedisKeyDefine LOGIN_USER = new RedisKeyDefine("登录用户的缓存", - "login_user:%s", // 参数为 sessionId - STRING, LoginUser.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); - RedisKeyDefine CAPTCHA_CODE = new RedisKeyDefine("验证码的缓存", "captcha_code:%s", // 参数为 uuid STRING, String.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionService.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionService.java index 533c83753..ebfa6606d 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionService.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionService.java @@ -1,9 +1,8 @@ package cn.iocoder.yudao.adminserver.modules.system.service.auth; -import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.security.core.LoginUser; import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.session.SysUserSessionPageReqVO; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.auth.SysUserSessionDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.auth.SysUserSessionDO; +import cn.iocoder.yudao.framework.common.pojo.PageResult; /** * 在线用户 Session Service 接口 @@ -12,46 +11,6 @@ import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.auth.SysUserSe */ public interface SysUserSessionService { - /** - * 创建在线用户 Session - * - * @param loginUser 登录用户 - * @param userIp 用户 IP - * @param userAgent 用户 UA - * @return Session 编号 - */ - String createUserSession(LoginUser loginUser, String userIp, String userAgent); - - /** - * 刷新在线用户 Session 的更新时间 - * - * @param sessionId Session 编号 - * @param loginUser 登录用户 - */ - void refreshUserSession(String sessionId, LoginUser loginUser); - - /** - * 删除在线用户 Session - * - * @param sessionId Session 编号 - */ - void deleteUserSession(String sessionId); - - /** - * 获得 Session 编号对应的在线用户 - * - * @param sessionId Session 编号 - * @return 在线用户 - */ - LoginUser getLoginUser(String sessionId); - - /** - * 获得 Session 超时时间,单位:毫秒 - * - * @return 超时时间 - */ - Long getSessionTimeoutMillis(); - /** * 获得在线用户分页列表 * @@ -66,4 +25,5 @@ public interface SysUserSessionService { * @return {@link Long } 移出的超时用户数量 **/ long clearSessionTimeout(); + } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java index d1f06816b..f8063d00d 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -11,13 +11,13 @@ import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginLogTypeEnum; import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginResultEnum; import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysAuthService; -import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysUserSessionService; import cn.iocoder.yudao.adminserver.modules.system.service.common.SysCaptchaService; import cn.iocoder.yudao.adminserver.modules.system.service.logger.SysLoginLogService; import cn.iocoder.yudao.adminserver.modules.system.service.logger.dto.SysLoginLogCreateReqDTO; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService; import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService; +import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; @@ -67,7 +67,7 @@ public class SysAuthServiceImpl implements SysAuthService { @Resource private SysLoginLogService loginLogService; @Resource - private SysUserSessionService userSessionService; + private SysUserSessionCoreService userSessionCoreService; @Resource private SysSocialService socialService; @@ -107,7 +107,7 @@ public class SysAuthServiceImpl implements SysAuthService { loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 // 缓存登录用户到 Redis 中,返回 sessionId 编号 - return userSessionService.createUserSession(loginUser, userIp, userAgent); + return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); } private void verifyCaptcha(String username, String captchaUUID, String captchaCode) { @@ -214,7 +214,7 @@ public class SysAuthServiceImpl implements SysAuthService { socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser); // 缓存登录用户到 Redis 中,返回 sessionId 编号 - return userSessionService.createUserSession(loginUser, userIp, userAgent); + return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); } @Override @@ -231,7 +231,7 @@ public class SysAuthServiceImpl implements SysAuthService { socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser); // 缓存登录用户到 Redis 中,返回 sessionId 编号 - return userSessionService.createUserSession(loginUser, userIp, userAgent); + return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); } @Override @@ -247,12 +247,12 @@ public class SysAuthServiceImpl implements SysAuthService { @Override public void logout(String token) { // 查询用户信息 - LoginUser loginUser = userSessionService.getLoginUser(token); + LoginUser loginUser = userSessionCoreService.getLoginUser(token); if (loginUser == null) { return; } // 删除 session - userSessionService.deleteUserSession(token); + userSessionCoreService.deleteUserSession(token); // 记录登出日子和 this.createLogoutLog(loginUser.getUsername()); } @@ -271,7 +271,7 @@ public class SysAuthServiceImpl implements SysAuthService { @Override public LoginUser verifyTokenAndRefresh(String token) { // 获得 LoginUser - LoginUser loginUser = userSessionService.getLoginUser(token); + LoginUser loginUser = userSessionCoreService.getLoginUser(token); if (loginUser == null) { return null; } @@ -283,7 +283,7 @@ public class SysAuthServiceImpl implements SysAuthService { private void refreshLoginUserCache(String token, LoginUser loginUser) { // 每 1/3 的 Session 超时时间,刷新 LoginUser 缓存 if (System.currentTimeMillis() - loginUser.getUpdateTime().getTime() < - userSessionService.getSessionTimeoutMillis() / 3) { + userSessionCoreService.getSessionTimeoutMillis() / 3) { return; } @@ -296,7 +296,7 @@ public class SysAuthServiceImpl implements SysAuthService { // 刷新 LoginUser 缓存 loginUser.setDeptId(user.getDeptId()); loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); - userSessionService.refreshUserSession(token, loginUser); + userSessionCoreService.refreshUserSession(token, loginUser); } } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysUserSessionServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysUserSessionServiceImpl.java index fc73cb875..0da53a8f1 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysUserSessionServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysUserSessionServiceImpl.java @@ -1,35 +1,32 @@ package cn.iocoder.yudao.adminserver.modules.system.service.auth.impl; import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.session.SysUserSessionPageReqVO; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.auth.SysUserSessionDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.auth.SysUserSessionMapper; -import cn.iocoder.yudao.adminserver.modules.system.dal.redis.auth.SysLoginUserRedisDAO; import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginLogTypeEnum; import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginResultEnum; import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysUserSessionService; import cn.iocoder.yudao.adminserver.modules.system.service.logger.SysLoginLogService; import cn.iocoder.yudao.adminserver.modules.system.service.logger.dto.SysLoginLogCreateReqDTO; import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService; -import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.auth.SysUserSessionDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.redis.auth.SysLoginUserCoreRedisDAO; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; -import cn.iocoder.yudao.framework.security.config.SecurityProperties; -import cn.iocoder.yudao.framework.security.core.LoginUser; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.time.Duration; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.addTime; /** * 在线用户 Session Service 实现类 @@ -40,10 +37,6 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.addTime; @Service public class SysUserSessionServiceImpl implements SysUserSessionService { - @Resource - private SecurityProperties securityProperties; - @Resource - private SysLoginUserRedisDAO loginUserRedisDAO; @Resource private SysUserSessionMapper userSessionMapper; @Resource @@ -51,54 +44,8 @@ public class SysUserSessionServiceImpl implements SysUserSessionService { @Resource private SysLoginLogService loginLogService; - @Override - public String createUserSession(LoginUser loginUser, String userIp, String userAgent) { - // 生成 Session 编号 - String sessionId = generateSessionId(); - // 写入 Redis 缓存 - loginUser.setUpdateTime(new Date()); - loginUserRedisDAO.set(sessionId, loginUser); - // 写入 DB 中 - SysUserSessionDO userSession = SysUserSessionDO.builder().id(sessionId) - .userId(loginUser.getId()).userType(UserTypeEnum.ADMIN.getValue()) - .userIp(userIp).userAgent(userAgent).username(loginUser.getUsername()) - .sessionTimeout(addTime(Duration.ofMillis(getSessionTimeoutMillis()))) - .build(); - userSessionMapper.insert(userSession); - // 返回 Session 编号 - return sessionId; - } - - @Override - public void refreshUserSession(String sessionId, LoginUser loginUser) { - // 写入 Redis 缓存 - loginUser.setUpdateTime(new Date()); - loginUserRedisDAO.set(sessionId, loginUser); - // 更新 DB 中 - SysUserSessionDO updateObj = SysUserSessionDO.builder().id(sessionId).build(); - updateObj.setUsername(loginUser.getUsername()); - updateObj.setUpdateTime(new Date()); - updateObj.setSessionTimeout(addTime(Duration.ofMillis(getSessionTimeoutMillis()))); - userSessionMapper.updateById(updateObj); - } - - @Override - public void deleteUserSession(String sessionId) { - // 删除 Redis 缓存 - loginUserRedisDAO.delete(sessionId); - // 删除 DB 记录 - userSessionMapper.deleteById(sessionId); - } - - @Override - public LoginUser getLoginUser(String sessionId) { - return loginUserRedisDAO.get(sessionId); - } - - @Override - public Long getSessionTimeoutMillis() { - return securityProperties.getSessionTimeout().toMillis(); - } + @Resource + private SysLoginUserCoreRedisDAO loginUserCoreRedisDAO; @Override public PageResult getUserSessionPage(SysUserSessionPageReqVO reqVO) { @@ -113,18 +60,20 @@ public class SysUserSessionServiceImpl implements SysUserSessionService { return userSessionMapper.selectPage(reqVO, userIds); } + // TODO @芋艿:优化下该方法 @Override public long clearSessionTimeout() { // 获取db里已经超时的用户列表 List sessionTimeoutDOS = userSessionMapper.selectListBySessionTimoutLt(); Map timeoutSessionDOMap = sessionTimeoutDOS .stream() - .filter(sessionDO -> loginUserRedisDAO.get(sessionDO.getId()) == null) + .filter(sessionDO -> loginUserCoreRedisDAO.get(sessionDO.getId()) == null) .collect(Collectors.toMap(SysUserSessionDO::getId, o -> o)); // 确认已经超时,按批次移出在线用户列表 if (CollUtil.isNotEmpty(timeoutSessionDOMap)) { - Lists.partition(new ArrayList<>(timeoutSessionDOMap.keySet()), 100).forEach(userSessionMapper::deleteBatchIds); - //记录用户超时退出日志 + Lists.partition(new ArrayList<>(timeoutSessionDOMap.keySet()), 100) + .forEach(userSessionMapper::deleteBatchIds); + // 记录用户超时退出日志 createTimeoutLogoutLog(timeoutSessionDOMap.values()); } return timeoutSessionDOMap.size(); @@ -143,13 +92,4 @@ public class SysUserSessionServiceImpl implements SysUserSessionService { } } - /** - * 生成 Session 编号,目前采用 UUID 算法 - * - * @return Session 编号 - */ - private static String generateSessionId() { - return IdUtil.fastSimpleUUID(); - } - } diff --git a/yudao-admin-server/src/main/resources/application.yaml b/yudao-admin-server/src/main/resources/application.yaml index 486d4a487..cad787dfe 100644 --- a/yudao-admin-server/src/main/resources/application.yaml +++ b/yudao-admin-server/src/main/resources/application.yaml @@ -31,7 +31,7 @@ mybatis-plus: logic-delete-value: 1 # 逻辑已删除值(默认为 1) logic-not-delete-value: 0 # 逻辑未删除值(默认为 0) mapper-locations: classpath*:mapper/*.xml - type-aliases-package: ${yudao.info.base-package}.modules.*.dal.dataobject + type-aliases-package: ${yudao.info.base-package}.modules.*.dal.dataobject, ${yudao.core-service.base-package}.modules.*.dal.dataobject --- #################### 芋道相关配置 #################### @@ -39,6 +39,8 @@ yudao: info: version: 1.0.0 base-package: cn.iocoder.yudao.adminserver + core-service: + base-package: cn.iocoder.yudao.coreservice web: api-prefix: /api controller-package: ${yudao.info.base-package} diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/BaseRedisUnitTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/BaseRedisUnitTest.java index fa6bd636b..7e650bc3e 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/BaseRedisUnitTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/BaseRedisUnitTest.java @@ -24,7 +24,7 @@ public class BaseRedisUnitTest { // Redis 配置类 RedisTestConfiguration.class, // Redis 测试配置类,用于启动 RedisServer RedisAutoConfiguration.class, // Spring Redis 自动配置类 - YudaoTracerAutoConfiguration.class, // 自己的 Redis 配置类 + YudaoRedisAutoConfiguration.class, // 自己的 Redis 配置类 RedissonAutoConfiguration.class, // Redisson 自动高配置类 }) public static class Application { diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/file/InfFileServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/file/InfFileServiceTest.java index 260ae7c60..7643b856c 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/file/InfFileServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/file/InfFileServiceTest.java @@ -10,6 +10,7 @@ import cn.iocoder.yudao.adminserver.modules.infra.dal.mysql.file.InfFileMapper; import cn.iocoder.yudao.adminserver.modules.infra.service.file.impl.InfFileServiceImpl; import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; import org.junit.jupiter.api.Test; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import javax.annotation.Resource; @@ -29,8 +30,9 @@ public class InfFileServiceTest extends BaseDbUnitTest { @Resource private InfFileServiceImpl fileService; - @Resource + @MockBean private FileProperties fileProperties; + @Resource private InfFileMapper fileMapper; diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java index ed00a6690..8762a80fb 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java @@ -1,6 +1,8 @@ package cn.iocoder.yudao.adminserver.modules.system.service.auth; import cn.iocoder.yudao.adminserver.BaseDbUnitTest; +import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; +import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.security.core.LoginUser; import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAuthLoginReqVO; @@ -59,7 +61,9 @@ public class SysAuthServiceImplTest extends BaseDbUnitTest { @MockBean private SysLoginLogService loginLogService; @MockBean - private SysUserSessionService userSessionService; + private SysUserSessionCoreService userSessionCoreService; + @MockBean + private SysSocialService socialService; @Test public void testLoadUserByUsername_success() { @@ -237,7 +241,7 @@ public class SysAuthServiceImplTest extends BaseDbUnitTest { // mock 获得 User 拥有的角色编号数组 when(permissionService.getUserRoleIds(userId, singleton(CommonStatusEnum.ENABLE.getStatus()))).thenReturn(userRoleIds); // mock 缓存登录用户到 Redis - when(userSessionService.createUserSession(loginUser, userIp, userAgent)).thenReturn(sessionId); + when(userSessionCoreService.createUserSession(loginUser, userIp, userAgent)).thenReturn(sessionId); // 调用, 并断言异常 String login = authService.login(reqVO, userIp, userAgent); assertEquals(sessionId, login); @@ -255,11 +259,11 @@ public class SysAuthServiceImplTest extends BaseDbUnitTest { String token = randomString(); LoginUser loginUser = randomPojo(LoginUser.class); // mock - when(userSessionService.getLoginUser(token)).thenReturn(loginUser); + when(userSessionCoreService.getLoginUser(token)).thenReturn(loginUser); // 调用 authService.logout(token); // 校验调用参数 - verify(userSessionService, times(1)).deleteUserSession(token); + verify(userSessionCoreService, times(1)).deleteUserSession(token); verify(loginLogService, times(1)).createLoginLog( argThat(o -> o.getLogType().equals(SysLoginLogTypeEnum.LOGOUT_SELF.getType()) && o.getResult().equals(SysLoginResultEnum.SUCCESS.getResult())) diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionServiceImplTest.java index 740460ec9..bd8c4f613 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionServiceImplTest.java @@ -1,207 +1,106 @@ package cn.iocoder.yudao.adminserver.modules.system.service.auth; -import static cn.hutool.core.util.RandomUtil.randomEle; -import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomDate; -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.addTime; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.Mockito.when; +import cn.hutool.core.date.DateUtil; +import cn.iocoder.yudao.adminserver.BaseDbAndRedisUnitTest; +import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.session.SysUserSessionPageReqVO; +import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO; +import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.auth.SysUserSessionMapper; +import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.user.SysUserMapper; +import cn.iocoder.yudao.adminserver.modules.system.enums.common.SysSexEnum; +import cn.iocoder.yudao.adminserver.modules.system.service.auth.impl.SysUserSessionServiceImpl; +import cn.iocoder.yudao.adminserver.modules.system.service.logger.SysLoginLogService; +import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.auth.SysUserSessionDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.redis.auth.SysLoginUserCoreRedisDAO; +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; +import cn.iocoder.yudao.framework.test.core.util.AssertUtils; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; -import java.time.Duration; +import javax.annotation.Resource; +import java.util.Arrays; +import java.util.Collections; import java.util.Date; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; -import javax.annotation.Resource; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.context.annotation.Import; - -import cn.hutool.core.date.DateUtil; -import cn.iocoder.yudao.adminserver.BaseDbAndRedisUnitTest; -import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; -import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.security.config.SecurityProperties; -import cn.iocoder.yudao.framework.security.core.LoginUser; -import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.session.SysUserSessionPageReqVO; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.auth.SysUserSessionDO; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO; -import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.auth.SysUserSessionMapper; -import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.user.SysUserMapper; -import cn.iocoder.yudao.adminserver.modules.system.dal.redis.auth.SysLoginUserRedisDAO; -import cn.iocoder.yudao.adminserver.modules.system.enums.common.SysSexEnum; -import cn.iocoder.yudao.adminserver.modules.system.service.auth.impl.SysUserSessionServiceImpl; -import cn.iocoder.yudao.adminserver.modules.system.service.dept.impl.SysDeptServiceImpl; -import cn.iocoder.yudao.adminserver.modules.system.service.logger.impl.SysLoginLogServiceImpl; -import cn.iocoder.yudao.adminserver.modules.system.service.user.impl.SysUserServiceImpl; -import cn.iocoder.yudao.framework.test.core.util.AssertUtils; -import cn.iocoder.yudao.framework.test.core.util.RandomUtils; -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; +import static cn.hutool.core.util.RandomUtil.randomEle; +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; +import static java.util.Collections.singletonList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; /** - * SysUserSessionServiceImpl Tester. + * {@link SysUserSessionServiceImpl} 的单元测试 * * @author Lyon - * @version 1.0 - * @since
3月 8, 2021
*/ -@Import({SysUserSessionServiceImpl.class, SysLoginUserRedisDAO.class}) +@Import({SysUserSessionServiceImpl.class}) public class SysUserSessionServiceImplTest extends BaseDbAndRedisUnitTest { @Resource - private SysUserSessionServiceImpl sysUserSessionService; + private SysUserSessionServiceImpl userSessionService; + @Resource - private SysUserSessionMapper sysUserSessionMapper; - @Resource - private SysLoginUserRedisDAO sysLoginUserRedisDAO; - @Resource - private SysUserMapper sysUserMapper; + private SysUserSessionMapper userSessionMapper; @MockBean - private SecurityProperties securityProperties; + private SysUserService userService; @MockBean - private SysDeptServiceImpl sysDeptService; + private SysLoginLogService loginLogService; @MockBean - private SysUserServiceImpl sysUserService; - @MockBean - private SysLoginLogServiceImpl sysLoginLogService; - - @Test - public void testCreateUserSession_success() { - // 准备参数 - String userIp = randomString(); - String userAgent = randomString(); - LoginUser loginUser = randomPojo(LoginUser.class); - // mock - when(securityProperties.getSessionTimeout()).thenReturn(Duration.ofDays(1)); - // 调用 - String sessionId = sysUserSessionService.createUserSession(loginUser, userIp, userAgent); - // 校验记录的属性是否正确 - SysUserSessionDO sysUserSessionDO = sysUserSessionMapper.selectById(sessionId); - assertEquals(sysUserSessionDO.getId(), sessionId); - assertEquals(sysUserSessionDO.getUserId(), loginUser.getId()); - assertEquals(sysUserSessionDO.getUserIp(), userIp); - assertEquals(sysUserSessionDO.getUserAgent(), userAgent); - assertEquals(sysUserSessionDO.getUsername(), loginUser.getUsername()); - LoginUser redisLoginUser = sysLoginUserRedisDAO.get(sessionId); - AssertUtils.assertPojoEquals(redisLoginUser, loginUser, "username","password"); - } - - @Test - public void testCreateRefreshUserSession_success() { - // 准备参数 - String sessionId = randomString(); - String userIp = randomString(); - String userAgent = randomString(); - Long timeLong = randomLongId(); - String userName = randomString(); - Date date = randomDate(); - LoginUser loginUser = randomPojo(LoginUser.class); - // mock - when(securityProperties.getSessionTimeout()).thenReturn(Duration.ofDays(1)); - loginUser.setUpdateTime(date); - sysLoginUserRedisDAO.set(sessionId, loginUser); - SysUserSessionDO userSession = SysUserSessionDO.builder().id(sessionId) - .userId(loginUser.getId()).userIp(userIp).userAgent(userAgent).username(userName) - .sessionTimeout(addTime(Duration.ofMillis(timeLong))) - .build(); - sysUserSessionMapper.insert(userSession); - SysUserSessionDO insertDO = sysUserSessionMapper.selectById(sessionId); - // 调用 - sysUserSessionService.refreshUserSession(sessionId, loginUser); - // 校验记录 redis - LoginUser redisLoginUser = sysLoginUserRedisDAO.get(sessionId); - assertNotEquals(redisLoginUser.getUpdateTime(), date); - // 校验记录 SysUserSessionDO - SysUserSessionDO updateDO = sysUserSessionMapper.selectById(sessionId); - assertEquals(updateDO.getUsername(), loginUser.getUsername()); - assertNotEquals(updateDO.getUpdateTime(), insertDO.getUpdateTime()); - assertNotEquals(updateDO.getSessionTimeout(), addTime(Duration.ofMillis(timeLong))); - } - - @Test - public void testDeleteUserSession_success() { - // 准备参数 - String sessionId = randomString(); - String userIp = randomString(); - String userAgent = randomString(); - Long timeLong = randomLongId(); - LoginUser loginUser = randomPojo(LoginUser.class); - // mock 存入 Redis - when(securityProperties.getSessionTimeout()).thenReturn(Duration.ofDays(1)); - sysLoginUserRedisDAO.set(sessionId, loginUser); - // mock 存入 db - SysUserSessionDO userSession = SysUserSessionDO.builder().id(sessionId) - .userId(loginUser.getId()).userIp(userIp).userAgent(userAgent).username(loginUser.getUsername()) - .sessionTimeout(addTime(Duration.ofMillis(timeLong))) - .build(); - sysUserSessionMapper.insert(userSession); - // 校验数据存在 - assertNotNull(sysLoginUserRedisDAO.get(sessionId)); - assertNotNull(sysUserSessionMapper.selectById(sessionId)); - // 调用 - sysUserSessionService.deleteUserSession(sessionId); - // 校验数据不存在了 - assertNull(sysLoginUserRedisDAO.get(sessionId)); - assertNull(sysUserSessionMapper.selectById(sessionId)); - } + private SysLoginUserCoreRedisDAO loginUserCoreRedisDAO; @Test public void testGetUserSessionPage_success() { // mock 数据 + SysUserDO dbUser = randomPojo(SysUserDO.class, o -> { + o.setSex(randomEle(SysSexEnum.values()).getSex()); + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); + }); + when(userService.getUsersByUsername(eq(dbUser.getUsername()))).thenReturn(singletonList(dbUser)); + // 插入可被查询到的数据 String userIp = randomString(); - SysUserDO dbUser1 = randomPojo(SysUserDO.class, o -> { - o.setUsername("testUsername1"); - o.setSex(randomEle(SysSexEnum.values()).getSex()); - o.setStatus(CommonStatusEnum.ENABLE.getStatus()); - }); - SysUserDO dbUser2 = randomPojo(SysUserDO.class, o -> { - o.setUsername("testUsername2"); - o.setSex(randomEle(SysSexEnum.values()).getSex()); - o.setStatus(CommonStatusEnum.ENABLE.getStatus()); - }); SysUserSessionDO dbSession = randomPojo(SysUserSessionDO.class, o -> { - o.setUserId(dbUser1.getId()); + o.setUserId(dbUser.getId()); + o.setUserType(randomEle(UserTypeEnum.values()).getValue()); o.setUserIp(userIp); }); - sysUserMapper.insert(dbUser1); - sysUserMapper.insert(dbUser2); - sysUserSessionMapper.insert(dbSession); - sysUserSessionMapper.insert(ObjectUtils.clone(dbSession, o -> { + userSessionMapper.insert(dbSession); + // 测试 username 不匹配 + userSessionMapper.insert(ObjectUtils.clone(dbSession, o -> { o.setId(randomString()); - o.setUserId(dbUser2.getId()); - })); - // 测试 userId 不匹配 - sysUserSessionMapper.insert(ObjectUtils.clone(dbSession, o -> { - o.setId(randomString()); - o.setUserId(123456l); + o.setUserId(123456L); })); // 测试 userIp 不匹配 - sysUserSessionMapper.insert(ObjectUtils.clone(dbSession, o -> { + userSessionMapper.insert(ObjectUtils.clone(dbSession, o -> { o.setId(randomString()); o.setUserIp("testUserIp"); })); // 准备参数 - SysUserSessionPageReqVO reqVo = new SysUserSessionPageReqVO(); - reqVo.setUserIp(userIp); + SysUserSessionPageReqVO reqVO = new SysUserSessionPageReqVO(); + reqVO.setUsername(dbUser.getUsername()); + reqVO.setUserIp(userIp); + // 调用 - PageResult pageResult = sysUserSessionService.getUserSessionPage(reqVo); + PageResult pageResult = userSessionService.getUserSessionPage(reqVO); // 断言 - assertEquals(3, pageResult.getTotal()); - assertEquals(3, pageResult.getList().size()); + assertEquals(1, pageResult.getTotal()); + assertEquals(1, pageResult.getList().size()); assertPojoEquals(dbSession, pageResult.getList().get(0)); } + // TODO 芋艿:单测写的有问题 @Test - public void testClearSessionTimeout_success() throws Exception { + public void testClearSessionTimeout_success() { // 准备超时数据 120 条, 在线用户 1 条 int expectedTimeoutCount = 120, expectedTotal = 1; @@ -209,17 +108,23 @@ public class SysUserSessionServiceImplTest extends BaseDbAndRedisUnitTest { List prepareData = Stream .iterate(0, i -> i) .limit(expectedTimeoutCount) - .map(i -> RandomUtils.randomPojo(SysUserSessionDO.class, o -> o.setSessionTimeout(DateUtil.offsetSecond(new Date(), -1)))) + .map(i -> randomPojo(SysUserSessionDO.class, o -> { + o.setUserType(randomEle(UserTypeEnum.values()).getValue()); + o.setSessionTimeout(DateUtil.offsetSecond(new Date(), -1)); + })) .collect(Collectors.toList()); - SysUserSessionDO sessionDO = RandomUtils.randomPojo(SysUserSessionDO.class, o -> o.setSessionTimeout(DateUtil.offsetMinute(new Date(), 30))); + SysUserSessionDO sessionDO = randomPojo(SysUserSessionDO.class, o -> { + o.setUserType(randomEle(UserTypeEnum.values()).getValue()); + o.setSessionTimeout(DateUtil.offsetMinute(new Date(), 30)); + }); prepareData.add(sessionDO); - prepareData.forEach(sysUserSessionMapper::insert); + prepareData.forEach(userSessionMapper::insert); - //清空超时数据 - long actualTimeoutCount = sysUserSessionService.clearSessionTimeout(); - //校验 + // 清空超时数据 + long actualTimeoutCount = userSessionService.clearSessionTimeout(); + // 校验 assertEquals(expectedTimeoutCount, actualTimeoutCount); - List userSessionDOS = sysUserSessionMapper.selectList(); + List userSessionDOS = userSessionMapper.selectList(); assertEquals(expectedTotal, userSessionDOS.size()); AssertUtils.assertPojoEquals(sessionDO, userSessionDOS.get(0), "updateTime"); } diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysLoginLogServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysLoginLogServiceImplTest.java index d087037bb..b9d4a55bb 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysLoginLogServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysLoginLogServiceImplTest.java @@ -3,6 +3,7 @@ package cn.iocoder.yudao.adminserver.modules.system.service.logger; import cn.hutool.core.util.RandomUtil; import cn.iocoder.yudao.adminserver.BaseDbUnitTest; import cn.iocoder.yudao.adminserver.modules.system.service.logger.dto.SysLoginLogCreateReqDTO; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; import cn.iocoder.yudao.adminserver.modules.system.controller.logger.vo.loginlog.SysLoginLogExportReqVO; @@ -62,6 +63,7 @@ public class SysLoginLogServiceImplTest extends BaseDbUnitTest { SysLoginLogDO loginLogDO = RandomUtils.randomPojo(SysLoginLogDO.class, logDO -> { logDO.setLogType(RandomUtil.randomEle(SysLoginLogTypeEnum.values()).getType()); logDO.setTraceId(TracerUtils.getTraceId()); + logDO.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()); logDO.setUserIp("192.168.199.16"); logDO.setUsername("wangkai"); @@ -100,13 +102,13 @@ public class SysLoginLogServiceImplTest extends BaseDbUnitTest { @Test public void testGetLoginLogList() { - // 构造测试数据 // 登录成功的 SysLoginLogDO loginLogDO = RandomUtils.randomPojo(SysLoginLogDO.class, logDO -> { logDO.setLogType(RandomUtil.randomEle(SysLoginLogTypeEnum.values()).getType()); logDO.setTraceId(TracerUtils.getTraceId()); + logDO.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()); logDO.setUserIp("192.168.111.16"); logDO.setUsername("wangxiaokai"); diff --git a/yudao-admin-server/src/test/resources/application-unit-test.yaml b/yudao-admin-server/src/test/resources/application-unit-test.yaml index 5b1461447..d306a7af4 100644 --- a/yudao-admin-server/src/test/resources/application-unit-test.yaml +++ b/yudao-admin-server/src/test/resources/application-unit-test.yaml @@ -36,34 +36,9 @@ mybatis: # Lock4j 配置项(单元测试,禁用 Lock4j) # Resilience4j 配置项 -resilience4j: - ratelimiter: - instances: - backendA: - limit-for-period: 1 # 每个周期内,允许的请求数。默认为 50 - limit-refresh-period: 60s # 每个周期的时长,单位:微秒。默认为 500 - timeout-duration: 1s # 被限流时,阻塞等待的时长,单位:微秒。默认为 5s - register-health-indicator: true # 是否注册到健康监测 --- #################### 监控相关配置 #################### --- #################### 芋道相关配置 #################### # 芋道配置项,设置当前项目所有自定义的配置 -yudao: - security: - token-header: Authorization - token-secret: abcdefghijklmnopqrstuvwxyz - token-timeout: 1d - session-timeout: 30m - mock-enable: true - mock-secret: test - swagger: - enable: false # 单元测试,禁用 Swagger - file: - base-path: http://127.0.0.1:${server.port}/${yudao.web.api-prefix}/file/get/ - xss: - enable: false - exclude-urls: # 如下两个 url,仅仅是为了演示,去掉配置也没关系 - - ${spring.boot.admin.context-path}/** # 不处理 Spring Boot Admin 的请求 - - ${management.endpoints.web.base-path}/** # 不处理 Actuator 的请求 diff --git a/yudao-admin-server/src/test/resources/sql/create_tables.sql b/yudao-admin-server/src/test/resources/sql/create_tables.sql index e575d184b..db67cad23 100644 --- a/yudao-admin-server/src/test/resources/sql/create_tables.sql +++ b/yudao-admin-server/src/test/resources/sql/create_tables.sql @@ -179,6 +179,7 @@ CREATE TABLE IF NOT EXISTS "sys_dict_type" ( CREATE TABLE IF NOT EXISTS `sys_user_session` ( `id` varchar(32) NOT NULL, `user_id` bigint DEFAULT NULL, + "user_type" tinyint NOT NULL, `username` varchar(50) NOT NULL DEFAULT '', `user_ip` varchar(50) DEFAULT NULL, `user_agent` varchar(512) DEFAULT NULL, @@ -223,6 +224,8 @@ CREATE TABLE IF NOT EXISTS "sys_notice" ( CREATE TABLE IF NOT EXISTS `sys_login_log` ( `id` bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY, `log_type` bigint(4) NOT NULL, + "user_id" bigint not null default '0', + "user_type" tinyint NOT NULL, `trace_id` varchar(64) NOT NULL DEFAULT '', `username` varchar(50) NOT NULL DEFAULT '', `result` tinyint(4) NOT NULL, diff --git a/yudao-core-service/pom.xml b/yudao-core-service/pom.xml new file mode 100644 index 000000000..46cfbaaa5 --- /dev/null +++ b/yudao-core-service/pom.xml @@ -0,0 +1,62 @@ + + + + yudao + cn.iocoder.boot + 1.1.0-snapshot + + 4.0.0 + + yudao-core-service + jar + + yudao-admin-server + + 公共服务,通过 jar 包的方式,被 yudao-admin-server、yudao-user-service 使用。例如说: + 1. 日志相关:访问日志、登录日志、异常日志等等 + 2. 认证相关:在线 Session + 3. 短信相关:短信模板、短信日志 + 等等 + + https://github.com/YunaiV/ruoyi-vue-pro + + + + + cn.iocoder.boot + yudao-spring-boot-starter-web + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + cn.iocoder.boot + yudao-spring-boot-starter-security + + + + + cn.iocoder.boot + yudao-spring-boot-starter-mybatis + + + + cn.iocoder.boot + yudao-spring-boot-starter-redis + + + + + cn.iocoder.boot + yudao-spring-boot-starter-test + test + + + + diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/infra/package-info.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/infra/package-info.java new file mode 100644 index 000000000..1f011d6a1 --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/infra/package-info.java @@ -0,0 +1,7 @@ +/** + * infra 包下,我们放基础设施的运维与管理,支撑上层的通用与核心业务。 + * 例如说:定时任务的管理、服务器的信息等等 + * + * 缩写:inf + */ +package cn.iocoder.yudao.coreservice.modules.infra; diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/member/package-info.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/member/package-info.java new file mode 100644 index 000000000..17abe90bd --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/member/package-info.java @@ -0,0 +1,7 @@ +/** + * member 包下,我们放会员业务. + * 例如说:会员中心等等 + * + * 缩写:mbr + */ +package cn.iocoder.yudao.coreservice.modules.member; diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/auth/SysUserSessionDO.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/dataobject/auth/SysUserSessionDO.java similarity index 89% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/auth/SysUserSessionDO.java rename to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/dataobject/auth/SysUserSessionDO.java index 03fc65881..67e4fc2d9 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/auth/SysUserSessionDO.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/dataobject/auth/SysUserSessionDO.java @@ -1,8 +1,8 @@ -package cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.auth; +package cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.auth; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.yudao.framework.security.core.LoginUser; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; @@ -35,7 +35,7 @@ public class SysUserSessionDO extends BaseDO { /** * 用户编号 * - * 关联 {@link SysUserDO#getId()} + * 关联 SysUserDO.id 或者 MbrUserDO.id */ private Long userId; /** diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/dataobject/package-info.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/dataobject/package-info.java new file mode 100644 index 000000000..f2ed88035 --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/dataobject/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.yudao.coreservice.modules.system.dal.dataobject; diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/auth/SysUserSessionCoreMapper.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/auth/SysUserSessionCoreMapper.java new file mode 100644 index 000000000..69b5c5acb --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/auth/SysUserSessionCoreMapper.java @@ -0,0 +1,10 @@ +package cn.iocoder.yudao.coreservice.modules.system.dal.mysql.auth; + +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.auth.SysUserSessionDO; +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface SysUserSessionCoreMapper extends BaseMapperX { + +} diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/package-info.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/package-info.java new file mode 100644 index 000000000..144a05407 --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.yudao.coreservice.modules.system.dal.mysql; diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java new file mode 100644 index 000000000..fb4118874 --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.coreservice.modules.system.dal.redis; + +import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine; +import cn.iocoder.yudao.framework.security.core.LoginUser; + +import static cn.iocoder.yudao.framework.redis.core.RedisKeyDefine.KeyTypeEnum.STRING; + +/** + * System Redis Key 枚举类 + * + * @author 芋道源码 + */ +public interface SysRedisKeyCoreConstants { + + RedisKeyDefine LOGIN_USER = new RedisKeyDefine("登录用户的缓存", + "login_user:%s", // 参数为 sessionId + STRING, LoginUser.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/auth/SysLoginUserRedisDAO.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/auth/SysLoginUserCoreRedisDAO.java similarity index 69% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/auth/SysLoginUserRedisDAO.java rename to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/auth/SysLoginUserCoreRedisDAO.java index 6ca358a4a..921492c9d 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/auth/SysLoginUserRedisDAO.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/auth/SysLoginUserCoreRedisDAO.java @@ -1,15 +1,14 @@ -package cn.iocoder.yudao.adminserver.modules.system.dal.redis.auth; +package cn.iocoder.yudao.coreservice.modules.system.dal.redis.auth; -import cn.iocoder.yudao.framework.security.core.LoginUser; -import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysUserSessionService; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; +import cn.iocoder.yudao.framework.security.config.SecurityProperties; +import cn.iocoder.yudao.framework.security.core.LoginUser; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Repository; import javax.annotation.Resource; -import java.time.Duration; -import static cn.iocoder.yudao.adminserver.modules.system.dal.redis.SysRedisKeyConstants.LOGIN_USER; +import static cn.iocoder.yudao.coreservice.modules.system.dal.redis.SysRedisKeyCoreConstants.LOGIN_USER; /** * {@link LoginUser} 的 RedisDAO @@ -17,12 +16,13 @@ import static cn.iocoder.yudao.adminserver.modules.system.dal.redis.SysRedisKeyC * @author 芋道源码 */ @Repository -public class SysLoginUserRedisDAO { +public class SysLoginUserCoreRedisDAO { @Resource private StringRedisTemplate stringRedisTemplate; + @Resource - private SysUserSessionService sysUserSessionService; // TODO 芋艿:得看看怎么拿出去 + private SecurityProperties securityProperties; public LoginUser get(String sessionId) { String redisKey = formatKey(sessionId); @@ -32,7 +32,7 @@ public class SysLoginUserRedisDAO { public void set(String sessionId, LoginUser loginUser) { String redisKey = formatKey(sessionId); stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(loginUser), - Duration.ofMillis(sysUserSessionService.getSessionTimeoutMillis())); + securityProperties.getSessionTimeout()); } public void delete(String sessionId) { diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/package-info.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/package-info.java new file mode 100644 index 000000000..3b8777935 --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/package-info.java @@ -0,0 +1,7 @@ +/** + * system 包下,我们放通用业务,支撑上层的核心业务。 + * 例如说:用户、部门、权限、数据字典等等 + * + * 缩写:sys + */ +package cn.iocoder.yudao.coreservice.modules.system; diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysUserSessionService.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/SysUserSessionCoreService.java similarity index 77% rename from yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysUserSessionService.java rename to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/SysUserSessionCoreService.java index d2f8ffae5..5106af7a3 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysUserSessionService.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/SysUserSessionCoreService.java @@ -1,13 +1,13 @@ -package cn.iocoder.yudao.userserver.modules.system.service.auth; +package cn.iocoder.yudao.coreservice.modules.system.service.auth; import cn.iocoder.yudao.framework.security.core.LoginUser; /** - * 在线用户 Session Service 接口 + * 在线用户 Session Core Service 接口 * * @author 芋道源码 */ -public interface SysUserSessionService { +public interface SysUserSessionCoreService { /** * 创建在线用户 Session @@ -42,13 +42,6 @@ public interface SysUserSessionService { */ LoginUser getLoginUser(String sessionId); - /** - * 获取当前登录用户信息 - * @param username 用户名称 - * @return 在线用户 - */ - String getSessionId(String username); - /** * 获得 Session 超时时间,单位:毫秒 * diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/impl/SysUserSessionCoreServiceImpl.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/impl/SysUserSessionCoreServiceImpl.java new file mode 100644 index 000000000..f26c6afc3 --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/impl/SysUserSessionCoreServiceImpl.java @@ -0,0 +1,93 @@ +package cn.iocoder.yudao.coreservice.modules.system.service.auth.impl; + +import cn.hutool.core.util.IdUtil; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.auth.SysUserSessionDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.mysql.auth.SysUserSessionCoreMapper; +import cn.iocoder.yudao.coreservice.modules.system.dal.redis.auth.SysLoginUserCoreRedisDAO; +import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService; +import cn.iocoder.yudao.framework.security.config.SecurityProperties; +import cn.iocoder.yudao.framework.security.core.LoginUser; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.time.Duration; +import java.util.Date; + +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.addTime; + +/** + * 在线用户 Session Core Service 实现类 + * + * @author 芋道源码 + */ +@Service +public class SysUserSessionCoreServiceImpl implements SysUserSessionCoreService { + + @Resource + private SysUserSessionCoreMapper userSessionCoreMapper; + + @Resource + private SysLoginUserCoreRedisDAO loginUserCoreRedisDAO; + + @Resource + private SecurityProperties securityProperties; + + @Override + public String createUserSession(LoginUser loginUser, String userIp, String userAgent) { + // 生成 Session 编号 + String sessionId = generateSessionId(); + // 写入 Redis 缓存 + loginUser.setUpdateTime(new Date()); + loginUserCoreRedisDAO.set(sessionId, loginUser); + // 写入 DB 中 + SysUserSessionDO userSession = SysUserSessionDO.builder().id(sessionId) + .userId(loginUser.getId()).userType(loginUser.getUserType()) + .userIp(userIp).userAgent(userAgent).username(loginUser.getUsername()) + .sessionTimeout(addTime(Duration.ofMillis(getSessionTimeoutMillis()))) + .build(); + userSessionCoreMapper.insert(userSession); + // 返回 Session 编号 + return sessionId; + } + + @Override + public void refreshUserSession(String sessionId, LoginUser loginUser) { + // 写入 Redis 缓存 + loginUser.setUpdateTime(new Date()); + loginUserCoreRedisDAO.set(sessionId, loginUser); + // 更新 DB 中 + SysUserSessionDO updateObj = SysUserSessionDO.builder().id(sessionId).build(); + updateObj.setUsername(loginUser.getUsername()); + updateObj.setUpdateTime(new Date()); + updateObj.setSessionTimeout(addTime(Duration.ofMillis(getSessionTimeoutMillis()))); + userSessionCoreMapper.updateById(updateObj); + } + + @Override + public void deleteUserSession(String sessionId) { + // 删除 Redis 缓存 + loginUserCoreRedisDAO.delete(sessionId); + // 删除 DB 记录 + userSessionCoreMapper.deleteById(sessionId); + } + + @Override + public LoginUser getLoginUser(String sessionId) { + return loginUserCoreRedisDAO.get(sessionId); + } + + @Override + public Long getSessionTimeoutMillis() { + return securityProperties.getSessionTimeout().toMillis(); + } + + /** + * 生成 Session 编号,目前采用 UUID 算法 + * + * @return Session 编号 + */ + private static String generateSessionId() { + return IdUtil.fastSimpleUUID(); + } + +} diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/package-info.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/package-info.java new file mode 100644 index 000000000..8b5f8b010 --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.yudao.coreservice.modules.system.service; diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/tool/package-info.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/tool/package-info.java new file mode 100644 index 000000000..dd3c3e913 --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/tool/package-info.java @@ -0,0 +1,7 @@ +/** + * tool 包下,我们放研发工具,提升研发效率与质量。 + * 例如说:代码生成器、接口文档等等 + * + * 缩写:tool + */ +package cn.iocoder.yudao.coreservice.modules.tool; diff --git a/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseDbAndRedisUnitTest.java b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseDbAndRedisUnitTest.java new file mode 100644 index 000000000..8b9177cf5 --- /dev/null +++ b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseDbAndRedisUnitTest.java @@ -0,0 +1,48 @@ +package cn.iocoder.yudao.coreservice; + +import cn.iocoder.yudao.coreservice.config.RedisTestConfiguration; +import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; +import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; +import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; +import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; +import org.redisson.spring.starter.RedissonAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.jdbc.Sql; + +/** + * 依赖内存 DB + Redis 的单元测试 + * + * 相比 {@link BaseDbUnitTest} 来说,额外增加了内存 Redis + * + * @author 芋道源码 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbAndRedisUnitTest.Application.class) +@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 +@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB +public class BaseDbAndRedisUnitTest { + + @Import({ + // DB 配置类 + YudaoDataSourceAutoConfiguration.class, // 自己的 DB 配置类 + DataSourceAutoConfiguration.class, // Spring DB 自动配置类 + DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 + DruidDataSourceAutoConfigure.class, // Druid 自动配置类 + // MyBatis 配置类 + YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 + MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 + // Redis 配置类 + RedisTestConfiguration.class, // Redis 测试配置类,用于启动 RedisServer + RedisAutoConfiguration.class, // Spring Redis 自动配置类 + YudaoRedisAutoConfiguration.class, // 自己的 Redis 配置类 + RedissonAutoConfiguration.class, // Redisson 自动高配置类 + }) + public static class Application { + } + +} diff --git a/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseDbUnitTest.java b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseDbUnitTest.java new file mode 100644 index 000000000..94d6d3107 --- /dev/null +++ b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseDbUnitTest.java @@ -0,0 +1,39 @@ +package cn.iocoder.yudao.coreservice; + +import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; +import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; +import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.jdbc.Sql; + +/** + * 依赖内存 DB 的单元测试 + * + * 注意,Service 层同样适用。对于 Service 层的单元测试,我们针对自己模块的 Mapper 走的是 H2 内存数据库,针对别的模块的 Service 走的是 Mock 方法 + * + * @author 芋道源码 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbUnitTest.Application.class) +@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 +@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB +public class BaseDbUnitTest { + + @Import({ + // DB 配置类 + YudaoDataSourceAutoConfiguration.class, // 自己的 DB 配置类 + DataSourceAutoConfiguration.class, // Spring DB 自动配置类 + DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 + DruidDataSourceAutoConfigure.class, // Druid 自动配置类 + // MyBatis 配置类 + YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 + MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 + }) + public static class Application { + } + +} diff --git a/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseRedisUnitTest.java b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseRedisUnitTest.java new file mode 100644 index 000000000..e95e6d787 --- /dev/null +++ b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/BaseRedisUnitTest.java @@ -0,0 +1,32 @@ +package cn.iocoder.yudao.coreservice; + +import cn.iocoder.yudao.coreservice.config.RedisTestConfiguration; +import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; +import org.redisson.spring.starter.RedissonAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; + +/** + * 依赖内存 Redis 的单元测试 + * + * 相比 {@link BaseDbUnitTest} 来说,从内存 DB 改成了内存 Redis + * + * @author 芋道源码 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseRedisUnitTest.Application.class) +@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 +public class BaseRedisUnitTest { + + @Import({ + // Redis 配置类 + RedisTestConfiguration.class, // Redis 测试配置类,用于启动 RedisServer + RedisAutoConfiguration.class, // Spring Redis 自动配置类 + YudaoRedisAutoConfiguration.class, // 自己的 Redis 配置类 + RedissonAutoConfiguration.class, // Redisson 自动高配置类 + }) + public static class Application { + } + +} diff --git a/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/config/RedisTestConfiguration.java b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/config/RedisTestConfiguration.java new file mode 100644 index 000000000..eb0791974 --- /dev/null +++ b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/config/RedisTestConfiguration.java @@ -0,0 +1,30 @@ +package cn.iocoder.yudao.coreservice.config; + +import com.github.fppt.jedismock.RedisServer; +import org.springframework.boot.autoconfigure.data.redis.RedisProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; + +import java.io.IOException; + +@Configuration(proxyBeanMethods = false) +@Lazy(false) // 禁止延迟加载 +@EnableConfigurationProperties(RedisProperties.class) +public class RedisTestConfiguration { + + /** + * 创建模拟的 Redis Server 服务器 + */ + @Bean + public RedisServer redisServer(RedisProperties properties) throws IOException { + RedisServer redisServer = new RedisServer(properties.getPort()); + // TODO 芋艿:一次执行多个单元测试时,貌似创建多个 spring 容器,导致不进行 stop。这样,就导致端口被占用,无法启动。。。 + try { + redisServer.start(); + } catch (Exception ignore) {} + return redisServer; + } + +} diff --git a/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/SysUserSessionCoreServiceTest.java b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/SysUserSessionCoreServiceTest.java new file mode 100644 index 000000000..0dd0c1a0c --- /dev/null +++ b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/modules/system/service/auth/SysUserSessionCoreServiceTest.java @@ -0,0 +1,122 @@ +package cn.iocoder.yudao.coreservice.modules.system.service.auth; + +import cn.iocoder.yudao.coreservice.BaseDbAndRedisUnitTest; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.auth.SysUserSessionDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.mysql.auth.SysUserSessionCoreMapper; +import cn.iocoder.yudao.coreservice.modules.system.dal.redis.auth.SysLoginUserCoreRedisDAO; +import cn.iocoder.yudao.coreservice.modules.system.service.auth.impl.SysUserSessionCoreServiceImpl; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; +import cn.iocoder.yudao.framework.security.config.SecurityProperties; +import cn.iocoder.yudao.framework.security.core.LoginUser; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; + +import javax.annotation.Resource; +import java.time.Duration; +import java.util.Date; + +import static cn.hutool.core.util.RandomUtil.randomEle; +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.addTime; +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +@Import({SysUserSessionCoreServiceImpl.class, SysLoginUserCoreRedisDAO.class}) +public class SysUserSessionCoreServiceTest extends BaseDbAndRedisUnitTest { + + @Resource + private SysUserSessionCoreServiceImpl userSessionCoreService; + + @Resource + private SysUserSessionCoreMapper userSessionCoreMapper; + @Resource + private SysLoginUserCoreRedisDAO loginUserCoreRedisDAO; + + @MockBean + private SecurityProperties securityProperties; + + @Test + public void testCreateUserSession_success() { + // 准备参数 + String userIp = randomString(); + String userAgent = randomString(); + LoginUser loginUser = randomPojo(LoginUser.class, o -> o.setUserType(randomEle(UserTypeEnum.values()).getValue())); + // mock 方法 + when(securityProperties.getSessionTimeout()).thenReturn(Duration.ofDays(1)); + + // 调用 + String sessionId = userSessionCoreService.createUserSession(loginUser, userIp, userAgent); + // 校验 SysUserSessionDO 记录 + SysUserSessionDO userSessionDO = userSessionCoreMapper.selectById(sessionId); + assertPojoEquals(loginUser, userSessionDO, "id", "updateTime"); + assertEquals(sessionId, userSessionDO.getId()); + assertEquals(userIp, userSessionDO.getUserIp()); + assertEquals(userAgent, userSessionDO.getUserAgent()); + // 校验 LoginUser 缓存 + LoginUser redisLoginUser = loginUserCoreRedisDAO.get(sessionId); + assertPojoEquals(loginUser, redisLoginUser, "username", "password"); + } + + @Test + public void testCreateRefreshUserSession_success() { + // 准备参数 + String sessionId = randomString(); + String userIp = randomString(); + String userAgent = randomString(); + long timeLong = randomLongId(); + String userName = randomString(); + Date date = randomDate(); + LoginUser loginUser = randomPojo(LoginUser.class, o -> o.setUserType(randomEle(UserTypeEnum.values()).getValue())); + // mock 方法 + when(securityProperties.getSessionTimeout()).thenReturn(Duration.ofDays(1)); + // mock 数据 + loginUser.setUpdateTime(date); + loginUserCoreRedisDAO.set(sessionId, loginUser); + SysUserSessionDO userSession = SysUserSessionDO.builder().id(sessionId) + .userId(loginUser.getId()).userType(loginUser.getUserType()) + .userIp(userIp).userAgent(userAgent).username(userName) + .sessionTimeout(addTime(Duration.ofMillis(timeLong))) + .build(); + userSessionCoreMapper.insert(userSession); + + // 调用 + userSessionCoreService.refreshUserSession(sessionId, loginUser); + // 校验 LoginUser 缓存 + LoginUser redisLoginUser = loginUserCoreRedisDAO.get(sessionId); + assertNotEquals(redisLoginUser.getUpdateTime(), date); + // 校验 SysUserSessionDO 记录 + SysUserSessionDO updateDO = userSessionCoreMapper.selectById(sessionId); + assertEquals(updateDO.getUsername(), loginUser.getUsername()); + assertNotEquals(updateDO.getUpdateTime(), userSession.getUpdateTime()); + assertNotEquals(updateDO.getSessionTimeout(), addTime(Duration.ofMillis(timeLong))); + } + + @Test + public void testDeleteUserSession_success() { + // 准备参数 + String sessionId = randomString(); + String userIp = randomString(); + String userAgent = randomString(); + Long timeLong = randomLongId(); + LoginUser loginUser = randomPojo(LoginUser.class, o -> o.setUserType(randomEle(UserTypeEnum.values()).getValue())); + // mock 存入 Redis + when(securityProperties.getSessionTimeout()).thenReturn(Duration.ofDays(1)); + // mock 数据 + loginUserCoreRedisDAO.set(sessionId, loginUser); + SysUserSessionDO userSession = SysUserSessionDO.builder().id(sessionId) + .userId(loginUser.getId()).userType(loginUser.getUserType()) + .userIp(userIp).userAgent(userAgent).username(loginUser.getUsername()) + .sessionTimeout(addTime(Duration.ofMillis(timeLong))) + .build(); + userSessionCoreMapper.insert(userSession); + + // 调用 + userSessionCoreService.deleteUserSession(sessionId); + // 校验数据不存在了 + assertNull(loginUserCoreRedisDAO.get(sessionId)); + assertNull(userSessionCoreMapper.selectById(sessionId)); + } + +} diff --git a/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/modules/system/service/package-info.java b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/modules/system/service/package-info.java new file mode 100644 index 000000000..8b5f8b010 --- /dev/null +++ b/yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/modules/system/service/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.yudao.coreservice.modules.system.service; diff --git a/yudao-core-service/src/test/resources/application-unit-test.yaml b/yudao-core-service/src/test/resources/application-unit-test.yaml new file mode 100644 index 000000000..d306a7af4 --- /dev/null +++ b/yudao-core-service/src/test/resources/application-unit-test.yaml @@ -0,0 +1,44 @@ +spring: + main: + lazy-initialization: true # 开启懒加载,加快速度 + banner-mode: off # 单元测试,禁用 Banner + +--- #################### 数据库相关配置 #################### + +spring: + # 数据源配置项 + datasource: + name: ruoyi-vue-pro + url: jdbc:h2:mem:testdb;MODE=MYSQL;DATABASE_TO_UPPER=false; # MODE 使用 MySQL 模式;DATABASE_TO_UPPER 配置表和字段使用小写 + driver-class-name: org.h2.Driver + username: sa + password: + schema: classpath:sql/create_tables.sql # MySQL 转 H2 的语句,使用 https://www.jooq.org/translate/ 工具 + druid: + async-init: true # 单元测试,异步初始化 Druid 连接池,提升启动速度 + initial-size: 1 # 单元测试,配置为 1,提升启动速度 + + # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 + redis: + host: 127.0.0.1 # 地址 + port: 16379 # 端口(单元测试,使用 16379 端口) + database: 0 # 数据库索引 + +mybatis: + lazy-initialization: true # 单元测试,设置 MyBatis Mapper 延迟加载,加速每个单元测试 + +--- #################### 定时任务相关配置 #################### + +--- #################### 配置中心相关配置 #################### + +--- #################### 服务保障相关配置 #################### + +# Lock4j 配置项(单元测试,禁用 Lock4j) + +# Resilience4j 配置项 + +--- #################### 监控相关配置 #################### + +--- #################### 芋道相关配置 #################### + +# 芋道配置项,设置当前项目所有自定义的配置 diff --git a/yudao-core-service/src/test/resources/application.yaml b/yudao-core-service/src/test/resources/application.yaml new file mode 100644 index 000000000..4db8b273c --- /dev/null +++ b/yudao-core-service/src/test/resources/application.yaml @@ -0,0 +1,35 @@ +spring: + application: + name: yudao-core-service + + # Jackson 配置项 + jackson: + serialization: + write-dates-as-timestamps: true # 设置 Date 的格式,使用时间戳 + write-date-timestamps-as-nanoseconds: false # 设置不使用 nanoseconds 的格式。例如说 1611460870.401,而是直接 1611460870401 + write-durations-as-timestamps: true # 设置 Duration 的格式,使用时间戳 + fail-on-empty-beans: false # 允许序列化无属性的 Bean + +# MyBatis Plus 的配置项 +mybatis-plus: + configuration: + map-underscore-to-camel-case: true # 虽然默认为 true ,但是还是显示去指定下。 + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 打印日志 + global-config: + db-config: + id-type: AUTO # 自增 ID + logic-delete-value: 1 # 逻辑已删除值(默认为 1) + logic-not-delete-value: 0 # 逻辑未删除值(默认为 0) + mapper-locations: classpath*:mapper/*.xml + type-aliases-package: ${yudao.core-service.base-package}.modules.*.dal.dataobject + +--- #################### 芋道相关配置 #################### + +yudao: + info: + version: 1.0.0 + base-package: cn.iocoder.yudao.coreservice + core-service: + base-package: cn.iocoder.yudao.coreservice + +debug: false diff --git a/yudao-core-service/src/test/resources/logback-spring.xml b/yudao-core-service/src/test/resources/logback-spring.xml new file mode 100644 index 000000000..daf756bff --- /dev/null +++ b/yudao-core-service/src/test/resources/logback-spring.xml @@ -0,0 +1,4 @@ + + + + diff --git a/yudao-core-service/src/test/resources/sql/clean.sql b/yudao-core-service/src/test/resources/sql/clean.sql new file mode 100644 index 000000000..316e89074 --- /dev/null +++ b/yudao-core-service/src/test/resources/sql/clean.sql @@ -0,0 +1,4 @@ +-- inf 开头的 DB + +-- sys 开头的 DB +DELETE FROM "sys_user_session"; diff --git a/yudao-core-service/src/test/resources/sql/create_tables.sql b/yudao-core-service/src/test/resources/sql/create_tables.sql new file mode 100644 index 000000000..b0a8c271a --- /dev/null +++ b/yudao-core-service/src/test/resources/sql/create_tables.sql @@ -0,0 +1,20 @@ +-- inf 开头的 DB + +-- sys 开头的 DB + +CREATE TABLE IF NOT EXISTS `sys_user_session` ( + `id` varchar(32) NOT NULL, + `user_id` bigint DEFAULT NULL, + "user_type" tinyint NOT NULL, + `username` varchar(50) NOT NULL DEFAULT '', + `user_ip` varchar(50) DEFAULT NULL, + `user_agent` varchar(512) DEFAULT NULL, + `session_timeout` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "creator" varchar(64) DEFAULT '', + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updater` varchar(64) DEFAULT '' , + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "deleted" bit NOT NULL DEFAULT FALSE, + PRIMARY KEY (`id`) +) COMMENT '用户在线 Session'; + diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index acb5dd270..a177593ea 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -72,6 +72,11 @@
+ + cn.iocoder.boot + yudao-core-service + ${revision} + cn.iocoder.boot yudao-spring-boot-starter-biz-operatelog diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/YudaoMybatisAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/YudaoMybatisAutoConfiguration.java index f8eeeef87..b03fc58e4 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/YudaoMybatisAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/YudaoMybatisAutoConfiguration.java @@ -15,7 +15,7 @@ import org.springframework.context.annotation.Configuration; * @author 芋道源码 */ @Configuration -@MapperScan(value = "${yudao.info.base-package}", annotationClass = Mapper.class, +@MapperScan(value = {"${yudao.info.base-package}", "${yudao.core-service.base-package}"}, annotationClass = Mapper.class, lazyInitialization = "${mybatis.lazy-initialization:false}") // Mapper 懒加载,目前仅用于单元测试 public class YudaoMybatisAutoConfiguration { diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/LoginUser.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/LoginUser.java index 2f9d647c6..4e49f66bc 100644 --- a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/LoginUser.java +++ b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/LoginUser.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.framework.security.core; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import org.springframework.security.core.GrantedAuthority; @@ -23,6 +24,12 @@ public class LoginUser implements UserDetails { * 用户编号 */ private Long id; + /** + * 用户类型 + * + * 关联 {@link UserTypeEnum} + */ + private Integer userType; /** * 部门编号 */ diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/package-info.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/package-info.java deleted file mode 100644 index 430f6c945..000000000 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package cn.iocoder.yudao.userserver.modules; diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysUserSessionServiceImpl.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysUserSessionServiceImpl.java deleted file mode 100644 index d7e686c0a..000000000 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysUserSessionServiceImpl.java +++ /dev/null @@ -1,47 +0,0 @@ -package cn.iocoder.yudao.userserver.modules.system.service.auth.impl; - -import cn.iocoder.yudao.framework.security.core.LoginUser; -import cn.iocoder.yudao.userserver.modules.system.service.auth.SysUserSessionService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; - -/** - * 在线用户 Session Service 实现类 - * - * @author 芋道源码 - */ -@Service -@Slf4j -public class SysUserSessionServiceImpl implements SysUserSessionService { - - @Override - public String createUserSession(LoginUser loginUser, String userIp, String userAgent) { - return null; - } - - @Override - public void refreshUserSession(String sessionId, LoginUser loginUser) { - - } - - @Override - public void deleteUserSession(String sessionId) { - - } - - @Override - public LoginUser getLoginUser(String sessionId) { - return null; - } - - @Override - public String getSessionId(String username) { - return null; - } - - @Override - public Long getSessionTimeoutMillis() { - return null; - } - -}