【问题标题】:same token is getting on token request in django相同的令牌正在 django 中获取令牌请求
【发布时间】:2019-05-29 00:39:12
【问题描述】:

您好,我正在使用 django rest-framework,我想实现基于令牌的身份验证, 我正在使用基本的 rest-framework 令牌认证模块。

但它在每个请求上都返回相同的令牌。 ex(87d97bb2df56e39c12b38131087bcfd232720d9a),我在向我的服务器发送的每个请求中都会收到此字符串。

我的 setting.py 文件

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'restApp2', # Local Apps (My project's apps)
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
]

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
    'rest_framework.authentication.TokenAuthentication',  # <-- And here
],
}

urls.py 文件

from django.contrib import admin
from django.urls import path, include
from restApp2 import views
from rest_framework.authtoken.views import obtain_auth_token

urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello/', views.HelloView.as_view(), name='hello'),
    path('api-token-auth/', obtain_auth_token, name='api_token_auth'),  # <-- And here
    ]

urlpatterns += [
    path('accounts/', include('django.contrib.auth.urls')),
]

我正在使用 POSTMON 的 post 方法调用下面的 url。

POST http://localhost:7070/rest-auth/login/

作为回应,我得到了

87d97bb2df56e39c12b38131087bcfd232720d9a.

但我想要新请求时使用不同的令牌。

请帮帮我,谢谢

【问题讨论】:

标签: django python-3.x django-rest-framework django-rest-auth


【解决方案1】:

如果您检查obtain_auth_token 视图,它要么获取现有令牌,要么在数据库中没有用户令牌时创建新令牌。

如果每次登录都需要新令牌,则在注销后删除以前的令牌。 django rest framework - token authentication logout

【讨论】:

    猜你喜欢
    • 2012-02-16
    • 2014-02-17
    • 1970-01-01
    • 2017-12-12
    • 2012-02-14
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多