diff --git a/backend/common/security/jwt.py b/backend/common/security/jwt.py index 5e11099..c8ebd79 100755 --- a/backend/common/security/jwt.py +++ b/backend/common/security/jwt.py @@ -69,26 +69,19 @@ class CustomHTTPBearer(HTTPBearer): # Check for x-wx-openid header first (WeChat Cloud Hosting authentication) wx_openid = request.headers.get('x-wx-openid') # print(request.headers) - # print(wx_openid) - if not wx_openid: - wx_openid = request.headers.get('X-WX-OPENID') if wx_openid: # Handle WeChat Cloud Hosting authentication wx_unionid = request.headers.get('x-wx-unionid') - if not wx_unionid: - wx_unionid = request.headers.get('X-WX-UNIONID') try: # Check if we have a cached token for this openid cached_token = await redis_client.get(f'wx_openid_token:{wx_openid}') if cached_token: - # Verify the cached token is still valid via standard flow try: await jwt_authentication(cached_token) return HTTPAuthorizationCredentials(scheme="Bearer", credentials=cached_token) except Exception: - # If token is invalid, remove it from cache and recreate below await redis_client.delete(f'wx_openid_token:{wx_openid}') # If no cached token or invalid token, authenticate the user diff --git a/backend/core/registrar.py b/backend/core/registrar.py index a8ff556..f00091d 100755 --- a/backend/core/registrar.py +++ b/backend/core/registrar.py @@ -38,6 +38,7 @@ async def register_init(app: FastAPI): await create_table() # 连接 redis await redis_client.open() + # await redis_client.refresh_all() # 初始化 limiter await FastAPILimiter.init( redis_client, diff --git a/backend/database/redis.py b/backend/database/redis.py index 40e81a1..c9eddb4 100755 --- a/backend/database/redis.py +++ b/backend/database/redis.py @@ -59,6 +59,13 @@ class RedisCli(Redis): if keys: await self.delete(*keys) + async def refresh_all(self): + try: + await self.flushall() + except Exception as e: + log.error('❌ 数据库 redis 刷新异常 {}', e) + raise + # 创建 redis 客户端单例 redis_client: RedisCli = RedisCli()