http://www.django-rest-framework.org/api-guide/authentication/#custom-authenticationDjango的REST框架定制认证
我想在我的Django应用程序使用自定义身份验证,但无法找到如何应用它。任何人都可以帮助我这个。我在文档中给出的例子很清楚,但他们没有提到创建这个新类的位置以及如何使用它。
在此先感谢!
http://www.django-rest-framework.org/api-guide/authentication/#custom-authenticationDjango的REST框架定制认证
我想在我的Django应用程序使用自定义身份验证,但无法找到如何应用它。任何人都可以帮助我这个。我在文档中给出的例子很清楚,但他们没有提到创建这个新类的位置以及如何使用它。
在此先感谢!
在保存您的api文件的文件夹中,创建另一个文件来保存您的自定义验证类,如authentication.py
。然后在您的设置下,在DEFAULT_AUTHENTICATION_CLASSES下,指向您的自定义认证类。
如何在DRF中实现自定义认证方案?
要实施自定义认证方案,我们需要继承DRF的BaseAuthentication
类并覆盖.authenticate(self, request)
方法。
如果认证成功,该方法应返回(user, auth)
的二元组,否则返回None
。在某些情况下,我们可能会从.authenticate()
方法中引发AuthenticationFailed
例外。
(来自DRF docs)实施例:
假设我们要验证的任何传入的请求如在名为'X_USERNAME'
定制请求头由username
给出的用户。
第1步:创建自定义验证类
为了做到这一点,我们将在my_app
创建authentication.py
文件。
# my_app/authentication.py
from django.contrib.auth.models import User
from rest_framework import authentication
from rest_framework import exceptions
class ExampleAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
username = request.META.get('X_USERNAME') # get the username request header
if not username: # no username passed in request headers
return None # authentication did not succeed
try:
user = User.objects.get(username=username) # get the user
except User.DoesNotExist:
raise exceptions.AuthenticationFailed('No such user') # raise exception if user does not exist
return (user, None) # authentication successful
步骤2:指定自定义验证类
,我们需要在我们的DRF设置来定义这个认证类创建自定义的验证类之后。这样做,所有请求都将基于此认证方案进行认证。
'DEFAULT_AUTHENTICATION_CLASSES': (
'my_app.authentication.ExampleAuthentication', # custom authentication class
...
),
注:如果你想使用这个定制验证级上按次基础或每个视图集基础,而不是在全球层面上,你可以明确地在你的意见确定此身份验证类。
class MyView(APIView):
authentication_classes = (ExampleAuthentication,) # specify this authentication class in your view
...
贝娄是一个简单的例子,可以用来实现自定义验证。在访问端点的示例中,您必须在POST数据上传递用户名&密码。
urls.py
urlpatterns = [
url(r'^stuff/', views.MyView.as_view()),
...
]
意见。PY
from django.contrib.auth.models import User
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from rest_framework import exceptions
from rest_framework import authentication
from django.contrib.auth import authenticate, get_user_model
from rest_framework.authentication import BasicAuthentication,
SessionAuthentication
class ExampleAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
# Get the username and password
username = request.data.get('username', None)
password = request.data.get('password', None)
if not username or not password:
raise exceptions.AuthenticationFailed(_('No credentials provided.'))
credentials = {
get_user_model().USERNAME_FIELD: username,
'password': password
}
user = authenticate(**credentials)
if user is None:
raise exceptions.AuthenticationFailed(_('Invalid username/password.'))
if not user.is_active:
raise exceptions.AuthenticationFailed(_('User inactive or deleted.'))
return (user, None) # authentication successful
class MyView(APIView):
authentication_classes = (SessionAuthentication, ExampleAuthentication,)
permission_classes = (IsAuthenticated,)
def post(self, request, format=None):
content = {
'user': unicode(request.user),
'auth': unicode(request.auth), # None
}
return Response(content)
卷曲
curl -v -X POST http://localhost:8000/stuff/ -d 'username=my_username&password=my_password'
可以请你就如何使用这种使用curl的例子吗? – momokjaaaaa
@momokjaaaaa检查此SO链接是否在POST请求中发送标头。 http://stackoverflow.com/questions/356705/how-to-send-a-header-using-a-http-request-through-a-curl-call –