views.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:django-sysinfo 作者: saxix 项目源码 文件源码
def http_basic_auth(func):
    @wraps(func)
    def _decorator(request, *args, **kwargs):
        from django.contrib.auth import authenticate, login

        if "HTTP_AUTHORIZATION" in request.META:
            authmeth, auth = request.META["HTTP_AUTHORIZATION"].split(b" ", 1)
            if authmeth.lower() == b"basic":
                auth = codecs.decode(auth.strip(), "base64")
                username, password = auth.split(b":", 1)
                user = authenticate(username=username, password=password)
                if user and is_authorized(user):
                    login(request, user)
                else:
                    raise PermissionDenied()
        return func(request, *args, **kwargs)

    return _decorator
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号