feat: 使用双token逻辑

This commit is contained in:
2026-06-16 16:02:09 +08:00
parent fe1ffd73b2
commit 05ce76ad7a
10 changed files with 237 additions and 15 deletions

View File

@@ -2,15 +2,39 @@ package com.webgame.webgamebackend.common.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/**
* Redis 配置类:序列化方式 + 消息监听容器
*/
@Configuration
public class RedisConfigurer {
/**
* 配置 RedisTemplate使用 String 序列化器替代 Java 原生序列化
* <p>
* 不配置此项的话Spring Boot 默认使用 JdkSerializationRedisSerializer
* 存入 Redis 的数据会带有 Java 序列化魔数前缀(\xAC\xED\x00\x05
* 导致人类不可读且无法被其他语言客户端读取。
*/
@Bean
@Primary
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
StringRedisSerializer stringSerializer = new StringRedisSerializer();
template.setKeySerializer(stringSerializer);
template.setValueSerializer(stringSerializer);
template.setHashKeySerializer(stringSerializer);
template.setHashValueSerializer(stringSerializer);
template.afterPropertiesSet();
return template;
}
/**
* 创建并配置 Redis 消息监听容器
* 用于实现 Redis 的发布/订阅功能,支持监听特定频道的消息

View File

@@ -1,7 +1,7 @@
package com.webgame.webgamebackend.common.dto.account;
/**
* 登录/注册响应
* 登录/注册/刷新响应,包含 accessToken 和 refreshToken
*/
public record LoginResponse(String token) {
public record LoginResponse(String accessToken, String refreshToken) {
}

View File

@@ -0,0 +1,15 @@
package com.webgame.webgamebackend.common.dto.account;
import com.webgame.webgamebackend.common.dto.base.BaseVo;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
/**
* 刷新令牌请求
*/
public record RefreshTokenRequest(
@NotNull(message = "刷新令牌不能为null")
@NotBlank(message = "刷新令牌不能为空")
String refreshToken
) implements BaseVo {
}

View File

@@ -16,6 +16,8 @@ public enum ErrorCode {
// ========== 账号模块 ==========
USERNAME_EXISTS(1001, "用户名已存在"),
BAD_CREDENTIALS(1002, "用户名或密码错误"),
REFRESH_TOKEN_INVALID(1003, "刷新令牌无效或已过期"),
REFRESH_TOKEN_MISSING(1004, "缺少刷新令牌"),
// ========== 用户模块 ==========
USER_NOT_FOUND(2001, "用户不存在"),