Explorar o código

增加认证失败交验

yf_yzl %!s(int64=2) %!d(string=hai) anos
pai
achega
5da28195e3
Modificáronse 2 ficheiros con 13 adicións e 1 borrados
  1. 3 1
      kedong/settings.py
  2. 10 0
      smartfarming/permissions/author_error.py

+ 3 - 1
kedong/settings.py

@@ -35,9 +35,11 @@ MIDDLEWARE = [
 
 
 REST_FRAMEWORK = {
+    'EXCEPTION_HANDLER': ('smartfarming.permissions.author_error.custom_exception_handler'),    
     'DEFAULT_PERMISSION_CLASSES': (
         'smartfarming.permissions.smking.LoginPermission',
-    )
+    ),
+
 }
 
 CORS_ORIGIN_WHITELIST = ()

+ 10 - 0
smartfarming/permissions/author_error.py

@@ -0,0 +1,10 @@
+# exceptions.py
+
+from rest_framework.exceptions import NotAuthenticated
+from rest_framework.views import exception_handler
+from rest_framework.response import Response
+
+def custom_exception_handler(exc, context):
+    if isinstance(exc, NotAuthenticated):
+        return Response({'msg': '无认证信息或认证失败', "code": "403"}, status=403)
+    return exception_handler(exc, context)  # 使用默认的异常处理器处理其他异常