2016-09-23 36 views
-1

我在django-rest-framekwork中的缺省認證中添加了一個Authencation,並且在api-guide中記錄錯誤時引發AuthenticationFailed。它返回一個500響應,表示AuthenticationFailed的異常被拋出,而根據document它應該返回401或403響應。django-rest-framework raise AuthencationFailed cause 500響應

我檢查了來源。它捕獲這個異常並在Request._authenticate()中重新提出,並且永遠不會再捕獲。

我錯過了什麼?

我使用Django 1.7和django-rest-framework 3.3.3。

的代碼,你可以想像那麼簡單:

class CustomAuthentication(authentication.BaseAuthentication): 
    def authenticate(self, request): 
     raise exceptions.AuthenticationFailed 

,並設置:

REST_FRAMEWORK = { 
    'DEFAULT_AUTHENTICATION_CLASSES': [ 
     'rest_framework.authentication.BasicAuthentication', 
     'rest_framework.authentication.SessionAuthentication', 
     'path.to.CustomAuthentication', 
    ], 
} 
+0

請提供一些代碼,提供一些關於您如何進行身份驗證以及可能會出錯的提示。 –

+0

這不關我怎麼認證。我只需要401或403響應而不是500響應。 –

+0

感謝您更新問題,您是否也可以分享您正在驗證的視圖? –

回答

0
def initialize_request(self, request, *args, **kwargs): 
    request = super(TheViewSet, self).initialize_request(
     request, *args, **kwargs) 
    return request 

替換:

def initialize_request(self, request, *args, **kwargs): 
    _request = super(TheViewSet, self).initialize_request(
     request, *args, **kwargs) 
    return _request 

對不起延遲。我得到了一個假期,並且這幾天有一次旅行。我仍然不知道它是如何產生問題的。

+0

看起來問題已經解決好,爲你感到高興:-) –