[英]Angular 2 frontend django 2 REST framework backend user auth
I'm new to Django and JavaScript, so please forgive me if this is an obvious question.我是 Django 和 JavaScript 的新手,所以如果这是一个明显的问题,请原谅我。
What is the best way to authenticate users?验证用户身份的最佳方法是什么?
I can only find posts about using this which doesn't support django 2.我只能找到有关使用帖子这个不Django的2支持。
Thanks for your help.谢谢你的帮助。
django-rest-framework
provides several solutions for authentication. django-rest-framework
提供了多种身份验证解决方案。 I recommend to read carefully the docs : http://www.django-rest-framework.org/api-guide/authentication/我建议仔细阅读文档: http : //www.django-rest-framework.org/api-guide/authentication/
I've already used Token authentication on several projects and it works fine:我已经在几个项目中使用了Token 身份验证,它工作正常:
You need to install the rest_framework.authtoken
app in your project.您需要在您的项目中安装rest_framework.authtoken
应用程序。 This app is provided with django-rest-framework此应用程序提供 django-rest-framework
Then you need create a token for every user.然后您需要为每个用户创建一个令牌。 There are several ways described in the docs.文档中描述了几种方法。
On the client side, you need to implement a login form.在客户端,您需要实现一个登录表单。 Your backend will check user and password and gets back the user token.您的后端将检查用户和密码并取回用户令牌。
Then you can pass this token in the header of every request:然后您可以在每个请求的标头中传递此令牌:
Authorization: Token value-of-the-token
Make sure to use https
if you use Token authentication.如果您使用令牌身份验证,请确保使用https
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.