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

This commit is contained in:
liqiang-fit2cloud 2023-04-28 21:44:32 +08:00
parent 45c733c30e
commit a1afb45929
2 changed files with 3 additions and 13 deletions

View File

@ -1,20 +1,14 @@
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();
@ -22,9 +16,5 @@ public class SessionConfig {
return sessionIdResolver;
}
@Bean
public RedisSessionRepository redisSessionRepository() {
return new RedisSessionRepository(redisTemplate);
}
}

View File

@ -20,9 +20,9 @@ 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.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.session.data.redis.RedisSessionRepository;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.WebSession;
@ -49,14 +49,14 @@ public class LoginController {
@Resource
private SystemParameterService systemParameterService;
@Resource
private RedisSessionRepository redisSessionRepository;
private RedisTemplate<String, Object> redisTemplate;
@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 {
if (StringUtils.isNotBlank(sessionId) && StringUtils.isNotBlank(csrfToken)) {
userLoginService.validateCsrfToken(sessionId, csrfToken);
Object userFromSession = redisSessionRepository.getSessionRedisOperations().opsForHash().get("spring:session:sessions:" + sessionId, "sessionAttr:user");
Object userFromSession = redisTemplate.opsForHash().get("spring:session:sessions:" + sessionId, "sessionAttr:user");
if (userFromSession != null) {
if (userFromSession instanceof User) {
// 用户只有工作空间权限