gpt4 book ai didi

django - 从 django-rest-auth 获取 React 中的用户详细信息会返回 403(禁止访问)- 使用 authtoken

转载 作者:行者123 更新时间:2023-12-02 16:36:40 24 4
gpt4 key购买 nike

在我的项目中,我使用 Django Rest Framework 管理后端,并在前端使用 React。

我目前正在实现身份验证系统,所以我刚刚将 DEFAULT_PERMISSION_CLASSESREST_FRAMEWORK 设置从 rest_framework.permissions.AllowAny 切换为rest_framework.authtoken(在一切正常之前)。

为了让事情更清楚,这是我的settings.py(来自INSTALLED_APPS):

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

# Custom Apps
'rest_framework',
'assessments',
'corsheaders',
'accounts',
'rest_framework.authtoken',
'rest_auth',

'django.contrib.sites',
'allauth',
'allauth.account',
'rest_auth.registration',

'allauth.socialaccount' # needed for delete user
]

MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'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',
]

ROOT_URLCONF = 'api.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'api.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
# 'rest_framework.permissions.AllowAny'
'rest_framework.authtoken'
]
}

CORS_ORIGIN_ALLOW_ALL = True


REST_AUTH_REGISTER_SERIALIZERS = {
'REGISTER_SERIALIZER': 'accounts.serializers.RegisterProfileSerializer',
}

REST_AUTH_SERIALIZERS = {
'USER_DETAILS_SERIALIZER': 'accounts.serializers.UserProfileSerializer'
}

SITE_ID = 2

此外,为了让事情尽可能“安静”,我使用了 django-rest-auth 以便让端点形成我的身份验证系统。

到目前为止,我已经设法为 rest-auth/registration/rest-auth/users 实现了 API 端点。同样,从 API 的角度来看,一切正常。事实上,我可以使用额外字段注册用户并正确查看用户详细信息。

我的问题是,当我尝试通过 React 中的 fetch 函数发出一个 GET 请求时,我收到了一个 GET http://127.0. 0.1:8000/rest-auth/user/403(禁止)

我认为这是由于 token 造成的,所以经过一番搜索后我发现可以通过这种方式发出获取请求:

async getUserRole() {
await fetch("http://127.0.0.1:8000/rest-auth/user/",
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': ' Token e68edf2d326bef3b12b3008c851261b0c7c3dced',
}
}
)
.then(response => response.json())
.then(data => this.setState({ userRole: data.role }))
}

如您所见,我正在为当前用户指定实际 token ,如管理页面所示:

enter image description here

(我知道从这张图片中您可能认为我在以管理员身份登录时尝试发出 GET 请求,因此 token 不正确,但我是实际上从 Django Rest Framework API 注销然后以用户 mirko 的身份重新登录。)

那么,有没有人知道我在这里做错了什么?

最佳答案

问题是我必须添加

DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
]

REST_FRAMEWORK 设置,指定 here

关于django - 从 django-rest-auth 获取 React 中的用户详细信息会返回 403(禁止访问)- 使用 authtoken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62592113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com