django_jwt는 장고(django)에서 JWT(Json Web Token)을 사용하는 방법에 대해서 정리한 저장소(Repository)입니다. 이 저장소(Repository)를 제작하면서 작성한 블로그가 있습니다. 자세한 내용은 아래에 링크를 통해 확인하시기 바랍니다.
아래에 명령어를 통해 django_jwt 저장소(Repository)를 복사(Clone)합니다.
git clone https://github.com/dev-yakuza/django_jwt.git아래에 명령어로 파이썬 가상 환경을 생성합니다.
virtualenv venv아래에 명령어로 파이썬 가상 환경을 실행합니다.
source venv/bin/activate아래에 명령어로 프로젝트에 필요한 모듈을 설치합니다.
pip install -r requirements.txt데이터베이스 연동을 위해 django_jwt/settings.py를 열고 아래의 내용을 자신의 DB에 맞게 수정합니다.
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_jwt', # DB name
'USER': 'root', # DB account
'PASSWORD': '', # DB account's password
'HOST': '127.0.0.1', # DB address(IP)
'PORT': '3306', # DB port(normally 3306)
}
}
...아래에 명령어로 데이터베이스를 생성합니다.
# python manage.py makemigrations
python manage.py migrate아래에 명령어로 장고(django) 관리자를 생성합니다.
python manage.py createsuperuser아래에 명령어로 준비한 데이터를 넣습니다.(Data Seed)
python manage.py loaddata blog/fixtures/posts-data.json아래에 명령어로 테스트서버를 실행합니다.
python manage.py runserverPostman을 실행 시키고 아래에 테스트 URL을 사용해 봅니다.
- JWT 토큰 발행
- URL: http://localhost:8000/api/token/
- Method: POST
- Param: username, password
- JWT 토큰 검증
- URL: http://localhost:8000/api/token/verify/
- Method: POST
- Param: token
- JWT 토큰 갱신
- URL: http://localhost:8000/api/token/refresh/
- Method: POST
- Param: token
- JWT 인증을 이용한 정보 조회
- URL: http://localhost:8000/api/blog/posts/
- Method: GET
- Header: Authorization jwt [jwt key]
자세한 테스트 방법은 아래의 블로그를 참고하시기 바랍니다.
django_jwtはジャンゴ(django)でJWT(Json Web Token)を使う方法について纏めたレポジトリ(Repository)です。このレポジトリ(Repository)を作る時作成したブログポストがあります。詳しく内容は下記のリンクを確認してください。
下記のコマンドでdjango_jwtレポジトリ(Repository)をコピー(Clone)します。
git clone https://github.com/dev-yakuza/django_jwt.git下記のコマンドでパイソン仮想環境を作ります。
virtualenv venv下記のコマンドでパイソンの仮想環境を実行します。
source venv/bin/activate下記のコマンドでプロジェクトに必要なモジュールをインストールします。
pip install -r requirements.txtデータベースを連動するため、django_jwt/settings.pyを開いて下記の内容を自分のDBに合わせて修正します。
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_jwt', # DB name
'USER': 'root', # DB account
'PASSWORD': '', # DB account's password
'HOST': '127.0.0.1', # DB address(IP)
'PORT': '3306', # DB port(normally 3306)
}
}
...下記のコマンドでデーターベースを生成します。
# python manage.py makemigrations
python manage.py migrate下記のコマンドでジャンゴ(django)の管理者を生成します。
python manage.py createsuperuser下記のコマンドで準備したデーターを入れます。(Data Seed)
python manage.py loaddata blog/fixtures/posts-data.json下記のコマンドでテストサーバーを起動します。
python manage.py runserverPostmanを実行して下記のテストURLを使ってみます。
- JWTトークン発行
- URL: http://localhost:8000/api/token/
- Method: POST
- Param: username, password
- JWTトークン検証
- URL: http://localhost:8000/api/token/verify/
- Method: POST
- Param: token
- JWTトークン更新
- URL: http://localhost:8000/api/token/refresh/
- Method: POST
- Param: token
- JWT認証を使った情報取得
- URL: http://localhost:8000/api/blog/posts/
- Method: GET
- Header: Authorization jwt [jwt key]
詳しくテスト方法は下記のブログを参考してください。
django_jwt is the repository about how to use JWT(Json Web Token) authentication in django. there are blog posts about this repository. if you want to know more details, see the link below.
execute the command below to clone the django_jwt repository.
git clone https://github.com/dev-yakuza/django_jwt.gitexecute the command below to start python virtual environment.
virtualenv venvexecute the command below to execute python virtual environment.
source venv/bin/activateexecute the command below to install modules for the project.
pip install -r requirements.txtyou need to modify django_jwt/settings.py to connect your database like below.
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_jwt', # DB name
'USER': 'root', # DB account
'PASSWORD': '', # DB account's password
'HOST': '127.0.0.1', # DB address(IP)
'PORT': '3306', # DB port(normally 3306)
}
}
...execute the command below to migrate.
# python manage.py makemigrations
python manage.py migrateexecute the command below to create django administrator.
python manage.py createsuperuserexecute the command below to insert data. (Data seed)
python manage.py loaddata blog/fixtures/posts-data.jsonexecute the command below to start django test server.
python manage.py runserveropen Postman and test links below.
- issue JWT token
- URL: http://localhost:8000/api/token/
- Method: POST
- Param: username, password
- verify JWT token
- URL: http://localhost:8000/api/token/verify/
- Method: POST
- Param: token
- refresh JWT token
- URL: http://localhost:8000/api/token/refresh/
- Method: POST
- Param: token
- get the data with JWT authentication
- URL: http://localhost:8000/api/blog/posts/
- Method: GET
- Header: Authorization jwt [jwt key]
you can see more details about test at the blog post below.