mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-12-05 05:29:29 +08:00
Revert "refactor(gateway): 重写is-login方法,不用响应式,防止阻塞redisson线程"
This reverts commit 166cde0b11
.
This commit is contained in:
parent
5871a9eac5
commit
785408f677
@ -1,30 +1,18 @@
|
||||
package io.metersphere.gateway.config;
|
||||
|
||||
import io.metersphere.commons.constants.SessionConstants;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.session.data.redis.RedisSessionRepository;
|
||||
import org.springframework.web.server.session.HeaderWebSessionIdResolver;
|
||||
import org.springframework.web.server.session.WebSessionIdResolver;
|
||||
|
||||
@Configuration
|
||||
public class SessionConfig {
|
||||
|
||||
@Resource
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Bean
|
||||
public WebSessionIdResolver webSessionIdResolver() {
|
||||
HeaderWebSessionIdResolver sessionIdResolver = new HeaderWebSessionIdResolver();
|
||||
sessionIdResolver.setHeaderName(SessionConstants.HEADER_TOKEN); // Define Session Header Name
|
||||
return sessionIdResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisSessionRepository redisSessionRepository() {
|
||||
return new RedisSessionRepository(redisTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,29 +5,31 @@ import io.metersphere.commons.constants.OperLogConstants;
|
||||
import io.metersphere.commons.constants.OperLogModule;
|
||||
import io.metersphere.commons.constants.SessionConstants;
|
||||
import io.metersphere.commons.user.SessionUser;
|
||||
import io.metersphere.commons.utils.RsaKey;
|
||||
import io.metersphere.commons.utils.RsaUtil;
|
||||
import io.metersphere.controller.handler.ResultHolder;
|
||||
import io.metersphere.dto.ServiceDTO;
|
||||
import io.metersphere.dto.UserDTO;
|
||||
import io.metersphere.gateway.log.annotation.MsAuditLog;
|
||||
import io.metersphere.gateway.service.AuthSourceService;
|
||||
import io.metersphere.gateway.service.BaseDisplayService;
|
||||
import io.metersphere.gateway.service.SystemParameterService;
|
||||
import io.metersphere.gateway.service.UserLoginService;
|
||||
import io.metersphere.gateway.log.annotation.MsAuditLog;
|
||||
import io.metersphere.request.LoginRequest;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.session.data.redis.RedisSessionRepository;
|
||||
import org.springframework.session.data.redis.ReactiveRedisSessionRepository;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.web.server.WebSession;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@ -48,26 +50,36 @@ public class LoginController {
|
||||
@Resource
|
||||
private SystemParameterService systemParameterService;
|
||||
@Resource
|
||||
private RedisSessionRepository redisSessionRepository;
|
||||
private ReactiveRedisSessionRepository reactiveRedisSessionRepository;
|
||||
|
||||
@GetMapping(value = "/is-login")
|
||||
public Mono<ResultHolder> isLogin(@RequestHeader(name = SessionConstants.HEADER_TOKEN, required = false) String sessionId,
|
||||
@RequestHeader(name = SessionConstants.CSRF_TOKEN, required = false) String csrfToken) throws Exception {
|
||||
RsaKey rsaKey = RsaUtil.getRsaKey();
|
||||
|
||||
if (StringUtils.isNotBlank(sessionId) && StringUtils.isNotBlank(csrfToken)) {
|
||||
userLoginService.validateCsrfToken(sessionId, csrfToken);
|
||||
Object userFromSession = redisSessionRepository.getSessionRedisOperations().opsForHash().get("spring:session:sessions:" + sessionId, "sessionAttr:user");
|
||||
if (userFromSession instanceof User) {
|
||||
// 用户只有工作空间权限
|
||||
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));
|
||||
}
|
||||
return reactiveRedisSessionRepository.getSessionRedisOperations().opsForHash().get("spring:session:sessions:" + sessionId, "sessionAttr:user")
|
||||
.switchIfEmpty(Mono.just(rsaKey))
|
||||
.map(r -> {
|
||||
if (r instanceof RsaKey) {
|
||||
return ResultHolder.error(rsaKey.getPublicKey());
|
||||
}
|
||||
if (r instanceof User) {
|
||||
// 用户只有工作空间权限
|
||||
if (StringUtils.isBlank(((User) r).getLastProjectId())) {
|
||||
((User) r).setLastProjectId("no_such_project");
|
||||
}
|
||||
// 使用数据库里的最新用户权限,不同的tab sessionId 不变
|
||||
UserDTO userDTO = userLoginService.getUserDTO(((User) r).getId());
|
||||
SessionUser sessionUser = SessionUser.fromUser(userDTO, sessionId);
|
||||
return ResultHolder.success(sessionUser);
|
||||
}
|
||||
return ResultHolder.success(r);
|
||||
});
|
||||
} else {
|
||||
return Mono.just(ResultHolder.error(rsaKey.getPublicKey()));
|
||||
}
|
||||
return Mono.just(ResultHolder.error(RsaUtil.getRsaKey().getPublicKey()));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/signin")
|
||||
|
Loading…
Reference in New Issue
Block a user