简体   繁体   English

如何在Django中验证有效的超级用户

[英]How to authenticate valid super user or not in django

I have created a superuser in Django. 我在Django中创建了一个超级用户。 Is there any way I can authenticate the superuser credentials using a REST API like from Postman? 有什么办法可以使用Postman这样的REST API验证超级用户凭据? or Do I need to write a scripted REST API? 还是我需要编写脚本化的REST API?

If my question is too broad please let me know. 如果我的问题太笼统,请告诉我。 I will update my question. 我将更新我的问题。

Regards 问候

Personally, I use django-rest-framework http://www.django-rest-framework.org/ , which has a third-party module called django-rest-auth http://django-rest-auth.readthedocs.io/en/latest/ that provides API endpoints to handle login, registration, and other user access. 就个人而言,我使用django-rest-framework http://www.django-rest-framework.org/ ,它具有一个名为django-rest-auth http://django-rest-auth.readthedocs.io的第三方模块/ en / latest /提供API端点来处理登录,注册和其他用户访问。 The documentation is pretty good and has been around for quite some time. 该文档非常好,已经存在了一段时间。

The django.contrib.auth User model has boolean values, is_staff and is_superuser and there is a decorator to check for staff https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#the-staff-member-required-decorator . django.contrib.auth用户模型具有布尔值,is_staff和is_superuser,并且有一个装饰器来检查人员https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#the-staff-member必需的装饰器

example from docs docs中的示例

from django.contrib.admin.views.decorators import staff_member_required

@staff_member_required
def my_view(request):

You could easily create your own decorator by implementing something similar in the style of the following: 您可以通过实现类似于以下样式的内容来轻松创建自己的装饰器:

def super_user_required(view_func=None, 
                          redirect_field_name=REDIRECT_FIELD_NAME,
                          login_url='admin:login'):
    """
    Decorator for views that checks that the user is logged in and is a superuser
    member, redirecting to the login page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_superuser,
        login_url=login_url,
        redirect_field_name=redirect_field_name
    )
    if view_func:
        return actual_decorator(view_func)
    return actual_decorator

The package provides optional JWT support. 该软件包提供了可选的JWT支持。 https://getblimp.github.io/django-rest-framework-jwt/ https://getblimp.github.io/django-rest-framework-jwt/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM