How to authorize django-rest-knox login path without 401 error?

后端 未结 3 819
予麋鹿
予麋鹿 2021-01-21 20:39

I\'m trying to put an authentication api on my django app so I can start using a Vue+Node frontend. I\'ve read over the knox documentation, but I don\'t seem to see anything su

3条回答
  •  清歌不尽
    2021-01-21 21:05

    Short answer, go to settings.py and edit DEFAULT_AUTHENTICATION_CLASSES :

    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': (
             'rest_framework.authentication.BasicAuthentication' ,
             'knox.auth.TokenAuthentication',),
    }
    

    The issue here is that DEFAULT_AUTHENTICATION_CLASSES is set to knox.auth.TokenAuthentication, this means that all views will have this as their default auth class thus needs a token including the knox login view itself. By adding 'rest_framework.authentication.BasicAuthentication' , auth classes will be tested in sequence until one of them works, so for login, you can now do as an example:

    curl --location --request POST 'http://127.0.0.1:8000/api/v1/auth/login/' \ --header 'Authorization: Basic username:password'
    

提交回复
热议问题