TIL(22.10.18) - Django SECRET KEY 분리하기

이지영·2022년 10월 18일
0

TIL/WIL

목록 보기
40/110

내일배움캠프 AI

오늘 한 일

사물인식 팀 프로젝트 장고부분 댓글CRUD



git에 장고 프로젝트를 올려두었는데 사진과 같은 내용의 메일이 왔다


django 공식문서
SECRET_KEY 란 특정 django 설치을 위한 비밀 키이며 이는 암호화 서명을 제공하는 데 사용되며 고유하고 예측할 수 없는 값으로 설정해야 한다. "라고 나와 있다. 즉, 암호화 인증에 사용되는 비밀키라는 말이다. - (보안 관리)


SECRET KEY 분리하기

  • secrets.json 파일 생성
{
	"SECRET_KEY": "django-insecure-x-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
  • settings.py 수정
import os, json
from django.core.exceptions import ImproperlyConfigured

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
secret_file = os.path.join(BASE_DIR, 'secrets.json')
with open(secret_file) as f:
    secrets = json.loads(f.read())

def get_secret(setting):
    try:
        return secrets[setting]
    except KeyError:
        error_msg = "Set the {} environment variable".format(setting)
        raise ImproperlyConfigured(error_msg)

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_secret("SECRET_KEY")
  • .gitignore에 secrets.json 추가
## Django Secret
secrets.json
profile
🐶🦶📏

0개의 댓글