项目结构类名称优化
This commit is contained in:
@@ -13,7 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.label.common.context.CompanyContext;
|
||||
import com.label.common.result.Result;
|
||||
import com.label.service.RedisService;
|
||||
import com.label.util.RedisKeyManager;
|
||||
import com.label.util.RedisUtil;
|
||||
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
@@ -98,7 +98,7 @@ public class TokenFilter extends OncePerRequestFilter {
|
||||
}
|
||||
String token = parts[1];
|
||||
// String token = authHeader.substring(7).trim();
|
||||
Map<Object, Object> tokenData = redisService.hGetAll(RedisKeyManager.tokenKey(token));
|
||||
Map<Object, Object> tokenData = redisService.hGetAll(RedisUtil.tokenKey(token));
|
||||
|
||||
if (tokenData == null || tokenData.isEmpty()) {
|
||||
writeUnauthorized(response, "令牌已过期或不存在");
|
||||
@@ -117,8 +117,8 @@ public class TokenFilter extends OncePerRequestFilter {
|
||||
TokenPrincipal principal = new TokenPrincipal(userId, role, companyId, username, token);
|
||||
SecurityUtils.getSubject().login(new BearerToken(token, principal));
|
||||
request.setAttribute("__token_principal__", principal);
|
||||
redisService.expire(RedisKeyManager.tokenKey(token), tokenTtlSeconds);
|
||||
redisService.expire(RedisKeyManager.userSessionsKey(userId), tokenTtlSeconds);
|
||||
redisService.expire(RedisUtil.tokenKey(token), tokenTtlSeconds);
|
||||
redisService.expire(RedisUtil.userSessionsKey(userId), tokenTtlSeconds);
|
||||
|
||||
filterChain.doFilter(request, response);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.label.common.shiro;
|
||||
|
||||
import com.label.service.RedisService;
|
||||
import com.label.util.RedisKeyManager;
|
||||
import com.label.util.RedisUtil;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -56,7 +56,7 @@ public class UserRealm extends AuthorizingRealm {
|
||||
}
|
||||
|
||||
private String getRoleFromCacheOrPrincipal(TokenPrincipal principal) {
|
||||
String permKey = RedisKeyManager.userPermKey(principal.getUserId());
|
||||
String permKey = RedisUtil.userPermKey(principal.getUserId());
|
||||
String cachedRole = redisService.get(permKey);
|
||||
if (cachedRole != null && !cachedRole.isEmpty()) {
|
||||
return cachedRole;
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.label.entity.SysCompany;
|
||||
import com.label.entity.SysUser;
|
||||
import com.label.mapper.SysCompanyMapper;
|
||||
import com.label.mapper.SysUserMapper;
|
||||
import com.label.util.RedisKeyManager;
|
||||
import com.label.util.RedisUtil;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -83,10 +83,10 @@ public class AuthService {
|
||||
tokenData.put("role", user.getRole());
|
||||
tokenData.put("companyId", user.getCompanyId().toString());
|
||||
tokenData.put("username", user.getUsername());
|
||||
redisService.hSetAll(RedisKeyManager.tokenKey(token), tokenData, tokenTtlSeconds);
|
||||
redisService.hSetAll(RedisUtil.tokenKey(token), tokenData, tokenTtlSeconds);
|
||||
|
||||
// 将 token 加入该用户的活跃会话集合(用于角色变更时批量更新/失效)
|
||||
String sessionsKey = RedisKeyManager.userSessionsKey(user.getId());
|
||||
String sessionsKey = RedisUtil.userSessionsKey(user.getId());
|
||||
redisService.sAdd(sessionsKey, token);
|
||||
// 防止 Set 无限增长:TTL = token 有效期(最后一次登录时滑动续期)
|
||||
redisService.expire(sessionsKey, tokenTtlSeconds);
|
||||
@@ -103,11 +103,11 @@ public class AuthService {
|
||||
public void logout(String token) {
|
||||
if (token != null && !token.isBlank()) {
|
||||
// 从用户会话集合中移除(若 token 仍有效则先读取 userId)
|
||||
String userId = redisService.hGet(RedisKeyManager.tokenKey(token), "userId");
|
||||
redisService.delete(RedisKeyManager.tokenKey(token));
|
||||
String userId = redisService.hGet(RedisUtil.tokenKey(token), "userId");
|
||||
redisService.delete(RedisUtil.tokenKey(token));
|
||||
if (userId != null) {
|
||||
try {
|
||||
redisService.sRemove(RedisKeyManager.userSessionsKey(Long.parseLong(userId)), token);
|
||||
redisService.sRemove(RedisUtil.userSessionsKey(Long.parseLong(userId)), token);
|
||||
} catch (NumberFormatException ignored) {}
|
||||
}
|
||||
log.info("用户退出,Token 已删除: {}", token);
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.label.entity.AnnotationTask;
|
||||
import com.label.entity.AnnotationTaskHistory;
|
||||
import com.label.mapper.AnnotationTaskMapper;
|
||||
import com.label.mapper.TaskHistoryMapper;
|
||||
import com.label.util.RedisKeyManager;
|
||||
import com.label.util.RedisUtil;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -48,7 +48,7 @@ public class TaskClaimService {
|
||||
*/
|
||||
@Transactional
|
||||
public void claim(Long taskId, TokenPrincipal principal) {
|
||||
String lockKey = RedisKeyManager.taskClaimKey(taskId);
|
||||
String lockKey = RedisUtil.taskClaimKey(taskId);
|
||||
|
||||
// 1. Redis SET NX 预锁(快速失败)
|
||||
boolean lockAcquired = redisService.setIfAbsent(
|
||||
@@ -104,7 +104,7 @@ public class TaskClaimService {
|
||||
.set(AnnotationTask::getClaimedAt, null));
|
||||
|
||||
// 清除 Redis 分布式锁
|
||||
redisService.delete(RedisKeyManager.taskClaimKey(taskId));
|
||||
redisService.delete(RedisUtil.taskClaimKey(taskId));
|
||||
|
||||
insertHistory(taskId, principal.getCompanyId(),
|
||||
"IN_PROGRESS", "UNCLAIMED",
|
||||
@@ -145,7 +145,7 @@ public class TaskClaimService {
|
||||
|
||||
// 重新设置 Redis 锁(防止并发再次争抢)
|
||||
redisService.setIfAbsent(
|
||||
RedisKeyManager.taskClaimKey(taskId),
|
||||
RedisUtil.taskClaimKey(taskId),
|
||||
principal.getUserId().toString(), CLAIM_LOCK_TTL);
|
||||
|
||||
insertHistory(taskId, principal.getCompanyId(),
|
||||
|
||||
@@ -15,7 +15,7 @@ import com.label.common.result.PageResult;
|
||||
import com.label.common.shiro.TokenPrincipal;
|
||||
import com.label.entity.SysUser;
|
||||
import com.label.mapper.SysUserMapper;
|
||||
import com.label.util.RedisKeyManager;
|
||||
import com.label.util.RedisUtil;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -126,11 +126,11 @@ public class UserService {
|
||||
.set(SysUser::getRole, newRole));
|
||||
|
||||
// 2. 更新所有活跃 Token 中的 role 字段(立即生效,无需重新登录)
|
||||
Set<String> tokens = redisService.sMembers(RedisKeyManager.userSessionsKey(userId));
|
||||
tokens.forEach(token -> redisService.hPut(RedisKeyManager.tokenKey(token), "role", newRole));
|
||||
Set<String> tokens = redisService.sMembers(RedisUtil.userSessionsKey(userId));
|
||||
tokens.forEach(token -> redisService.hPut(RedisUtil.tokenKey(token), "role", newRole));
|
||||
|
||||
// 3. 删除权限缓存(如 Shiro 缓存存在)
|
||||
redisService.delete(RedisKeyManager.userPermKey(userId));
|
||||
redisService.delete(RedisUtil.userPermKey(userId));
|
||||
|
||||
log.info("用户角色已变更: userId={}, newRole={}, 更新 {} 个活跃 Token", userId, newRole, tokens.size());
|
||||
}
|
||||
@@ -159,14 +159,14 @@ public class UserService {
|
||||
|
||||
// 禁用时:删除所有活跃 Token(立即失效)
|
||||
if ("DISABLED".equals(newStatus)) {
|
||||
Set<String> tokens = redisService.sMembers(RedisKeyManager.userSessionsKey(userId));
|
||||
tokens.forEach(token -> redisService.delete(RedisKeyManager.tokenKey(token)));
|
||||
redisService.delete(RedisKeyManager.userSessionsKey(userId));
|
||||
Set<String> tokens = redisService.sMembers(RedisUtil.userSessionsKey(userId));
|
||||
tokens.forEach(token -> redisService.delete(RedisUtil.tokenKey(token)));
|
||||
redisService.delete(RedisUtil.userSessionsKey(userId));
|
||||
log.info("账号已禁用,已删除 {} 个活跃 Token: userId={}", tokens.size(), userId);
|
||||
}
|
||||
|
||||
// 删除权限缓存
|
||||
redisService.delete(RedisKeyManager.userPermKey(userId));
|
||||
redisService.delete(RedisUtil.userPermKey(userId));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 查询 --
|
||||
|
||||
@@ -4,9 +4,10 @@ package com.label.util;
|
||||
* Centralized Redis key naming conventions.
|
||||
* All keys follow the pattern: prefix:{id}
|
||||
*/
|
||||
public final class RedisKeyManager {
|
||||
public final class RedisUtil {
|
||||
|
||||
private RedisKeyManager() {}
|
||||
private RedisUtil() {
|
||||
}
|
||||
|
||||
/** Session token key: token:{uuid} */
|
||||
public static String tokenKey(String uuid) {
|
||||
Reference in New Issue
Block a user