ruo_python_django/admin-api/middleware/myAuthorization.py
2025-07-25 10:03:06 +08:00

51 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: <encoding name> -*-
"""
@Authormengying
@file myAuthorization.py
@date2023/6/14 16:46
@email: 652044581@qq.com
@desc: 授权相关
"""
import json
from django_redis import get_redis_connection
class CacheKeys:
TOKEN_NAME = "Authorization"
class Authorization:
@staticmethod
def get_user_info(request):
"""从header中获取访问人员信息放在header中"""
RedisClient = get_redis_connection()
token = request.headers.get(CacheKeys.TOKEN_NAME, '')
if not token:
setattr(request.headers, "user_info", {})
return None
user = RedisClient.get(token)
RedisClient.expire(token, 30 * 60)
if user:
user = json.loads(user)
user["token"] = token
else:
user = {}
setattr(request.headers, "user_info", user)
@staticmethod
def white_list_check(request):
"""判断访问名路径是否是白名单"""
white_list = ['/api/login', '/api/captchaImage', '/api/logout']
if request.path in white_list:
return True
else:
return False