Django使用装饰器做简单的权限验证




from django.http import JsonResponse
def get_api_user_id(request):
    try:
        token = request.META.get('HTTP_AUTHORIZATION')[5:]
        token_user = jwt_decode_handler(token)
        user_id = token_user['user_id']

    except:
        user_id = None
    return user_id
def has_permission(func):
    def judgment(*args):
        user_id = get_api_user_id(args[0])
        if not user_id:
            res = {
                'errno': response_code.NO_LOGIN,
                'statusCode': 200,
                'errMsg': '没有登录',
            }
            return JsonResponse(res)
        else:
            return func(args[0])
    return judgment
#使用
@has_permission
def collect_add(request):

权限粒度只到是否登录

暂无评论