DRF令牌认证教程 [英] DRF Token Authentication Tutorial

查看:108
本文介绍了DRF令牌认证教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Django很新,但我想学习如何使用Angularjs实现DRF令牌认证。我发现的一些教程在展示如何设置它们以及源代码等方面没有太大的帮助。



另外,出于生产目的使用第三方软件更实用吗?或者设置自己的(这是一个个人项目,所以时间贡献不是一个问题)。



我的令牌代码的代码: Github

解决方案

在settings.py

  INSTALLED_APPS =(
...
'rest_framework.authtoken'

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':[
'rest_framework.authentication.TokenAuthentication',
],
} b $ b

在signals.py

从django.conf导入设置
从django.db.models.signals导入post_save
从django.dispatch导入收件人
从rest_framework.authtoken.models导入令牌

@receiver(post_save,sender = settings.AUTH_USER_MODEL)
def create_auth_token(sender,instance = None,created = False,** kwargs):
如果创建:
Token.objects.create(user = instance)

在views.py

  class ExampleAuthToken(APIView):
def post(self,request,format = None):
username = request.data.get(username)
password = request.data.get(password)
try:
user = User.objects.get(username = username)
除了User.DoesNotExist:
user = User.objects.create_user(username = username)
user.set_password(password)
user.save()
content = {
'user':unicode(user.username),
'token':unicode(user.auth_token),
}
返回响应(内容)

在urls.py

  urlpatterns = [
url(r'^ authtoken /',ExampleAuthToken.as_view(),name ='authtoken'),
]

使用ang调用ular j s;> $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
};

$ .post(http:// localhost:8000 / authtoken /,凭证{
success:function(data){
console.log(data);
}
}


I am pretty new to Django, but I want to learn how to implement a DRF Token Authentication with Angularjs. Some of the tutorials I have found haven't been too helpful in showing how to set it up, along with their source code etc...

Also, for production purposes, is it more practical to use a third party package? Or set up my own (it's for a personal project, so time contribution is not an issue).

My Buggy Code for Token Auth: Github

解决方案

In settings.py

INSTALLED_APPS = (
    ...
    'rest_framework.authtoken'
)
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ],
}

In signals.py

from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from rest_framework.authtoken.models import Token

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)

In views.py

class ExampleAuthToken(APIView):
    def post(self, request, format=None):
        username = request.data.get("username")
        password = request.data.get("password")
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            user = User.objects.create_user(username=username)
            user.set_password(password)
            user.save()
        content = {
            'user': unicode(user.username),
            'token': unicode(user.auth_token),
        }
        return Response(content)

In urls.py

urlpatterns = [
    url(r'^authtoken/', ExampleAuthToken.as_view(), name='authtoken'),
]

To call using the angularjs;

var credentials = {
  username: "root",
  password: "root123"
};

$.post("http://localhost:8000/authtoken/", credentials {
    success: function(data){
          console.log(data);
    }
}

这篇关于DRF令牌认证教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆