我有一个用 Django REST Framework (DRF) 编写的应用程序。此外,还有这样的注册端点:
from refreshtoken.views import delegate_jwt_token
from rest_framework_jwt.views import obtain_jwt_token
urlpatterns = [
path('api-token-auth/', obtain_jwt_token),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path(r'refresh-token', delegate_jwt_token, name='refresh-token'),
]
此外,还有一些端点需要授权。
所以,我正在尝试使用curl
客户端提取我的令牌:
curl -X POST -H "Content-Type: application/json" http://127.0.0.1:8000/api-token-auth/ -d '{"username": "test", "password": "testpassword"}'
它返回如下内容:
{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiYTdlMmIyMjItZTZkNy00NjhiLTkxNzYtOTE2YzAwZWRhY2E2IiwidXNlcm5hbWUiOiJ0ZXN0IiwiZXhwIjoxNTUwNTEwNDAwLCJlbWFpbCI6InRlc3RAZHhhbXBsZS5jb20iLCJpc19zdGFmZiI6ZmFsc2UsImdyb3VwcyI6W10sInN1YnNjcmliZWQiOmZhbHNlLCJ0ZWxlZ3JhbV9zdWJzY3JpYmVkIjpmYWxzZX0.OExR9TlO3GUisYAu_D86CJ6hgF1EcofpQA0MZ1ENT2c","refresh_token":"1ab03e609d7a7ae05ce104c73858a346a0438e72"}
然后,使用我想登录的这个令牌,使用令牌:
curl -X POST -H "Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiYTdlMmIyMjItZTZkNy00NjhiLTkxNzYtOTE2YzAwZWRhY2E2IiwidXNlcm5hbWUiOiJ0ZXN0IiwiZXhwIjoxNTUwNTEwNDAwLCJlbWFpbCI6InRlc3RAZHhhbXBsZS5jb20iLCJpc19zdGFmZiI6ZmFsc2UsImdyb3VwcyI6W10sInN1YnNjcmliZWQiOmZhbHNlLCJ0ZWxlZ3JhbV9zdWJzY3JpYmVkIjpmYWxzZX0.OExR9TlO3GUisYAu_D86CJ6hgF1EcofpQA0MZ1ENT2c" -H "Content-Type: application/json" http://127.0.0.1:8000/api-auth/login/ -d '{"username": "test", "password": "testpassword"}'
它返回403 (Forbidden)
CSRF 验证失败。请求中止。
为什么我做错了?
这里有一个有趣的点:
对令牌的每个请求都会为同一个用户:密码对返回一个新令牌(是预期的还是我的令牌过期太快)?