Django視圖層——內建的中間件類
參考文獻:
https://docs.djangoproject.com/zh-hans/2.0/ref/middleware/
可用的中間件
緩存類中間件(cache middleware)
class UpdateCacheMiddleware
class FetchFromCacheMiddleware
啟用站點範圍的緩存。 如果啟用了這些,只要CACHE_MIDDLEWARE_SECONDS設置定義,每個Django支持的頁面都將被緩存。 請參閱緩存文檔
- 通用類中間件(common middleware)
class CommonMiddleware
添加了一些方便的操作:
- 禁止在DISALLOWED_USER_AGENTS設置中訪問用戶代理,該設置應該是已編譯的正則表達式對象的列表。
- 根據APPEND_SLASH和PREPEND_WWW設置執行URL重寫。
如果APPEND_SLASH為True並且初始URL未以斜杠結尾,並且在URLconf中找不到,則通過在末尾附加斜杠來形成新URL。 如果在URLconf中找到此新URL,則Django會將請求重定向到此新URL。 否則,照常處理初始URL
例如,如果您沒有foo.com/bar的有效URL模式但foo.com/bar/的格式有效,則foo.com/bar將被重定向到foo.com/bar/。
如果PREPEND_WWW為True,則缺少前導“www”的網址。 將被重定向到具有前導“www”的相同URL。
這兩個選項都用於規範化URL。 理念是每個URL應該只存在於一個地方。 從技術上講,URL foo.com/bar與foo.com/bar/不同 - 搜索引擎索引器會將它們視為單獨的URL - 因此最佳做法是規範化URL。
- 根據USE_ETAGS設置處理ETag。 如果USE_ETAGS設置為True,Django將通過對頁面內容進行MD5散列計算每個請求的ETag,並且如果合適,它將負責發送Not Modified響應。
- 為非流式響應設置Content-Length標頭。
GZip中間件
class GZipMiddleware
警告:
安全研究人員最近透露,當在網站上使用壓縮技術(包括GZipMiddleware)時,該網站可能會受到一些可能的攻擊。在您的網站上使用GZipMiddleware之前,您應該非常仔細地考慮您是否受到這些攻擊。
如果您對是否受到影響有任何疑問,則應避免使用GZipMiddleware。
應將此中間件放在需要讀取或寫入響應主體的任何其他中間件之前,以便之後進行壓縮。
以下條件如果為真,那麽它將不會執行壓縮:
- 內容主體長度小於200個字節。
- 響應已設置Content-Encoding標頭。
- 請求(瀏覽器)尚未發送包含gzip的Accept-Encoding標頭。
如果響應具有ETag標頭,則ETag變弱以符合RFC 7232#section-2.1。
您可以使用gzip_page()裝飾器將GZip壓縮應用於各個視圖。
Conditional GET中間件
class ConditionalGetMiddleware
處理條件GET操作。 如果響應沒有ETag標頭,則中間件會在需要時添加一個。 如果響應具有ETag或Last-Modified標頭,並且請求具有If-None-Match或If-Modified-Since,則響應將被HttpResponseNotModified替換。
Locale中間件
class LocaleMiddleware
根據請求中的數據啟用語言選擇。 它為每個用戶定制內容。
LocaleMiddleware.response_redirect_class
默認會到HttpResponseRedirect。子類LocaleMiddleware並覆蓋該屬性以自定義中間件發出的重定向
Message中間件
class MessageMiddleware
允許基於cookie和session的消息的支持。
Security中間件
class SecurityMiddleware
註意:如果您的部署情況允許,通常最好讓您的前端Web服務器執行SecurityMiddleware提供的功能。 這樣,如果存在Django不提供的請求(例如靜態媒體或用戶上傳的文件),它們將具有與對Django應用程序的請求相同的保護。
這個django.middleware.security.SecurityMiddleware中間件對request/response提供了一些安全增強措施。每一個都可以單獨打開或關閉,通過下面的設置。
SECURE_BROWSER_XSS_FILTER
SECURE_CONTENT_TYPE_NOSNIFF
SECURE_HSTS_INCLUDE_SUBDOMAINS
SECURE_HSTS_PRELOAD
SECURE_HSTS_SECONDS
SECURE_REDIRECT_EXEMPT
SECURE_SSL_HOST
SECURE_SSL_REDIRECT
HTTP Strict Transport Security
對於只應通過HTTPS訪問的站點,您可以通過設置“Strict-Transport-Security”標頭,指示現代瀏覽器拒絕通過不安全的連接(在給定的時間段內)連接到您的域名。 這可以減少您在某些SSL剝離中間人(MITM)攻擊中的風險。
X-Content-Type-Options: nosniff
X-XSS-Pro tection: 1: mode=block
SSL Redirect
如果您的站點同時提供HTTP和HTTPS連接,則大多數用戶默認情況下最終會使用不安全的連接。 為了獲得最佳安全性,您應該將所有HTTP連接重定向到HTTPS。
如果將SECURE_SSL_REDIRECT設置為True,SecurityMiddleware將永久(HTTP 301)將所有HTTP連接重定向到HTTPS。
Session中間件
class SessionMiddleware
打開session支持。
Site中間件
class CurrentSiteMiddleware
將表示當前站點的站點屬性添加到每個傳入的HttpRequest對象
Authentication中間件
class AuthenticationMiddleware
對每一個HttpRequest請求對象添加user屬性,代表了當前的登錄用戶。
class RemoteUserMiddleware
使用Web服務器提供的驗證的中間件。
class PersistentRemoteUserMiddleware
僅在登錄頁面使用Web服務器提供的驗證的中間件。
CSRF protection中間件
- class CsrfViewMiddleware
- 通過向POST表單添加隱藏的表單字段並檢查正確值的請求,添加對跨站點請求偽造的保護
X-Frame-Options中間件
class XFrameOptionsMiddleware
通過X-Frame-Options標頭進行簡單的點擊劫持保護。
中間件的順序
- 下面是一些關於應用這些中間件的順序的建議:
-
SecurityMiddleware
It should go near the top of the list if you‘re going to turn on the SSL redirect as that avoids running through a bunch of other unnecessary middleware.
-
UpdateCacheMiddleware
Before those that modify the
Vary
header (SessionMiddleware
,GZipMiddleware
,LocaleMiddleware
). -
GZipMiddleware
Before any middleware that may change or use the response body.
After
UpdateCacheMiddleware
: ModifiesVary
header. -
ConditionalGetMiddleware
Before
CommonMiddleware
: uses itsETag
header whenUSE_ETAGS
=True
. -
SessionMiddleware
After
UpdateCacheMiddleware
: ModifiesVary
header. -
LocaleMiddleware
One of the topmost, after
SessionMiddleware
(uses session data) andUpdateCacheMiddleware
(modifiesVary
header). -
CommonMiddleware
Before any middleware that may change the response (it sets the
ETag
andContent-Length
headers). A middleware that appears beforeCommonMiddleware
and changes the response must reset the headers.After
GZipMiddleware
so it won‘t calculate anETag
header on gzipped contents.Close to the top: it redirects when
APPEND_SLASH
orPREPEND_WWW
are set toTrue
. -
CsrfViewMiddleware
Before any view middleware that assumes that CSRF attacks have been dealt with.
It must come after
SessionMiddleware
if you‘re usingCSRF_USE_SESSIONS
. -
AuthenticationMiddleware
After
SessionMiddleware
: uses session storage. -
MessageMiddleware
After
SessionMiddleware
: can use session-based storage. -
FetchFromCacheMiddleware
After any middleware that modifies the
Vary
header: that header is used to pick a value for the cache hash-key. -
FlatpageFallbackMiddleware
Should be near the bottom as it‘s a last-resort type of middleware.
-
RedirectFallbackMiddleware
Should be near the bottom as it‘s a last-resort type of middleware.
Django視圖層——內建的中間件類