TokenAuthentication
Django 나머지 프레임 워크에서 구현하려고합니다 . 서버에서 토큰 키를 얻을 수 있습니다.
$curl -d "username=admin&password=admin" http://localhost:8000/api-token-auth/
{"token":"a571a596eab2e4063da4b9250a05b570ba8e9786"}
하지만 어떤 api 와도 사용할 수 없습니다 .
curl -X GET http://localhost:8000/cart/ -H "Authorization: Token a571a596eab2e4063da4b9250a05b570ba8e9786"
{"detail":"Authentication credentials were not provided."}
내 설정 파일,
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'myapp',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
#rest framework
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': (
'rest_framework.filters.DjangoFilterBackend',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
'rest_framework.permissions.DjangoObjectPermissions',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
}
TokenAuthentication 백엔드가 누락되었습니다.
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
rest_framework.authentication.TokenAuthentication
줄을 추가하십시오 . 인증 백엔드가 순차적으로 테스트되므로 순서가 중요합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다