在 DjangoRestFramework 中更改令牌键名称的目的是什么

Myr*_*hyn 5 token django-rest-framework

在 DjangoRestFramework 中,您可以更改标头中的关键字以进行令牌验证。

来自文档

注意:如果要在标头中使用不同的关键字,例如 Bearer,只需将 TokenAuthentication 子类化并设置关键字类变量即可。

更改默认关键字的目的是什么?我看过“Bearer”、“Basic”和其他一些变体,但只是不明白这样做的目的。谁能解释一下?

Dru*_*lan 11

我发现我们需要如何keywordTokenAuthentication.

class BearerAuthentication(authentication.TokenAuthentication):
    '''
    Simple token based authentication using utvsapitoken.

    Clients should authenticate by passing the token key in the 'Authorization'
    HTTP header, prepended with the string 'Bearer '.  For example:

    Authorization: Bearer 956e252a-513c-48c5-92dd-bfddc364e812
    '''
    keyword = 'Bearer'
Run Code Online (Sandbox Code Playgroud)

然后,而不是使用authentication.TokenAuthenticationsettings.py,我们将使用BearerAuthentication

# settings.py
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (                                                                                                                                                                                       
        'your.models.BearerAuthentication',
    )
}
Run Code Online (Sandbox Code Playgroud)


Myr*_*hyn 0

许多网络服务器支持多种授权方法。在这些情况下,仅发送令牌是不够的。

来源