refactor(gateway): 重写is-login方法,不用响应式,防止阻塞redisson线程

This commit is contained in:
liqiang-fit2cloud 2023-04-28 20:49:44 +08:00
parent 166cde0b11
commit 45c733c30e
1 changed files with 15 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import io.metersphere.commons.constants.OperLogConstants;
import io.metersphere.commons.constants.OperLogModule; import io.metersphere.commons.constants.OperLogModule;
import io.metersphere.commons.constants.SessionConstants; import io.metersphere.commons.constants.SessionConstants;
import io.metersphere.commons.user.SessionUser; import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.RsaUtil; import io.metersphere.commons.utils.RsaUtil;
import io.metersphere.controller.handler.ResultHolder; import io.metersphere.controller.handler.ResultHolder;
import io.metersphere.dto.ServiceDTO; import io.metersphere.dto.ServiceDTO;
@ -56,15 +57,21 @@ public class LoginController {
if (StringUtils.isNotBlank(sessionId) && StringUtils.isNotBlank(csrfToken)) { if (StringUtils.isNotBlank(sessionId) && StringUtils.isNotBlank(csrfToken)) {
userLoginService.validateCsrfToken(sessionId, csrfToken); userLoginService.validateCsrfToken(sessionId, csrfToken);
Object userFromSession = redisSessionRepository.getSessionRedisOperations().opsForHash().get("spring:session:sessions:" + sessionId, "sessionAttr:user"); Object userFromSession = redisSessionRepository.getSessionRedisOperations().opsForHash().get("spring:session:sessions:" + sessionId, "sessionAttr:user");
if (userFromSession instanceof User) { if (userFromSession != null) {
// 用户只有工作空间权限 if (userFromSession instanceof User) {
if (StringUtils.isBlank(((User) userFromSession).getLastProjectId())) { // 用户只有工作空间权限
((User) userFromSession).setLastProjectId("no_such_project"); if (StringUtils.isBlank(((User) userFromSession).getLastProjectId())) {
((User) userFromSession).setLastProjectId("no_such_project");
}
// 使用数据库里的最新用户权限不同的tab sessionId 不变
UserDTO userDTO = userLoginService.getUserDTO(((User) userFromSession).getId());
SessionUser sessionUser = SessionUser.fromUser(userDTO, sessionId);
return Mono.just(ResultHolder.success(sessionUser));
} }
// 使用数据库里的最新用户权限不同的tab sessionId 不变 LogUtil.info("userFromSession.class: " + userFromSession.getClass().getName());
UserDTO userDTO = userLoginService.getUserDTO(((User) userFromSession).getId()); return Mono.just(ResultHolder.success(userFromSession));
SessionUser sessionUser = SessionUser.fromUser(userDTO, sessionId); } else {
return Mono.just(ResultHolder.success(sessionUser)); LogUtil.info("userFromSession is null");
} }
} }
return Mono.just(ResultHolder.error(RsaUtil.getRsaKey().getPublicKey())); return Mono.just(ResultHolder.error(RsaUtil.getRsaKey().getPublicKey()));