让 django rest 框架 jwt 身份验证工作 [django rest, angular 5]
Getting django rest framework jwt authentication to work [django rest, angular 5]
所以我安装了 djanogo rest framework JWT 并设置了设置和身份验证 类。根据本指南。我将省略设置,因为它们是正确的,这不是问题所在。也是为了不要post代码太多
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({})
但我一直收到 anonymousUser 返回。它不应该是用户 object 因为我在 header 中传递了一个令牌吗?
我不知道我做错了什么。
根据文档,headers 的格式应为 Authorization: JWT <your_token>
。当您在 authorizetoken
函数内的 headers 中设置令牌时,您在 'JWT'+ token
之间缺少 white-space。这可能是不对用户进行身份验证的问题。您是否尝试过 Postman 的端点?
所以我安装了 djanogo rest framework JWT 并设置了设置和身份验证 类。根据本指南。我将省略设置,因为它们是正确的,这不是问题所在。也是为了不要post代码太多
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({})
但我一直收到 anonymousUser 返回。它不应该是用户 object 因为我在 header 中传递了一个令牌吗?
我不知道我做错了什么。
根据文档,headers 的格式应为 Authorization: JWT <your_token>
。当您在 authorizetoken
函数内的 headers 中设置令牌时,您在 'JWT'+ token
之间缺少 white-space。这可能是不对用户进行身份验证的问题。您是否尝试过 Postman 的端点?