我有一个简单的Django应用程序管理用户编辑和注册与Django和JWT。根据文档,我已经将应用程序设置为使用JWT与带有settings.py的插件简单jwt:
预期行为:Cookies应该设置在/refresh auth/和/token/刷新上
问题:Cookie只在/refresh auth/上正确设置,而在/token/上没有正确设置。
因此,我还添加了建议的中间件。
在这里,我的settings.py:
'dj_rest_auth.jwt_auth.JWTCookieAuthentication',={“默认_身份验证_类”:REST_FRAMEWORK }
REST_USE_JWT = True
JWT_AUTH_COOKIE = 'my-app-auth'
JWT_AUTH_REFRESH_COOKIE = 'my-refresh-token'
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'auth_app.middleware.MoveJWTRefreshCookieIntoTheBody'
]我的urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url, re_path
from auth_app import views
# JWT CONF
from rest_framework_simplejwt.views import (
TokenRefreshView,
TokenVerifyView,
)
from dj_rest_auth.registration.views import VerifyEmailView, RegisterView
from dj_rest_auth.views import PasswordResetConfirmView
from allauth.account.views import confirm_email
urlpatterns = [
path('admin/', admin.site.urls),
path('token/verify/', TokenVerifyView.as_view(), name='token_verify'),
path('token/refresh/', TokenRefreshView().as_view(), name='token_refresh'),
path('api/protected/', views.ProtectedView.as_view(), name='protected_view'),
path('api/open/', views.OpenView.as_view(), name='open_view'),
# dj-rest-auth common
path('dj-rest-auth/', include('dj_rest_auth.urls')),
# dj-rest-auth registration
path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')),
path('dj-rest-auth/account-confirm-email/', VerifyEmailView.as_view(), name='account_email_verification_sent'),
re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$', VerifyEmailView.as_view(),
name='account_confirm_email'),
path(
'rest-auth/password/reset/confirm/<slug:uidb64>/<slug:token>/',
PasswordResetConfirmView.as_view(), name='password_reset_confirm'
),
]很长时间以来,我一直在使用所提供的解决方案来检查我的解决方案,但仍然没有在刷新端点上设置cookie。我是不是遗漏了什么?
发布于 2022-10-24 12:53:32
我解决了这个问题!
如果将来有人要解决这个问题,请在这里解决:在TokenRefreshView中,您必须用属于dj auth的get_refresh_view()替换simplejwt get_refresh_view()。
换句话说,正确的JWT配置是:
# JWT CONF
from rest_framework_simplejwt.views import TokenVerifyView
from dj_rest_auth.jwt_auth import get_refresh_viewhttps://stackoverflow.com/questions/74180659
复制相似问题