mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 15:21:53 +08:00
增加 yudao-core-service 模块,提供共享逻辑
This commit is contained in:
parent
e999cc31c6
commit
5b723d02b2
1
pom.xml
1
pom.xml
@ -12,6 +12,7 @@
|
||||
<module>yudao-framework</module>
|
||||
<module>yudao-admin-server</module>
|
||||
<module>yudao-user-server</module>
|
||||
<module>yudao-core-service</module>
|
||||
</modules>
|
||||
|
||||
<name>${artifactId}</name>
|
||||
|
@ -18,6 +18,11 @@
|
||||
|
||||
<dependencies>
|
||||
<!-- 业务组件 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-core-service</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-biz-operatelog</artifactId>
|
||||
|
@ -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) {
|
||||
|
@ -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<Boolean> deleteUserSession(@RequestParam("id") String id) {
|
||||
userSessionService.deleteUserSession(id);
|
||||
userSessionCoreService.deleteUserSession(id);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
|
@ -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<SysRoleDO> roleList, List<SysMenuDO> menuList) {
|
||||
return SysAuthPermissionInfoRespVO.builder()
|
||||
@ -39,10 +45,6 @@ public interface SysAuthConvert {
|
||||
|
||||
SysAuthMenuRespVO convertTreeNode(SysMenuDO menu);
|
||||
|
||||
LoginUser convert(SysUserProfileUpdateReqVO reqVO);
|
||||
|
||||
LoginUser convert(SysUserProfileUpdatePasswordReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 将菜单列表,构建成菜单树
|
||||
*
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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<SysUserSessionDO> {
|
||||
default List<SysUserSessionDO> selectListBySessionTimoutLt() {
|
||||
return selectList(new QueryWrapperX<SysUserSessionDO>().lt("session_timeout",new Date()));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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();
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<SysUserSessionDO> getUserSessionPage(SysUserSessionPageReqVO reqVO) {
|
||||
@ -113,18 +60,20 @@ public class SysUserSessionServiceImpl implements SysUserSessionService {
|
||||
return userSessionMapper.selectPage(reqVO, userIds);
|
||||
}
|
||||
|
||||
// TODO @芋艿:优化下该方法
|
||||
@Override
|
||||
public long clearSessionTimeout() {
|
||||
// 获取db里已经超时的用户列表
|
||||
List<SysUserSessionDO> sessionTimeoutDOS = userSessionMapper.selectListBySessionTimoutLt();
|
||||
Map<String, SysUserSessionDO> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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}
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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()))
|
||||
|
@ -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 <pre>3月 8, 2021</pre>
|
||||
*/
|
||||
@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<SysUserSessionDO> pageResult = sysUserSessionService.getUserSessionPage(reqVo);
|
||||
PageResult<SysUserSessionDO> 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<SysUserSessionDO> 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<SysUserSessionDO> userSessionDOS = sysUserSessionMapper.selectList();
|
||||
List<SysUserSessionDO> userSessionDOS = userSessionMapper.selectList();
|
||||
assertEquals(expectedTotal, userSessionDOS.size());
|
||||
AssertUtils.assertPojoEquals(sessionDO, userSessionDOS.get(0), "updateTime");
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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 的请求
|
||||
|
@ -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,
|
||||
|
62
yudao-core-service/pom.xml
Normal file
62
yudao-core-service/pom.xml
Normal file
@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>yudao</artifactId>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<version>1.1.0-snapshot</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>yudao-core-service</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>yudao-admin-server</name>
|
||||
<description>
|
||||
公共服务,通过 jar 包的方式,被 yudao-admin-server、yudao-user-service 使用。例如说:
|
||||
1. 日志相关:访问日志、登录日志、异常日志等等
|
||||
2. 认证相关:在线 Session
|
||||
3. 短信相关:短信模板、短信日志
|
||||
等等
|
||||
</description>
|
||||
<url>https://github.com/YunaiV/ruoyi-vue-pro</url>
|
||||
|
||||
<dependencies>
|
||||
<!-- Web 相关 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- spring boot 配置所需依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- DB 相关 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-mybatis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Test 测试相关 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,7 @@
|
||||
/**
|
||||
* infra 包下,我们放基础设施的运维与管理,支撑上层的通用与核心业务。
|
||||
* 例如说:定时任务的管理、服务器的信息等等
|
||||
*
|
||||
* 缩写:inf
|
||||
*/
|
||||
package cn.iocoder.yudao.coreservice.modules.infra;
|
@ -0,0 +1,7 @@
|
||||
/**
|
||||
* member 包下,我们放会员业务.
|
||||
* 例如说:会员中心等等
|
||||
*
|
||||
* 缩写:mbr
|
||||
*/
|
||||
package cn.iocoder.yudao.coreservice.modules.member;
|
@ -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;
|
||||
/**
|
@ -0,0 +1 @@
|
||||
package cn.iocoder.yudao.coreservice.modules.system.dal.dataobject;
|
@ -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<SysUserSessionDO> {
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
package cn.iocoder.yudao.coreservice.modules.system.dal.mysql;
|
@ -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);
|
||||
|
||||
}
|
@ -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) {
|
@ -0,0 +1,7 @@
|
||||
/**
|
||||
* system 包下,我们放通用业务,支撑上层的核心业务。
|
||||
* 例如说:用户、部门、权限、数据字典等等
|
||||
*
|
||||
* 缩写:sys
|
||||
*/
|
||||
package cn.iocoder.yudao.coreservice.modules.system;
|
@ -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 超时时间,单位:毫秒
|
||||
*
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
package cn.iocoder.yudao.coreservice.modules.system.service;
|
@ -0,0 +1,7 @@
|
||||
/**
|
||||
* tool 包下,我们放研发工具,提升研发效率与质量。
|
||||
* 例如说:代码生成器、接口文档等等
|
||||
*
|
||||
* 缩写:tool
|
||||
*/
|
||||
package cn.iocoder.yudao.coreservice.modules.tool;
|
@ -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 {
|
||||
}
|
||||
|
||||
}
|
@ -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 {
|
||||
}
|
||||
|
||||
}
|
@ -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 {
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
package cn.iocoder.yudao.coreservice.modules.system.service;
|
@ -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 配置项
|
||||
|
||||
--- #################### 监控相关配置 ####################
|
||||
|
||||
--- #################### 芋道相关配置 ####################
|
||||
|
||||
# 芋道配置项,设置当前项目所有自定义的配置
|
35
yudao-core-service/src/test/resources/application.yaml
Normal file
35
yudao-core-service/src/test/resources/application.yaml
Normal file
@ -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
|
4
yudao-core-service/src/test/resources/logback-spring.xml
Normal file
4
yudao-core-service/src/test/resources/logback-spring.xml
Normal file
@ -0,0 +1,4 @@
|
||||
<configuration>
|
||||
<!-- 引用 Spring Boot 的 logback 基础配置 -->
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
|
||||
</configuration>
|
4
yudao-core-service/src/test/resources/sql/clean.sql
Normal file
4
yudao-core-service/src/test/resources/sql/clean.sql
Normal file
@ -0,0 +1,4 @@
|
||||
-- inf 开头的 DB
|
||||
|
||||
-- sys 开头的 DB
|
||||
DELETE FROM "sys_user_session";
|
20
yudao-core-service/src/test/resources/sql/create_tables.sql
Normal file
20
yudao-core-service/src/test/resources/sql/create_tables.sql
Normal file
@ -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';
|
||||
|
@ -72,6 +72,11 @@
|
||||
</dependency>
|
||||
|
||||
<!-- 业务组件 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-core-service</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-biz-operatelog</artifactId>
|
||||
|
@ -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 {
|
||||
|
||||
|
@ -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;
|
||||
/**
|
||||
* 部门编号
|
||||
*/
|
||||
|
@ -1 +0,0 @@
|
||||
package cn.iocoder.yudao.userserver.modules;
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user