SoFunction
Updated on 2024-11-15

Detailed explanation of Django configuration JWT authentication method

1. install rest_framework + djangorestframework_simplejwt

Install djangorestframework_simplejwt :pip install djangorestframework-simplejwt

Install rest_framework:pip install djangorestframework

djangorestframework_simplejwt is the django application that provides jwt.

2. After configuring rest_framework, add the following to support jwt authentication

REST_FRAMEWORK = {
 'DEFAULT_AUTHENTICATION_CLASSES': [
  'rest_framework_simplejwt.',
 ],
}

3. Write a test view

from rest_framework import permissions
from rest_framework_simplejwt import authentication
class TestView():
 permission_classes = []
 authentication_classes = (,)
 def get(self, request, *args, **kwargs):
  return Response('ok')

Import the two views of jwt

from rest_framework_simplejwt.views import (
 TokenObtainPairView, 
 TokenRefreshView,  
)

urlpatterns = [
 ...
 url(r'^api/auth/token/obtain/$', TokenObtainPairView.as_view()), # What needs to be added
 url(r'^api/auth/token/refresh/$', TokenRefreshView.as_view()), # What needs to be added
 url(r'^api/test/$', TestView.as_view()), # Add routes for testviews
 ...
]

5. Obtaining a Token

Start the server with the default port of 8000.

Way 1: via curl

Run curl in cmd to get the token

curl -X POST -H "Content-Type: application/json" -d '{"username": "abab", "password": "abab123456"}' http://localhost:8000/api/auth/token/obtain/

The result returns the token

{"access":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwidXNlcl9pZCI6MywianRpIjoiZDRlMTJiMjk0M2ZiNGFkYTg1NzZiNWIzMzcyY2RlMjQiLCJleHAiOjE1MzE1MDY5Njl9.S1MPTw359xVK-GpmJary1fZwDsHb8yXsVtyf-tCbHM8","refresh":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsInVzZXJfaWQiOjMsImp0aSI6IjAyYWM3NmQ0MDBkNzRlYzNhOGU5NDM2MWYzYzUzMWQyIiwiZXhwIjoxNTMxNTkzMDY5fQ.rXkYG2SJ74vof3rA38xX-EfMagHxeQRv7ZolszofuHA"}

Mode 2: Through PostMan software

Just set up the following three places

6. provide a token to get information about the testview

Way 1: via curl

curl \
> -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwidXNlcl9pZCI6MywianRpIjoiZDRlMTJiMjk0M2ZiNGFkYTg1NzZiNWIzMzcyY2RlMjQiLCJleHAiOjE1MzE1MDY5Njl9.S1MPTw359xVK-GpmJary1fZwDsHb8yXsVtyf-tCbHM8" \
> http://localhost:8000/api/test/
"ok"

Mode 2: Through PostMan software

Set the following places, depending on whether your interface is a Get request or a Post request, set the interface request method yourself.

Token is the content of the Token obtained in the previous step.

The default Token expiration time is 5 minutes, you can modify the expiration time through the settings, the setting method and detailed settings document, go to Bigyoung small station () to view advanced content.

This article on Django configuration JWT authentication method is introduced to this article, more related Django configuration JWT authentication method content please search my previous posts or continue to browse the following related articles I hope that you will support me in the future more!