์ตœ๋Œ€ 1 ๋ถ„ ์†Œ์š”

๋ฏธ๋“ค์›จ์–ด๊ฐ€ ๋ญ˜๊นŒ?

Django์˜ MIDDLEWARE๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด Django ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ–ˆ๋‹ค.

๋ฏธ๋“ค์›จ์–ด ๋Š” Django์˜ ์š”์ฒญ/์‘๋‹ต ์ฒ˜๋ฆฌ์— ๋Œ€ํ•œ ํ›„ํฌ ํ”„๋ ˆ์ž„์›Œํฌ ์ด๋‹ค.

๋˜ํ•œ, Django์˜ ์ž…๋ ฅ/์ถœ๋ ฅ์„ ์ „์—ญ์ ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ€๋ณ๊ณ  ์ €์ˆ˜์ค€์˜ ํ”Œ๋Ÿฌ๊ทธ์ธ ์‹œ์Šคํ…œ์ด๋‹ค.

๊ฐ ๋ฏธ๋“ค์›จ์–ด์˜ ๊ตฌ์„ฑ ์š”์†Œ๋Š” ํŠน์ • ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชฉ์ ์„ ๊ฐ–๊ณ  ์žˆ๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด Django์˜ AuthenticationMiddleware ์„ธ์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž๋ฅผ ์š”์ฒญ๊ณผ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฏธ๋“ค์›จ์–ด ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.

์ฆ‰, ๋ฏธ๋“ค์›จ์–ด๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์„œ๋กœ ํ†ต์‹ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด์ธ ๊ฒƒ์ด๋‹ค.

Django MIDDLEWARE ํ™œ์„ฑํ™”

๋ฏธ๋“ค์›จ์–ด ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด Django์˜ settings.py์—์„œย [MIDDLEWARE](https://docs.djangoproject.com/en/5.1/ref/settings/#std-setting-MIDDLEWARE) ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

ย [MIDDLEWARE](https://docs.djangoproject.com/en/5.1/ref/settings/#std-setting-MIDDLEWARE) ์—์„œ ๋ฏธ๋“ค์›จ์–ด ๊ตฌ์„ฑ ์š”์†Œ๋Š” ๋ฌธ์ž์—ด(๋ฏธ๋“ค์›จ์–ด ํŒฉํ† ๋ฆฌ์˜ ํด๋ž˜์Šค ๋˜๋Š” ํ•จ์ˆ˜ ์ด๋ฆ„์— ๋Œ€ํ•œ ์ „์ฒด Python ๊ฒฝ๋กœ)๋กœ ํ‘œํ˜„๋œ๋‹ค.

django-admin startproject ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑํ•œ ํ”„๋กœ์ ํŠธ์˜ ๊ธฐ๋ณธ MIDDLEWARE ๋ชฉ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

Django๋ฅผ ์„ค์น˜ํ•  ๋•Œ๋Š” ์‚ฌ์‹ค ๋ฏธ๋“ค์›จ์–ด๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์•„, MIDDLEWARE๊ฐ€ ๋น„์–ด์žˆ์–ด๋„ ๋œ๋‹ค.

ํ•˜์ง€๋งŒ, ์ตœ์†Œํ•œ CommonMiddleware๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ฐ•๋ ฅํžˆ ๊ถŒ์žฅํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

๋ฏธ๋“ค์›จ์–ด๋Š” ๋‹ค๋ฅธ ๋ฏธ๋“ค์›จ์–ด์— ์ข…์†๋  ์ˆ˜ ์žˆ์–ด, ๋ฏธ๋“ค์›จ์–ด์˜ ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค!

์˜ˆ๋ฅผ ๋“ค๋ฉด AuthenticationMiddleware ๋Š” ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž๋ฅผ ์„ธ์…˜์— ์ €์žฅํ•˜๋ฏ€๋กœ SessionMiddleware ๋‹ค์Œ์— ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค.

default MIDDLEWARE ํŒŒํ—ค์น˜๊ธฐ!

์ฐธ๊ณ  ์ž๋ฃŒ

[Middleware Django documentation](https://docs.djangoproject.com/en/5.1/topics/http/middleware/)

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ