Django

파이썬 장고 실무 심화 3주차_JWT 토큰 로컬스토리지

끈끈 2023. 4. 21. 02:28

 

https://django-rest-framework-simplejwt.readthedocs.io/en/latest/getting_started.html#installation

 

Getting started — Simple JWT 5.2.2.post16+gf298efa documentation

Cryptographic Dependencies (Optional) If you are planning on encoding or decoding tokens using certain digital signature algorithms (i.e. RSA and ECDSA; visit PyJWT for other algorithms), you will need to install the cryptography library. This can be insta

django-rest-framework-simplejwt.readthedocs.io

 

새 프로젝트 만들기

python -m venv venv
source venv/Scripts/Activate
pip install django djangorestframework djangorestframework-simplejwt
pip freeze > requirementx.txt
django-admin startproject drf_project .
.gitignore
git init
git remote add origin < >
git add .
git commit -m " "
git push origin main

 

drf_project > settings.py:

INSTALLED_APPS = [
    'rest_framework',
    'rest_framework_simplejwt',
    'users',
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    )
}

 

drf_project > urls.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('users/', include('users.urls')),
]

 

users > urls.py:

from django.urls import path
from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)

urlpatterns = [
    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]

 

Postman

 

https://jwt.io/

 

JWT.IO

JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.

jwt.io

 

JWT 시크릿키

 


 

models.py / admin.py

 

https://docs.djangoproject.com/en/4.2/topics/auth/customizing/

 

Django

The web framework for perfectionists with deadlines.

docs.djangoproject.com

 

Hashed password

 

users > serializer.py:

from rest_framework import serializers
from users.models import User


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = "__all__"

    def create(self, validated_data):
        user = super().create(validated_data)
        password = user.password
        user.set_password(password)
        user.save()
        return user

 


 

Customizing token

 

JWT

 

https://django-rest-framework-simplejwt.readthedocs.io/en/latest/customizing_token_claims.html

 

Customizing token claims — Simple JWT 5.2.2.post16+gf298efa documentation

Customizing token claims If you wish to customize the claims contained in web tokens which are generated by the TokenObtainPairView and TokenObtainSlidingView views, create a subclass for the desired view as well as a subclass for its corresponding seriali

django-rest-framework-simplejwt.readthedocs.io

 


 

access 토큰

 

 

refresh 토큰으로 access 토큰 재발급

 

 


 

cors-headers

 

 

pip install django-cors-headers
pip freeze > requirements.txt

 

drf_project > settings.py:

INSTALLED_APPS = [
    "corsheaders",
]

MIDDLEWARE = [
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware", # 낮아도 얘보다는 위에
]

CORS_ALLOW_ALL_ORIGINS = True

 

https://pypi.org/project/django-cors-headers/

 

django-cors-headers

django-cors-headers is a Django application for handling the server headers required for Cross-Origin Resource Sharing (CORS).

pypi.org

 


 

로컬스토리지

 

로컬 스토리지

 

https://www.w3schools.com/jsref/prop_win_localstorage.asp

 

Window localStorage Property

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com