如何在 django-rest-framework 中扩展 JWTAuthentication
How to extend JWTAuthentication in django- rest-framework
我创建了 CustomAuthentication class,此 class 扩展了 JWTAuthentication class。我把这个 class 放在文件 auth.py 和 settings.py 相同的位置。
在settings.py我修改:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
}
至
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
auth.CustomAuthentication',
),
}
但是它不起作用,它抛出
"Could not import 'auth.CustomAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: Module "auth" 没有定义 "CustomAuthentication" attribute/class."
这里是auth.py的内容:
from rest_framework_simplejwt.authentication import JWTAuthentication
class CustomJWTAuthentication(JWTAuthentication):
is_valid = True
这里有什么问题?请帮忙
非常感谢。
class 名称在 auth.py 中是 'CustomJWTAuthentication',而您在 settings.py 中使用的是 'CustomAuthentication',请将其更改为相同。
我创建了 CustomAuthentication class,此 class 扩展了 JWTAuthentication class。我把这个 class 放在文件 auth.py 和 settings.py 相同的位置。
在settings.py我修改:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
}
至
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
auth.CustomAuthentication',
),
}
但是它不起作用,它抛出
"Could not import 'auth.CustomAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: Module "auth" 没有定义 "CustomAuthentication" attribute/class."
这里是auth.py的内容:
from rest_framework_simplejwt.authentication import JWTAuthentication
class CustomJWTAuthentication(JWTAuthentication):
is_valid = True
这里有什么问题?请帮忙
非常感谢。
class 名称在 auth.py 中是 'CustomJWTAuthentication',而您在 settings.py 中使用的是 'CustomAuthentication',请将其更改为相同。