Token認証とJWT認証
APIクライアントを安全に認証します
「Token認証とJWT認証」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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. 🎉
よくある質問
「Token認証とJWT認証」レッスンは無料ですか?
はい。「Token認証とJWT認証」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「Token認証とJWT認証」で何を学びますか?
APIクライアントを安全に認証します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「Token認証とJWT認証」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。