2016-07-04 9 views
0

Meine API:Warum funktioniert die rest_framework.authentication.BasicAuthentication nicht in meinen Codes?

from rest_framework.authentication import BasicAuthentication 
"""A simple API for file upload.""" 
class FileUploadView(APIView): 
    parser_classes = (MultiPartParser,) 
    authentication_classes = (BasicAuthentication,) 
    @method_decorator(csrf_exempt) 
    def dispatch(self, request, *args, **kwargs): 
     return super(FileUploadView, self).dispatch(request, *args, **kwargs) 

    def put(self, request): 
     print "request:", str(request.META) 
     print "request:", str(request.user.username) 
     try: 
      data = {'files': 'testing'} 
      response = Response(data) 
     except Exception as e: 
      print "Exception when put file:", e 
      data = { 'error' : str(e) } 
      response = Response(data) 

     return response 

Die oben ist mein API views.py. Ich benutzte den Postboten, um PUT zu machen. Ich habe nichts in der Header-Autorisierung hinzugefügt (keine HTTP_AUTHORIZATION im Anforderungsheader), ich kann {'files': 'testing'} als meine Antwort bekommen.

Warum? Alles fehlt? Danke

Antwort

0

Sie haben die Authentifizierungsklasse hinzugefügt, aber den Zugriff auf Ihre Ansicht nicht eingeschränkt. Standardmäßig hat der DRF uneingeschränkten Zugriff. Lesen Sie die Dokumentation Abschnitt:

Wenn nicht angegeben, ist diese Einstellung standardmäßig ermöglicht uneingeschränkten Zugang:

'DEFAULT_PERMISSION_CLASSES': (
    'rest_framework.permissions.AllowAny', 
) 

Setting the permission policy