对于下面的一段代码,我想返回一个布尔值,对应于用户是否被认证。在Django rest框架中对无效令牌认证的自定义响应
class UserAuthenticatedView(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (AllowAny,)
def get(self, request, format=None):
is_authenticated = request.user.is_authenticated()
resp = {'is_authenticated': is_authenticated}
return Response(resp, content_type="application/json", status=status.HTTP_200_OK)
然而,对于无效令牌,控制甚至不是不打算里面get
方法由于这我不能够自定义的响应。在这种情况下,我收到了回复:{'detail': 'invalid token'}
, 有关如何自定义无效令牌响应的想法?
我想你应该创建一个自定义TokenAuthentication类。 – Gocht