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

This reverts commit a1afb45929.
This commit is contained in:
liqiang-fit2cloud 2023-04-28 22:20:00 +08:00
parent a1afb45929
commit d742e0d84f
2 changed files with 13 additions and 3 deletions

View File

@ -1,14 +1,20 @@
package io.metersphere.gateway.config; package io.metersphere.gateway.config;
import io.metersphere.commons.constants.SessionConstants; import io.metersphere.commons.constants.SessionConstants;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 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.HeaderWebSessionIdResolver;
import org.springframework.web.server.session.WebSessionIdResolver; import org.springframework.web.server.session.WebSessionIdResolver;
@Configuration @Configuration
public class SessionConfig { public class SessionConfig {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Bean @Bean
public WebSessionIdResolver webSessionIdResolver() { public WebSessionIdResolver webSessionIdResolver() {
HeaderWebSessionIdResolver sessionIdResolver = new HeaderWebSessionIdResolver(); HeaderWebSessionIdResolver sessionIdResolver = new HeaderWebSessionIdResolver();
@ -16,5 +22,9 @@ public class SessionConfig {
return sessionIdResolver; 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.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.session.data.redis.RedisSessionRepository;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.WebSession; import org.springframework.web.server.WebSession;
@ -49,14 +49,14 @@ public class LoginController {
@Resource @Resource
private SystemParameterService systemParameterService; private SystemParameterService systemParameterService;
@Resource @Resource
private RedisTemplate<String, Object> redisTemplate; private RedisSessionRepository redisSessionRepository;
@GetMapping(value = "/is-login") @GetMapping(value = "/is-login")
public Mono<ResultHolder> isLogin(@RequestHeader(name = SessionConstants.HEADER_TOKEN, required = false) String sessionId, public Mono<ResultHolder> isLogin(@RequestHeader(name = SessionConstants.HEADER_TOKEN, required = false) String sessionId,
@RequestHeader(name = SessionConstants.CSRF_TOKEN, required = false) String csrfToken) throws Exception { @RequestHeader(name = SessionConstants.CSRF_TOKEN, required = false) String csrfToken) throws Exception {
if (StringUtils.isNotBlank(sessionId) && StringUtils.isNotBlank(csrfToken)) { if (StringUtils.isNotBlank(sessionId) && StringUtils.isNotBlank(csrfToken)) {
userLoginService.validateCsrfToken(sessionId, csrfToken); userLoginService.validateCsrfToken(sessionId, csrfToken);
Object userFromSession = redisTemplate.opsForHash().get("spring:session:sessions:" + sessionId, "sessionAttr:user"); Object userFromSession = redisSessionRepository.getSessionRedisOperations().opsForHash().get("spring:session:sessions:" + sessionId, "sessionAttr:user");
if (userFromSession != null) { if (userFromSession != null) {
if (userFromSession instanceof User) { if (userFromSession instanceof User) {
// 用户只有工作空间权限 // 用户只有工作空间权限