2016-09-23 77 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

看起来问题已经解决好,为你感到高兴:-) –