feat: 使用双token逻辑
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package com.webgame.webgamebackend.service.impl;
|
||||
|
||||
import com.webgame.webgamebackend.common.exception.BusinessException;
|
||||
import com.webgame.webgamebackend.common.exception.ErrorCode;
|
||||
import com.webgame.webgamebackend.common.utils.RedisCacheUtil;
|
||||
import com.webgame.webgamebackend.service.RefreshTokenService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.HexFormat;
|
||||
|
||||
/**
|
||||
* 刷新令牌服务实现,基于 Redis 存储
|
||||
*
|
||||
* <p>数据结构:</p>
|
||||
* <ul>
|
||||
* <li>xzg:refresh:{token} → userId(正向映射,用于校验)</li>
|
||||
* <li>xzg:refresh:user:{userId} → token(反向映射,用于登录时清除旧 token)</li>
|
||||
* </ul>
|
||||
* <p>TTL: 30 天,由 Redis 自动过期</p>
|
||||
* <p>一个用户同一时间只保留一个有效的 refresh token,新登录会使旧 token 失效</p>
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class RefreshTokenServiceImpl implements RefreshTokenService {
|
||||
|
||||
/** Redis key 前缀 */
|
||||
private static final String PREFIX = "refresh";
|
||||
|
||||
/** 用户→token 反向映射 key 前缀 */
|
||||
private static final String USER_PREFIX = "refresh:user";
|
||||
|
||||
/** 刷新令牌有效期:30 天(毫秒) */
|
||||
private static final long EXPIRE_MS = 2_592_000_000L;
|
||||
|
||||
/** 安全随机数生成器 */
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
|
||||
private final RedisCacheUtil redisCacheUtil;
|
||||
|
||||
@Override
|
||||
public String create(String userId) {
|
||||
// 先清除该用户之前的 refresh token,确保一个用户只有一个有效 token
|
||||
String oldToken = redisCacheUtil.getCache(USER_PREFIX, userId, String.class, EXPIRE_MS);
|
||||
if (oldToken != null) {
|
||||
redisCacheUtil.removeCache(PREFIX, oldToken);
|
||||
log.debug("已清除旧的刷新令牌: userId={}", userId);
|
||||
}
|
||||
// 创建新 token
|
||||
String token = generateToken();
|
||||
boolean ok = redisCacheUtil.cacheValue(PREFIX, token, userId, EXPIRE_MS);
|
||||
if (!ok) {
|
||||
log.error("刷新令牌写入 Redis 失败: userId={}", userId);
|
||||
throw new BusinessException(ErrorCode.INTERNAL_ERROR, "刷新令牌生成失败");
|
||||
}
|
||||
// 维护反向映射
|
||||
redisCacheUtil.cacheValue(USER_PREFIX, userId, token, EXPIRE_MS);
|
||||
log.debug("刷新令牌已创建: userId={}", userId);
|
||||
return token;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String validate(String token) {
|
||||
if (token == null || token.isBlank()) {
|
||||
throw new BusinessException(ErrorCode.REFRESH_TOKEN_MISSING);
|
||||
}
|
||||
String userId = redisCacheUtil.getCache(PREFIX, token, String.class, EXPIRE_MS);
|
||||
if (userId == null) {
|
||||
throw new BusinessException(ErrorCode.REFRESH_TOKEN_INVALID);
|
||||
}
|
||||
return userId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String rotate(String oldToken) {
|
||||
String userId = validate(oldToken);
|
||||
redisCacheUtil.removeCache(PREFIX, oldToken);
|
||||
redisCacheUtil.removeCache(USER_PREFIX, userId);
|
||||
log.debug("旧刷新令牌已删除: userId={}", userId);
|
||||
return create(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void revoke(String token) {
|
||||
if (token != null && !token.isBlank()) {
|
||||
String userId = redisCacheUtil.getCache(PREFIX, token, String.class, EXPIRE_MS);
|
||||
if (userId != null) {
|
||||
redisCacheUtil.removeCache(USER_PREFIX, userId);
|
||||
}
|
||||
redisCacheUtil.removeCache(PREFIX, token);
|
||||
log.debug("刷新令牌已撤销");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 64 位十六进制随机字符串作为刷新令牌
|
||||
*/
|
||||
private String generateToken() {
|
||||
byte[] bytes = new byte[32];
|
||||
RANDOM.nextBytes(bytes);
|
||||
return HexFormat.of().formatHex(bytes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user