简体   繁体   English

我应该在Django Rest Framework中使用JWT或Basic Token身份验证吗?

[英]Should I use JWT or Basic Token authentication in Django Rest Framework?

I'm about to implement Token Authentication in my API using Django Rest Framework. 我即将使用Django Rest Framework在我的API中实现令牌认证。 But I'm not sure if I should use the basic token build-in DRF or use the JSON Web Token (JWT) standard (using this package djangorestframework-jwt ) The only reference that I found was in the DRF docs: 但我不确定是否应该使用基本令牌内置DRF或使用JSON Web令牌(JWT)标准 (使用此包djangorestframework-jwt )我发现的唯一参考是在DRF文档中:

Unlike the built-in TokenAuthentication scheme, JWT Authentication doesn't need to use a database to validate a token. 与内置TokenAuthentication方案不同,JWT Authentication不需要使用数据库来验证令牌。

Is there any other difference, advantages or disadvantages to consider? 是否还有其他差异,优点或缺点需要考虑?

Note: The API is gonna be accessed from the website (using angularjs) and by a mobile app 注意:API将从网站(使用angularjs)和移动应用程序访问

There are many benefits to using JWT tokens regardless of the platform. 无论平台如何,使用JWT令牌都有很多好处。 JWT tokens base64 encode all the users claims in their body and can be safely decoded on the client into a stateful object. JWT令牌base64对所有用户声明进行编码,可以在客户端上安全地解码为有状态对象。 This is hugely beneficial when compared to alternative opaque tokens which provide zero use to the client app. 与替代的不透明令牌相比,这是非常有益的,这些令牌对客户端应用程序没有任何用处。 On login, you immediately have atomic data in the client without additional round trips to the API to poll for user information. 在登录时,您立即在客户端中拥有原子数据,而无需额外往返API以轮询用户信息。

JWT tokens are stateless: there is no need to store or keep track of them server side, which is more scalable horizontally across many servers. JWT令牌是无状态的:不需要存储或跟踪它们的服务器端,这在许多服务器上可以横向扩展。 They are safe because the private signing key used to grant them is stored server side, any inbound API calls bearing them are simply validated with the private key, guaranteeing they were issued by your Authorization API. 它们是安全的,因为用于授予它们的私有签名密钥存储在服务器端,任何带有它们的入站API调用都只需使用私钥进行验证,从而保证它们是由Authorization API颁发的。

JWT tokens work nicely in Angular, React, and any other client framework. JWT令牌在Angular,React和任何其他客户端框架中都能很好地工作。 Because they are JSON, you can base64 decode them in the client and bind client UI elements directly to your claims - someone with an admin claim can see an admin menu and a user without that claim will never know the menu exists, if implemented correctly. 因为它们是JSON,所以您可以在客户端中对它们进行base64解码,并将客户端UI元素直接绑定到您的声明 - 具有管理员声明的人可以看到管理员菜单,如果没有该声明,用户将永远不会知道该菜单是否存在(如果正确实施)。

Aside from this, a JWT token still behaves in the same way as any bearer token: 除此之外,JWT令牌的行为与任何承载令牌的行为方式相同:

  • Issued by Authorization API 由授权API发布
  • Stored by client in cookies or local storage 客户端存储在cookie或本地存储中
  • Passed to Resource API in Authorization header 传递给Authorization标头中的Resource API

In summary, you will have fewer N+1 trips back and forth between your client and server as well as less work to scale if you implement JWT tokens. 总而言之,如果您实现JWT令牌,您在客户端和服务器之间来回的N + 1次数会减少,并且缩减的工作量也会减少。

JWT: 智威汤逊:

  1. Any client that has it can ask for stuff (similar to money when buying stuff) Any有它的客户都可以要求东西(类似于购买东西时的钱)
  2. No database lookup once issued - embedded expiry dictates validation 一旦发布就不会进行数据库查找 - 嵌入式到期指示验证

JWT has an expiry date and until that time, it will remain valid. JWT有一个到期日,在此之前,它将保持有效。 This may be undesirable when you need to log out a user on password reset, or forced. 当您需要在密码重置或强制时注销用户时,这可能是不合需要的。

A token blacklist may be used to address the above issues. 可以使用令牌黑名单来解决上述问题。 This will re-introduce persistent or in-memory tracking which JWT was trying to avoid in the first place. 这将重新引入JWT首先试图避免的持久性或内存中跟踪。 However, the tracking will be on selected keys ONLY, whereas, the Basic Token Auth, the tracking is for all users. 但是,跟踪仅在选定的键上,而基本令牌验证,跟踪适用于所有用户。

JWT can be decoded by anyone who has it. 拥有它的任何人都可以解码JWT。 Therefore one needs to be mindful of the information packed in the token. 因此,需要注意令牌中包含的信息。 The Basic Auth Token, on the other hand, is just a simple hash, which can be seen as just a reference to a user. 另一方面,Basic Auth Token只是一个简单的哈希,可以看作只是对用户的引用。

With caching and other performance enhancements in mind, one may not need to worry about the overhead, but the convenience and the future proofing of the flow. 考虑到缓存和其他性能增强,可能不需要担心开销,而是流程的便利性和未来的证明。

Having full control over authentication, authorization and invalidation is a good thing to have, no matter whether JWT + blacklist or Basic Token Auth is used. 无论是使用JWT +黑名单还是基本令牌身份验证,都可以完全控制身份验证,授权和失效。

Therefore, the Basic Auth Token may be better if the flow is customized to address the needs. 因此,如果定制流以满足需求,则基本身份验证令牌may会更好。

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

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