简体   繁体   English

在 Django(drf 和 simplejwt)中究竟应该如何实现基于 JWT 的身份验证?

[英]How exactly should JWT-based authentication be implemented in Django (drf and simplejwt)?

I am struggling to understand exactly how JWT-based authentication should be implemented in Django (I am using simplejwt).我正在努力了解应该如何在 Django 中实现基于 JWT 的身份验证(我使用的是 simplejwt)。 I am just a beginner, so please brace yourselves for some silly questions.我只是一个初学者,所以请为一些愚蠢的问题做好准备。 The rest-framework-simplejwt documentation is very minimal and does not provide enough detail for a newbie like me. rest-framework-simplejwt 文档非常少,并没有为像我这样的新手提供足够的细节。

path('token/obtain', jwt_views.TokenObtainPairView.as_view(), name='token_create'),
path('token/refresh', jwt_views.TokenRefreshView.as_view(), name='token_refresh'),

So, I've created the paths in my urls.py as suggested by the official documentation.因此,我按照官方文档的建议在 urls.py 中创建了路径。 Where do I go from here?我在哪里 go 从这里? I guess my confusion comes from the fact that I am not sure where exactly in the code I have to issue my tokens.我想我的困惑来自这样一个事实,即我不确定我必须在代码中的确切位置发布我的令牌。 Let's say, I am logging in the user.假设我正在登录用户。 So, in order to obtain the token, do I have to send a request to the 'token_create' endpoint from inside my view?那么,为了获得令牌,我是否必须从视图内部向“token_create”端点发送请求? Or do I have to somehow indicate it in one of my serializers?还是我必须以某种方式在我的序列化程序之一中指出它? What about the 'refresh_token' endpoint? 'refresh_token' 端点呢? Is there a specific method that I need to use?我需要使用特定的方法吗?

Then, what do I do with the token once it has been issued?那么,令牌发行后我该怎么办? Clearly, I shouldn't save it in the database since it defeats the entire purpose of using JWTs in the first place.显然,我不应该将它保存在数据库中,因为它首先破坏了使用 JWT 的全部目的。 From my understanding, I should attach it to the headers so that the subsequent requests by the user contain the tokens in the headers.据我了解,我应该将其附加到标头,以便用户的后续请求包含标头中的令牌。

The frontend will be written in ReactJS and will be on a separate server from my Django backend API, and the communication between the two will be configured through CORS. The frontend will be written in ReactJS and will be on a separate server from my Django backend API, and the communication between the two will be configured through CORS.

In this case, how do I attach the token to the headers and make it so that the user's browser sends in the token with each request?在这种情况下,如何将令牌附加到标头并使其使用户的浏览器在每个请求中发送令牌? Is there some sort of package that could be useful for that?是否有某种 package 可能对此有用?

I think you just mixed everything up, I'm gonna explain everything however you may already know some stuff.我想你只是把一切都搞混了,我会解释一切,但你可能已经知道一些东西。

JWT simply is a way to authorize users, you usually create an endpoint to create a token for the users, this endpoint can be named login , create_token , 'generate_token', or anything! JWT 只是一种授权用户的方式,您通常创建一个端点来为用户创建一个令牌,这个端点可以命名为logincreate_token 、 'generate_token' 或任何东西! doesn't really matter!真的没关系!

However maybe if u use a specific library maybe it forces you to use a specific endpoint but in Flask it's really what you like.但是,如果您使用特定的库,可能会迫使您使用特定的端点,但在Flask中,这确实是您喜欢的。

This login (whatever you call it) endpoint will take a username and password and checks if it exists and it's correct, then generates a JWT with a library like PyJWT , You can configure the JWT to be expired in for example 20 mins or more, then you encrypt a dictionary (JSON?) which usually contains user_id which you query from the database.这个login (不管你怎么称呼它)端点将接受用户名和密码并检查它是否存在并且它是正确的,然后生成一个JWT和一个像PyJWT这样的库,你可以配置 JWT 在例如 20 分钟或更长时间内过期,然后你加密一个dictionary (JSON?),它通常包含你从数据库中查询的user_id example of the JSON you provide to the user with:您向用户提供的 JSON 示例:

{
  "user_id": something,
  "role": something,
  ...
}

Then it will be encrypted to a long string.然后它将被加密为一个长字符串。

now when the user sends a request, he/she needs to have that long string as the Authorization header of the request.现在当用户发送请求时,他/她需要有那个长字符串作为请求的Authorization header。

In postman --> Authorizations , choose Bearer Authorization and then insert that long string.postman --> Authorizations中,选择Bearer Authorization然后插入那个长字符串。

We also give the user a refresh_token .我们还给用户一个refresh_token

This is the example of the JSON you provide the user with when he/she calls the login endpoint:这是您在用户调用login端点时向用户提供的 JSON 示例:

{
 token: some_long_string,
 refresh_token: some_long_string,

}

So what is refresh Token?那么什么是刷新Token呢?

it's just the token that when the main token expires instead of making the user enter username and password again, he just sends the refresh token we gave him while he called login .只是当主令牌过期而不是让用户再次输入用户名和密码时,他只是发送了我们在他调用login时给他的刷新令牌。

One more point: This was the whole flow and logic you need to implement.还有一点:这是您需要实现的整个流程和逻辑。 Do it as you like, libraries or anything you like, doesn't really matter.随心所欲,图书馆或任何你喜欢的东西,并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM