简体   繁体   English

使用Django Rest Framework登录

[英]Login with Django Rest Framework

I have a website in Django and I'm developing an Android app. 我在Django有一个网站,我正在开发一个Android应用程序。 In one activity I have to login the user. 在一项活动中,我必须登录用户。 I have installed the Django Rest Framework but I'm afraid that is insecure to send the username and password. 我已经安装了Django Rest Framework,但我担心发送用户名和密码是不安全的。 What's the best way to do a login using Rest Framework? 使用Rest Framework进行登录的最佳方法是什么?

You may use basic authentication, by providing an user name and password, but it has to be done over https. 您可以通过提供用户名和密码来使用基本身份验证,但必须通过https完成。 Otherwise, there are different other authentication mechanism you may use. 否则,您可以使用不同的其他身份验证机制。 Have a look here . 看看这里 The appropriate one for a mobile application would probably be token authentication. 适用于移动应用程序的可能是令牌身份验证。

One of the most popular ways to authenticate with REST APIs is through tokens. 使用REST API进行身份验证的最常用方法之一是通过令牌。 What this essentially means is to set up an API endpoint that receives the "username" and "password" and responds with a token. 这实际上意味着设置一个API端点,它接收“用户名”和“密码”并用令牌响应。 And then each request make to the API should go with this token as a header or a param, and get resolved and validated before running the function behind the API. 然后,对API的每个请求应该使用此标记作为标头或参数,并在运行API后面的函数之前得到解析和验证。 This way you username and password will only be send once. 这样您的用户名和密码只会发送一次。 Of course, even with this, it is recommended to use HTTPS. 当然,即使这样,也建议使用HTTPS。

A good way to do this is to use djangorestframework-jwt ( http://getblimp.github.io/django-rest-framework-jwt/ ). 一个好方法是使用djangorestframework-jwt( http://getblimp.github.io/django-rest-framework-jwt/ )。 This is an app providing JSON Web Token functionality for Django Rest Framework APIs. 这是一个为Django Rest Framework API提供JSON Web Token功能的应用程序。

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

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