【发布时间】:2016-06-08 19:13:49
【问题描述】:
我使用了以下自定义授权类
class CustomDjangoAuthorization(DjangoAuthorization):
def read_detail(self, object_list, bundle):
result = super(CustomDjangoAuthorization, self).read_detail(object_list, bundle)
# now we check here for specific permission
if bundle.request.user.profile.user_status:
raise Unauthorized("You are not allowed to access that resource.")
return result
它给了
401 未经授权
当user_status = 1。但是当我把user_status改成0时,它仍然显示
401 未经授权
错误。
我的未发送授权理解是,对于每个请求,tastepie 都会检查授权,并为 Ok 提供 200 响应,为 Unauthorized 提供 401 响应。我在这里遗漏了什么吗?
嘿 Sean,我尝试在 super 之前移动自定义代码。我得到一个
AttributeError: ‘AnonymousUser’对象没有属性‘profile’
在 localhost 中一切正常,生产出现问题。
这两种情况都会发生,user_status = 1 和user_status = 0
使用 Django 1.8 和 Tastypie 0.13.3。
【问题讨论】:
-
很有可能您对 super() 的调用由于某些未连接的原因返回 401,然后您将其返回给用户。可能想检查结果中的结果:)
-
啊,DjangoAuthorization 确实需要您登录,因此我认为是
‘AnonymousUser’ object has no attribute ‘profile’错误。