Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure JWT authentication in Django

2025-04-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to configure JWT authentication in Django. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

1. Install rest_framework + djangorestframework_simplejwt

Install djangorestframework_simplejwt: pip install djangorestframework-simplejwt

Install rest_framework:pip install djangorestframework

Djangorestframework_simplejwt is a django application that provides jwt.

two。 After configuring rest_framework, add the following to settings.py to support jwt authentication REST_FRAMEWORK = {'DEFAULT_AUTHENTICATION_CLASSES': [' rest_framework_simplejwt.authentication.JWTAuthentication',],} 3. Write a test viewfrom rest_framework import permissionsfrom rest_framework_simplejwt import authenticationclass TestView (views.APIView): permission_classes = [permissions.IsAuthenticated] authentication_classes = (authentication.JWTAuthentication,) def get (self, request, * args, * * kwargs): return Response ('ok') 4.urls.py imports jwt's two viewfrom rest_framework_simplejwt.views import (TokenObtainPairView, TokenRefreshView,)

Urlpatterns = [... Url (r'^ api/auth/token/obtain/$', TokenObtainPairView.as_view ()), # the content to be added url (r'^ api/auth/token/refresh/$', TokenRefreshView.as_view ()), # the content to be added url (r'^ api/test/$', TestView.as_view ()), # add the route to test views.] 5. Get Token

Start server first. The default port is 8000.

Method 1: through curl

Run curl in cmd to get token

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

The result returned token.

{"access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwidXNlcl9pZCI6MywianRpIjoiZDRlMTJiMjk0M2ZiNGFkYTg1NzZiNWIzMzcyY2RlMjQiLCJleHAiOjE1MzE1MDY5Njl9.S1MPTw359xVK-GpmJary1fZwDsHb8yXsVtyf-tCbHM8", "refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsInVzZXJfaWQiOjMsImp0aSI6IjAyYWM3NmQ0MDBkNzRlYzNhOGU5NDM2MWYzYzUzMWQyIiwiZXhwIjoxNTMxNTkzMDY5fQ.rXkYG2SJ74vof3rA38xX-EfMagHxeQRv7ZolszofuHA"} method 2: through the PostMan software

You only need to set up the following three places

Postman gets token6. Provide token, the way to get testview information 1: through curlcurl\ >-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwidXNlcl9pZCI6MywianRpIjoiZDRlMTJiMjk0M2ZiNGFkYTg1NzZiNWIzMzcyY2RlMjQiLCJleHAiOjE1MzE1MDY5Njl9.S1MPTw359xVK-GpmJary1fZwDsHb8yXsVtyf-tCbHM8"\ > http://localhost:8000/api/test/"ok" method 2: through PostMan software

Set the following places to see whether your interface is a Get request or a Post request, and set your own interface request method.

Token filled in the Token content obtained in the previous step.

After reading the above, do you have any further understanding of how to configure JWT authentication in Django? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report