我使用Django Rest Framework创建了REST API,并使用django-rest-auth作为auth端点。这些API用于移动应用程序。我已经使用TokenAuthentication来保护API。
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
使用APIDOC时出现问题。我已经使用coreapi添加了apidoc。文档也需要得到保护。当我使用上述设置访问/ docs /时,我得到以下错误:
'dict' object has no attribute 'data'
所以我启用了SessionAuthentication。
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
这导致登录端点(/ rest-auth / login)因CSRF错误而中断。
{
"detail": "CSRF Failed: CSRF token missing or incorrect."
}
那么如何使用TokenAuth和SessionAuth文档单独保护端点?或者我可以完全绕过登录端点的安全性吗?
答案 0 :(得分:0)
在定义URL时,可以单独定义身份验证类:
url(r'^docs/', include_docs_urls(title='PeySO API Doc', public=False,
authentication_classes=[SessionAuthentication])),
这允许我们仅为文档启用SessionAuthentication,并为API端点使用TokenAuthentication。