0

所以我已经安装了 djanogo rest framework JWT 并设置了设置和认证类。根据本指南。我将省略设置,因为它们是正确的,这不是问题所在。也不要发布太多代码

https://jpadilla.github.io/django-rest-framework-jwt/

然后我从前端调用服务器上的授权视图

 let token = "hardcoded token just to get the service working"; 
    if(token != null){
      this.authservice.authorizetoken(token)
        .subscribe(
          (req: any)=>{
            console.log(req);
          }
        );

// grab the permissions a user has and who they are by token
    authorizetoken(token){
      return this.http.get(userauthorization, {
        headers: new HttpHeaders().set('Authorization', 'JWT' + token )
      });
    }

然后在我的 django 中是查看代码:

class UserAuthorization(APIView):
    authentication_classes = (JSONWebTokenAuthentication,)
    def get(self, request, *args, **kwargs):
        print(request.user)
        return Response({})

但我不断收到匿名用户返回。它不应该是一个用户对象,因为我在标题中传递了一个令牌?

我不知道我做错了什么。

4

1 回答 1

0

根据文档,标题应该是 format Authorization: JWT <your_token>。当您在authorizetoken函数内的标头中设置令牌时,您在'JWT'+ token. 这可能是未对用户进行身份验证的问题。您是否尝试过使用 Postman 的端点?

于 2018-05-08T06:21:25.410 回答