简体   繁体   English

django身份验证和密码重置

[英]django authentication and password reset

So, i am currently working on a web application project and i implemented authentication and password confirmation successfully. 因此,我目前正在从事Web应用程序项目,并且成功实现了身份验证和密码确认。

But my issue is that i did it using html templates and now the requirement came up that we have to develop our application using api's for the backened. 但是我的问题是我使用html模板完成了此操作,现在要求我们必须使用api开发支持后端的应用程序。

Now, i am new to api and really confused how to use the authentication system i built (as we have to provide template to the in-built implemented class and they accept the values from their itself) 现在,我是api的新手,并且真的很困惑如何使用我构建的身份验证系统(因为我们必须为内置的实现类提供模板,并且它们接受其自身的值)

Is it possible to actually see and manage the registered users from the code-behind while still using there in-built mechanism 是否仍然可以使用内置机制从背后的代码中实际查看和管理注册用户

For password change you can use this generic view using the inbuilt Django auth framework 要更改密码,您可以使用内置的Django身份验证框架使用此通用视图

@login_required
def change_password(request):
    if request.method == "POST":
        form = PasswordChangeForm(request.user, request.POST)
        if form.is_valid():
            user = form.save()
            # Important to update the session otherwise user will have to login again
            update_session_auth_hash(request, user)
            # Server side alert
            print("Password changed for {0}".format(user.username))
            return redirect('/index/')
        else:
            print(form.errors)
    else:
        form = PasswordChangeForm(request.user)
    return render(request, 'website/changepassword.html', {'form': form})

You need to use djangorestframework , and use the decorator @apiview(['GET', 'POST']) to create a RestAPI 您需要使用djangorestframework ,并使用装饰器@apiview(['GET', 'POST'])创建RestAPI

You can use TokenAuthentication available in django rest framework. 您可以使用django rest框架中提供的TokenAuthentication。 See what documentation says: 查看说明文件内容:

TokenAuthentication TokenAuthentication

This authentication scheme uses a simple token-based HTTP Authentication scheme. 此身份验证方案使用简单的基于令牌的HTTP身份验证方案。 Token authentication is appropriate for client-server setups, such as native desktop and mobile clients. 令牌认证适用于客户端-服务器设置,例如本机台式机和移动客户端。

To use the TokenAuthentication scheme you'll need to configure the authentication classes to include TokenAuthentication, and additionally include rest_framework.authtoken in your INSTALLED_APPS setting: 要使用TokenAuthentication方案,您需要配置身份验证类以包括TokenAuthentication,并在INSTALLED_APPS设置中另外包含rest_framework.authtoken:

INSTALLED_APPS = (
...
'rest_framework.authtoken'

)

Note: Make sure to run manage.py migrate after changing your settings. 注意:确保在更改设置后运行manage.py migration。 The rest_framework.authtoken app provides Django database migrations. rest_framework.authtoken应用程序提供Django数据库迁移。

You'll also need to create tokens for your users. 您还需要为用户创建令牌。

from rest_framework.authtoken.models import Token

token = Token.objects.create(user=...)
print token.key

For clients to authenticate, the token key should be included in the Authorization HTTP header. 为了使客户端进行身份验证,令牌密钥应包含在Authorization HTTP标头中。 The key should be prefixed by the string literal "Token", with whitespace separating the two strings. 密钥应以字符串文字“ Token”作为前缀,并用空格分隔两个字符串。 For example: 例如:

Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b

Note: If you want to use a different keyword in the header, such as Bearer, simply subclass TokenAuthentication and set the keyword class variable. 注意:如果要在标头中使用其他关键字(例如Bearer),则只需对TokenAuthentication进行子类化并设置关键字class变量。

If successfully authenticated, TokenAuthentication provides the following credentials. 如果成功通过身份验证,则TokenAuthentication将提供以下凭据。

  • request.user will be a Django User instance. request.user将是Django User实例。
  • request.auth will be a rest_framework.authtoken.models.Token instance. request.auth将是rest_framework.authtoken.models.Token实例。

Unauthenticated responses that are denied permission will result in an HTTP 401 Unauthorized response with an appropriate WWW-Authenticate header. 拒绝权限的未经身份验证的响应将导致带有适当的WWW-Authenticate标头的HTTP 401未经授权的响应。 For example: 例如:

WWW-Authenticate: Token

The curl command line tool may be useful for testing token authenticated APIs. curl命令行工具对于测试令牌认证的API可能有用。 For example: 例如:

curl -X GET http://127.0.0.1:8000/api/example/ -H 'Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'

Note: If you use TokenAuthentication in production you must ensure that your API is only available over https. 注意:如果在生产中使用TokenAuthentication,则必须确保您的API仅可通过https使用。

Source: http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication 来源: http : //www.django-rest-framework.org/api-guide/authentication/#tokenauthentication

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

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