Update the JWT for easier scaling (#1011)

* Update the JWT for easier scaling

* Fix comment
This commit is contained in:
Wu Clan
2026-01-14 23:52:29 +08:00
committed by GitHub
parent 362a559236
commit f876162456
2 changed files with 50 additions and 25 deletions
+25 -15
View File
@@ -217,6 +217,30 @@ async def get_current_user(db: AsyncSession, pk: int) -> User:
return user
async def get_jwt_user(user_id: int) -> GetUserInfoWithRelationDetail:
"""
获取 JWT 用户
:param user_id:
:return:
"""
cache_user = await redis_client.get(f'{settings.JWT_USER_REDIS_PREFIX}:{user_id}')
if not cache_user:
async with async_db_session() as db:
current_user = await get_current_user(db, user_id)
user = GetUserInfoWithRelationDetail.model_validate(current_user)
await redis_client.setex(
f'{settings.JWT_USER_REDIS_PREFIX}:{user_id}',
settings.TOKEN_EXPIRE_SECONDS,
user.model_dump_json(),
)
else:
# TODO: 在恰当的时机,应替换为使用 model_validate_json
# https://docs.pydantic.dev/latest/concepts/json/#partial-json-parsing
user = GetUserInfoWithRelationDetail.model_validate(from_json(cache_user, allow_partial=True))
return user
def superuser_verify(request: Request, _token: str = DependsJwtAuth) -> bool:
"""
验证当前用户超级管理员权限
@@ -247,21 +271,7 @@ async def jwt_authentication(token: str) -> GetUserInfoWithRelationDetail:
if token != redis_token:
raise errors.TokenError(msg='Token 已失效')
cache_user = await redis_client.get(f'{settings.JWT_USER_REDIS_PREFIX}:{user_id}')
if not cache_user:
async with async_db_session() as db:
current_user = await get_current_user(db, user_id)
user = GetUserInfoWithRelationDetail.model_validate(current_user)
await redis_client.setex(
f'{settings.JWT_USER_REDIS_PREFIX}:{user_id}',
settings.TOKEN_EXPIRE_SECONDS,
user.model_dump_json(),
)
else:
# TODO: 在恰当的时机,应替换为使用 model_validate_json
# https://docs.pydantic.dev/latest/concepts/json/#partial-json-parsing
user = GetUserInfoWithRelationDetail.model_validate(from_json(cache_user, allow_partial=True))
return user
return await get_jwt_user(user_id)
# 超级管理员鉴权依赖注入