Django中的HTTP基本认证

Catalogue
  1. 1. 参考资料
def func(request):
key = bytes(settings.AUTO_KEY, encoding='UTF-8')
if 'HTTP_AUTHORIZATION' in request.META:
auth = request.META['HTTP_AUTHORIZATION'].split()
if len(auth) == 2 and auth[0].lower() == "basic":
user_name, pass_word = str(base64.b64decode(auth[1].encode('utf-8')), 'utf-8').split(':')
if not (user_name in settings.AUTO_USER and pass_word == hmac.new(key, bytes(user_name, encoding='UTF-8'),
digestmod=hashlib.sha1).hexdigest()):
return JsonResponse(dict(status=401, data={}, msg="授权未通过"))
else:
return JsonResponse(dict(status=401, data={}, msg="授权未通过"))
else:
return JsonResponse(dict(status=401, data={}, msg="授权未通过"))

参考资料