令牌与 JWT 认证
安全地认证 API 客户端
令牌与 JWT 认证 是 CoddyKit 上的免费 Django Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
APIs Need Stateless Auth
Browsers use sessions, but API clients often have no cookies. Token authentication lets a client prove who it is on every request instead.
How a Token Works
The user logs in once and gets a token string. They send it with each request, and DRF maps it back to the right user.
Enabling TokenAuthentication
Add the authtoken app and turn on TokenAuthentication so DRF knows to read tokens from incoming requests.
INSTALLED_APPS += ['rest_framework.authtoken']
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
],
}Issuing a Token
DRF gives you a built-in obtain_auth_token view. Post a username and password to it, and it returns that user's token.
from rest_framework.authtoken.views import obtain_auth_token
urlpatterns = [
path('api-token-auth/', obtain_auth_token),
]Sending the Token
The client puts the token in the Authorization header, prefixed with the word Token, on every protected call. 🔑
Authorization: Token 9944b09199c62bcf9418ad846dd0e4The Limit of Simple Tokens
A DRF token never expires and is just a database lookup. For larger systems, a JWT adds expiry and self-contained data.
What a JWT Carries
A JSON Web Token is a signed string holding claims like the user id and an expiry time. The server trusts it without a DB hit.
Adding SimpleJWT
Install djangorestframework-simplejwt and register its authentication class to swap tokens for JWTs.
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}Access and Refresh Tokens
SimpleJWT gives two tokens: a short-lived access token for requests and a longer refresh token to get a new access token.
JWT Login Endpoints
Wire up TokenObtainPairView to log in and TokenRefreshView to renew, and your JWT flow is ready.
from rest_framework_simplejwt.views import (
TokenObtainPairView, TokenRefreshView)
urlpatterns = [
path('token/', TokenObtainPairView.as_view()),
path('token/refresh/', TokenRefreshView.as_view()),
]Sending a Bearer Token
JWT clients use the Bearer scheme in the Authorization header instead of the Token word DRF tokens use. 🎟️
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9Quick Check
One key difference separates DRF tokens from JWTs.
Recap: Proving Identity
You learned to authenticate API clients with DRF tokens for simplicity and JWTs for expiry and scale, both sent in the Authorization header. 🎉
常见问题解答
「令牌与 JWT 认证」课时是免费的吗?
是的 — 「令牌与 JWT 认证」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。
「令牌与 JWT 认证」这节课中我会学到什么?
安全地认证 API 客户端 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Django Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「令牌与 JWT 认证」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Django Academy 课中编写并运行代码吗?
能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- ModelViewSet 与路由器
- 权限与限流
- 令牌与 JWT 认证
- 过滤、搜索与分页