{"title":"ミドルウェア","version":"3.2","locale":"ja","docname":"ref/middleware","url":"/ja/3.2/ref/middleware/","canonical":"https://djangodocs.dev/ja/3.2/ref/middleware/","summary":"This document explains all middleware components that come with Django. For information on how to use them and how to write your own middleware, see the middleware…","html":"<span id=\"middleware\"></span><h1>ミドルウェア<a class=\"heading-anchor\" href=\"#module-django.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document explains all middleware components that come with Django. For\ninformation on how to use them and how to write your own middleware, see\nthe <a class=\"reference internal\" href=\"/ja/3.2/topics/http/middleware/\"><span class=\"doc\">middleware usage guide</span></a>.</p>\n<section id=\"available-middleware\">\n<h2>使用できる middleware<a class=\"heading-anchor\" href=\"#available-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"module-django.middleware.cache\">\n<span id=\"cache-middleware\"></span><h3>Cache middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.cache\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.cache.UpdateCacheMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">UpdateCacheMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.cache.UpdateCacheMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.cache.FetchFromCacheMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">FetchFromCacheMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.cache.FetchFromCacheMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>サイト全体でキャッシュを有効にします。有効にすると、Django を利用している各ページは <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-CACHE_MIDDLEWARE_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CACHE_MIDDLEWARE_SECONDS</span></code></a> 設定で指定した時間だけキャッシュされます。詳しくは the <a class=\"reference internal\" href=\"/ja/3.2/topics/cache/\"><span class=\"doc\">cache documentation</span></a> を読んでください。</p>\n</section>\n<section id=\"module-django.middleware.common\">\n<span id=\"common-middleware\"></span><h3>Common middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.common\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.common.CommonMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CommonMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.common.CommonMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>完璧主義者のためにいくつかの便利な機能を提供します。</p>\n<ul>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-DISALLOWED_USER_AGENTS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DISALLOWED_USER_AGENTS</span></code></a> 設定に含まれるユーザーエージェントのアクセスを禁止します。この設定は、コンパイルした正規表現オブジェクトのリストである必要があります。</p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-APPEND_SLASH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APPEND_SLASH</span></code></a> および <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-PREPEND_WWW\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PREPEND_WWW</span></code></a> 設定に基づいて、URL の書き換えを実行します。</p>\n<p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-APPEND_SLASH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APPEND_SLASH</span></code></a> が <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> で、与えられた URL がスラッシュで終わっていなくて、さらに URLconf に URL が見つからなければ、Django は / が最後に追加された新しい URL を作り、この新しい URL へリクエストをリダイレクトします。それ以外のときには、最初の URL を通常通りに処理します。</p>\n<p>たとえば、 <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar</span></code> は、 <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar</span></code> に対する有効な URL パタンがなく、<code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar/</span></code> に対しては有効なパタンが <em>存在する</em> 場合に、 <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar/</span></code> にリダイレクトされます。</p>\n<p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-PREPEND_WWW\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PREPEND_WWW</span></code></a> を <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> にすると、先頭に &quot;www.&quot; がない URL は &quot;www.&quot; で始まる同じ URL にリダイレクトされます。</p>\n<p>2つのオプションが意味するのは、URL を正規化するということです。これは、1つの URL には1つの、かつただ1つだけの場所を指すべきだという考えです。技術的には URL <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar</span></code> は <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar/</span></code> とは区別されます。実際、サーチエンジンのインデクスはこれらを区別して作成されます。よって、URL を正規化するというのがベストプラクティスなのです。</p>\n<p>If necessary, individual views may be excluded from the <code class=\"docutils literal notranslate\"><span class=\"pre\">APPEND_SLASH</span></code>\nbehavior using the <a class=\"reference internal\" href=\"/ja/3.2/topics/http/decorators/#django.views.decorators.common.no_append_slash\" title=\"django.views.decorators.common.no_append_slash\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">no_append_slash()</span></code></a>\ndecorator:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.views.decorators.common</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">no_append_slash</span>\n\n<span class=\"nd\">@no_append_slash</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">sensitive_fbv</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;View to be excluded from APPEND_SLASH.&quot;&quot;&quot;</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">()</span>\n</code></pre></div>\n<aside class=\"version-note version-changed\" data-version=\"3.2\">\n<p class=\"version-note-title\">Changed in Django 3.2</p><p>Support for the <a class=\"reference internal\" href=\"/ja/3.2/topics/http/decorators/#django.views.decorators.common.no_append_slash\" title=\"django.views.decorators.common.no_append_slash\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">no_append_slash()</span></code></a>\ndecorator was added.</p>\n</aside>\n</li>\n<li><p>Sets the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code> header for non-streaming responses.</p></li>\n</ul>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.common.CommonMiddleware.response_redirect_class\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">CommonMiddleware.</span></span><span class=\"sig-name descname\"><span class=\"pre\">response_redirect_class</span></span><a class=\"heading-anchor\" href=\"#django.middleware.common.CommonMiddleware.response_redirect_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Defaults to <a class=\"reference internal\" href=\"/ja/3.2/ref/request-response/#django.http.HttpResponsePermanentRedirect\" title=\"django.http.HttpResponsePermanentRedirect\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponsePermanentRedirect</span></code></a>. Subclass\n<code class=\"docutils literal notranslate\"><span class=\"pre\">CommonMiddleware</span></code> and override the attribute to customize the redirects\nissued by the middleware.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.common.BrokenLinkEmailsMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">BrokenLinkEmailsMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.common.BrokenLinkEmailsMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<ul class=\"simple\">\n<li><p>Sends broken link notification emails to <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-MANAGERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MANAGERS</span></code></a> (see\n<a class=\"reference internal\" href=\"/ja/3.2/howto/error-reporting/\"><span class=\"doc\">エラーのレポート</span></a>).</p></li>\n</ul>\n</section>\n<section id=\"module-django.middleware.gzip\">\n<span id=\"gzip-middleware\"></span><h3>GZip middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.gzip\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.gzip.GZipMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">GZipMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.gzip.GZipMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>Security researchers recently revealed that when compression techniques\n(including <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code>) are used on a website, the site may become\nexposed to a number of possible attacks. Before using <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code> on\nyour site, you should consider very carefully whether you are subject to\nthese attacks. If you're in <em>any</em> doubt about whether you're affected, you\nshould avoid using <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code>. For more details, see the <a class=\"reference external\" href=\"http://breachattack.com/resources/BREACH%20-%20SSL,%20gone%20in%2030%20seconds.pdf\">the BREACH\npaper (PDF)</a> and <a class=\"reference external\" href=\"http://breachattack.com\">breachattack.com</a>.</p>\n</aside>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">django.middleware.gzip.GZipMiddleware</span></code> compresses content for browsers\nthat understand GZip compression (all modern browsers).</p>\n<p>This middleware should be placed before any other middleware that need to\nread or write the response body so that compression happens afterward.</p>\n<p>It will NOT compress content if any of the following are true:</p>\n<ul class=\"simple\">\n<li><p>The content body is less than 200 bytes long.</p></li>\n<li><p>The response has already set the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Encoding</span></code> header.</p></li>\n<li><p>The request (the browser) hasn't sent an <code class=\"docutils literal notranslate\"><span class=\"pre\">Accept-Encoding</span></code> header\ncontaining <code class=\"docutils literal notranslate\"><span class=\"pre\">gzip</span></code>.</p></li>\n</ul>\n<p>If the response has an <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code> header, the ETag is made weak to comply with\n<span class=\"target\" id=\"index-0\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc7232.html#section-2.1\"><strong>RFC 7232 Section 2.1</strong></a>.</p>\n<p>You can apply GZip compression to individual views using the\n<a class=\"reference internal\" href=\"/ja/3.2/topics/http/decorators/#django.views.decorators.gzip.gzip_page\" title=\"django.views.decorators.gzip.gzip_page\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">gzip_page()</span></code></a> decorator.</p>\n</section>\n<section id=\"module-django.middleware.http\">\n<span id=\"conditional-get-middleware\"></span><h3>Conditional GET middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.http\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.http.ConditionalGetMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">ConditionalGetMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.http.ConditionalGetMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Handles conditional GET operations. If the response doesn't have an <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code>\nheader, the middleware adds one if needed. If the response has an <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Last-Modified</span></code> header, and the request has <code class=\"docutils literal notranslate\"><span class=\"pre\">If-None-Match</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">If-Modified-Since</span></code>, the response is replaced by an\n<a class=\"reference internal\" href=\"/ja/3.2/ref/request-response/#django.http.HttpResponseNotModified\" title=\"django.http.HttpResponseNotModified\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponseNotModified</span></code></a>.</p>\n</section>\n<section id=\"module-django.middleware.locale\">\n<span id=\"locale-middleware\"></span><h3>Locale ミドルウェア<a class=\"heading-anchor\" href=\"#module-django.middleware.locale\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.locale.LocaleMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">LocaleMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.locale.LocaleMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>リクエストからのデータに基づいた言語セクションを有効化します。この機能は、それぞれのユーザに対してコンテンツをカスタマイズします。 <a class=\"reference internal\" href=\"/ja/3.2/topics/i18n/translation/\"><span class=\"doc\">国際化のドキュメント</span></a> を参照してください。</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.locale.LocaleMiddleware.response_redirect_class\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">LocaleMiddleware.</span></span><span class=\"sig-name descname\"><span class=\"pre\">response_redirect_class</span></span><a class=\"heading-anchor\" href=\"#django.middleware.locale.LocaleMiddleware.response_redirect_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>デフォルトは <a class=\"reference internal\" href=\"/ja/3.2/ref/request-response/#django.http.HttpResponseRedirect\" title=\"django.http.HttpResponseRedirect\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponseRedirect</span></code></a> です。<code class=\"docutils literal notranslate\"><span class=\"pre\">LocaleMiddleware</span></code> をサブクラス化して属性をオーバーライドし、ミドルウェアによって発行されたリダイレクトをカスタマイズします。</p>\n</section>\n<section id=\"module-django.contrib.messages.middleware\">\n<span id=\"message-middleware\"></span><h3>Message ミドルウェア<a class=\"heading-anchor\" href=\"#module-django.contrib.messages.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.messages.middleware.MessageMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">MessageMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.messages.middleware.MessageMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>クッキーおよびセッションをベースとしたメッセージサポートを有効化します。<a class=\"reference internal\" href=\"/ja/3.2/ref/contrib/messages/\"><span class=\"doc\">メッセージのドキュメント</span></a> を参照してください。</p>\n</section>\n<section id=\"module-django.middleware.security\">\n<span id=\"id1\"></span><span id=\"security-middleware\"></span><h3>Security middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.security\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>If your deployment situation allows, it's usually a good idea to have your\nfront-end Web server perform the functionality provided by the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code>. That way, if there are requests that aren't served\nby Django (such as static media or user-uploaded files), they will have\nthe same protections as requests to your Django application.</p>\n</aside>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.security.SecurityMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">SecurityMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.security.SecurityMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">django.middleware.security.SecurityMiddleware</span></code> provides several security\nenhancements to the request/response cycle. Each one can be independently\nenabled or disabled with a setting.</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_BROWSER_XSS_FILTER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_BROWSER_XSS_FILTER</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_CONTENT_TYPE_NOSNIFF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CONTENT_TYPE_NOSNIFF</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_HSTS_INCLUDE_SUBDOMAINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_INCLUDE_SUBDOMAINS</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_HSTS_PRELOAD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_PRELOAD</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_HSTS_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_SECONDS</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_REDIRECT_EXEMPT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REDIRECT_EXEMPT</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_REFERRER_POLICY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REFERRER_POLICY</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_SSL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_HOST</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_SSL_REDIRECT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_REDIRECT</span></code></a></p></li>\n</ul>\n<section id=\"http-strict-transport-security\">\n<span id=\"id2\"></span><h4>HTTP Strict Transport Security<a class=\"heading-anchor\" href=\"#http-strict-transport-security\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>For sites that should only be accessed over HTTPS, you can instruct modern\nbrowsers to refuse to connect to your domain name via an insecure connection\n(for a given period of time) by setting the <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security\">&quot;Strict-Transport-Security&quot;\nheader</a>. This reduces your exposure to some SSL-stripping man-in-the-middle\n(MITM) attacks.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will set this header for you on all HTTPS responses if\nyou set the <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_HSTS_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_SECONDS</span></code></a> setting to a non-zero integer value.</p>\n<p>When enabling HSTS, it's a good idea to first use a small value for testing,\nfor example, <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_HSTS_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_SECONDS</span> <span class=\"pre\">=</span> <span class=\"pre\">3600</span></code></a> for one\nhour. Each time a Web browser sees the HSTS header from your site, it will\nrefuse to communicate non-securely (using HTTP) with your domain for the given\nperiod of time. Once you confirm that all assets are served securely on your\nsite (i.e. HSTS didn't break anything), it's a good idea to increase this value\nso that infrequent visitors will be protected (31536000 seconds, i.e. 1 year,\nis common).</p>\n<p>Additionally, if you set the <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_HSTS_INCLUDE_SUBDOMAINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_INCLUDE_SUBDOMAINS</span></code></a> setting\nto <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will add the <code class=\"docutils literal notranslate\"><span class=\"pre\">includeSubDomains</span></code> directive\nto the <code class=\"docutils literal notranslate\"><span class=\"pre\">Strict-Transport-Security</span></code> header. This is recommended (assuming all\nsubdomains are served exclusively using HTTPS), otherwise your site may still\nbe vulnerable via an insecure connection to a subdomain.</p>\n<p>If you wish to submit your site to the <a class=\"reference external\" href=\"https://hstspreload.org/\">browser preload list</a>, set the\n<a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_HSTS_PRELOAD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_PRELOAD</span></code></a> setting to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>. That appends the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">preload</span></code> directive to the <code class=\"docutils literal notranslate\"><span class=\"pre\">Strict-Transport-Security</span></code> header.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>The HSTS policy applies to your entire domain, not just the URL of the\nresponse that you set the header on. Therefore, you should only use it if\nyour entire domain is served via HTTPS only.</p>\n<p>Browsers properly respecting the HSTS header will refuse to allow users to\nbypass warnings and connect to a site with an expired, self-signed, or\notherwise invalid SSL certificate. If you use HSTS, make sure your\ncertificates are in good shape and stay that way!</p>\n</aside>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>If you are deployed behind a load-balancer or reverse-proxy server, and the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Strict-Transport-Security</span></code> header is not being added to your responses,\nit may be because Django doesn't realize that it's on a secure connection;\nyou may need to set the <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_PROXY_SSL_HEADER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_PROXY_SSL_HEADER</span></code></a> setting.</p>\n</aside>\n</section>\n<section id=\"referrer-policy\">\n<span id=\"id4\"></span><h4>Referrer Policy<a class=\"heading-anchor\" href=\"#referrer-policy\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Browsers use <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer\">the Referer header</a> as a way to send information to a site\nabout how users got there. When a user clicks a link, the browser will send the\nfull URL of the linking page as the referrer. While this can be useful for some\npurposes -- like figuring out who's linking to your site -- it also can cause\nprivacy concerns by informing one site that a user was visiting another site.</p>\n<p>Some browsers have the ability to accept hints about whether they should send\nthe HTTP <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header when a user clicks a link; this hint is provided\nvia <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy\">the Referrer-Policy header</a>. This header can suggest any of three\nbehaviors to browsers:</p>\n<ul class=\"simple\">\n<li><p>Full URL: send the entire URL in the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header. For example, if the\nuser is visiting <code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/page.html</span></code>, the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header\nwould contain <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;https://example.com/page.html&quot;</span></code>.</p></li>\n<li><p>Origin only: send only the &quot;origin&quot; in the referrer. The origin consists of\nthe scheme, host and (optionally) port number. For example, if the user is\nvisiting <code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/page.html</span></code>, the origin would be\n<code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/</span></code>.</p></li>\n<li><p>No referrer: do not send a <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header at all.</p></li>\n</ul>\n<p>There are two types of conditions this header can tell a browser to watch out\nfor:</p>\n<ul class=\"simple\">\n<li><p>Same-origin versus cross-origin: a link from <code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/1.html</span></code>\nto <code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/2.html</span></code> is same-origin. A link from\n<code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/page.html</span></code> to <code class=\"docutils literal notranslate\"><span class=\"pre\">https://not.example.com/page.html</span></code> is\ncross-origin.</p></li>\n<li><p>Protocol downgrade: a downgrade occurs if the page containing the link is\nserved via HTTPS, but the page being linked to is not served via HTTPS.</p></li>\n</ul>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>When your site is served via HTTPS, <a class=\"reference internal\" href=\"/ja/3.2/ref/csrf/#using-csrf\"><span class=\"std std-ref\">Django's CSRF protection system</span></a> requires the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header to be present, so completely\ndisabling the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header will interfere with CSRF protection. To\ngain most of the benefits of disabling <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> headers while also\nkeeping CSRF protection, consider enabling only same-origin referrers.</p>\n</aside>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> can set the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referrer-Policy</span></code> header for you, based on\nthe <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_REFERRER_POLICY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REFERRER_POLICY</span></code></a> setting (note spelling: browsers send a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header when a user clicks a link, but the header instructing a\nbrowser whether to do so is spelled <code class=\"docutils literal notranslate\"><span class=\"pre\">Referrer-Policy</span></code>). The valid values for\nthis setting are:</p>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">no-referrer</span></code></dt><dd><p>Instructs the browser to send no referrer for links clicked on this site.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">no-referrer-when-downgrade</span></code></dt><dd><p>Instructs the browser to send a full URL as the referrer, but only when no\nprotocol downgrade occurs.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">origin</span></code></dt><dd><p>Instructs the browser to send only the origin, not the full URL, as the\nreferrer.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">origin-when-cross-origin</span></code></dt><dd><p>Instructs the browser to send the full URL as the referrer for same-origin\nlinks, and only the origin for cross-origin links.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">same-origin</span></code></dt><dd><p>Instructs the browser to send a full URL, but only for same-origin links. No\nreferrer will be sent for cross-origin links.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">strict-origin</span></code></dt><dd><p>Instructs the browser to send only the origin, not the full URL, and to send\nno referrer when a protocol downgrade occurs.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">strict-origin-when-cross-origin</span></code></dt><dd><p>Instructs the browser to send the full URL when the link is same-origin and\nno protocol downgrade occurs; send only the origin when the link is\ncross-origin and no protocol downgrade occurs; and no referrer when a\nprotocol downgrade occurs.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">unsafe-url</span></code></dt><dd><p>Instructs the browser to always send the full URL as the referrer.</p>\n</dd>\n</dl>\n<aside class=\"admonition-unknown-policy-values admonition\">\n<p class=\"admonition-title\">Unknown Policy Values</p>\n<p>Where a policy value is <a class=\"reference external\" href=\"https://w3c.github.io/webappsec-referrer-policy/#unknown-policy-values\">unknown</a> by a user agent, it is possible to\nspecify multiple policy values to provide a fallback. The last specified\nvalue that is understood takes precedence. To support this, an iterable or\ncomma-separated string can be used with <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_REFERRER_POLICY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REFERRER_POLICY</span></code></a>.</p>\n</aside>\n</section>\n<section id=\"x-content-type-options-nosniff\">\n<span id=\"x-content-type-options\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">X-Content-Type-Options:</span> <span class=\"pre\">nosniff</span></code><a class=\"heading-anchor\" href=\"#x-content-type-options-nosniff\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Some browsers will try to guess the content types of the assets that they\nfetch, overriding the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code> header. While this can help display\nsites with improperly configured servers, it can also pose a security\nrisk.</p>\n<p>If your site serves user-uploaded files, a malicious user could upload a\nspecially-crafted file that would be interpreted as HTML or JavaScript by\nthe browser when you expected it to be something harmless.</p>\n<p>To prevent the browser from guessing the content type and force it to\nalways use the type provided in the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code> header, you can pass\nthe <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options\">X-Content-Type-Options: nosniff</a> header.  <code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will\ndo this for all responses if the <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_CONTENT_TYPE_NOSNIFF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CONTENT_TYPE_NOSNIFF</span></code></a> setting\nis <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n<p>Note that in most deployment situations where Django isn't involved in serving\nuser-uploaded files, this setting won't help you. For example, if your\n<a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a> is served directly by your front-end Web server (nginx,\nApache, etc.) then you'd want to set this header there. On the other hand, if\nyou are using Django to do something like require authorization in order to\ndownload files and you cannot set the header using your Web server, this\nsetting will be useful.</p>\n</section>\n<section id=\"x-xss-protection-1-mode-block\">\n<span id=\"x-xss-protection\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">X-XSS-Protection:</span> <span class=\"pre\">1;</span> <span class=\"pre\">mode=block</span></code><a class=\"heading-anchor\" href=\"#x-xss-protection-1-mode-block\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Some browsers have the ability to block content that appears to be an <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Cross-site_scripting\">XSS\nattack</a>. They work by looking for JavaScript content in the GET or POST\nparameters of a page. If the JavaScript is replayed in the server's response,\nthe page is blocked from rendering and an error page is shown instead.</p>\n<p>The <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection\">X-XSS-Protection header</a> is used to control the operation of the\nXSS filter.</p>\n<p>To enable the XSS filter in the browser, and force it to always block\nsuspected XSS attacks, you can pass the <code class=\"docutils literal notranslate\"><span class=\"pre\">X-XSS-Protection:</span> <span class=\"pre\">1;</span> <span class=\"pre\">mode=block</span></code>\nheader. <code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will do this for all responses if the\n<a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_BROWSER_XSS_FILTER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_BROWSER_XSS_FILTER</span></code></a> setting is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>The browser XSS filter is a useful defense measure, but must not be\nrelied upon exclusively. It cannot detect all XSS attacks and not all\nbrowsers support the header. Ensure you are still <a class=\"reference internal\" href=\"/ja/3.2/topics/security/#cross-site-scripting\"><span class=\"std std-ref\">validating and\nsanitizing</span></a> all input to prevent XSS attacks.</p>\n</aside>\n</section>\n<section id=\"ssl-redirect\">\n<span id=\"id10\"></span><h4>SSL リダイレクト<a class=\"heading-anchor\" href=\"#ssl-redirect\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>サイトが HTTP と HTTPS 接続の両方をサポートしている場合、多くのユーザーはデフォルトでセキュアでない接続を行ってしまいます。最善のセキュリティのためには、すべての HTTP 接続を HTTPS 接続にリダイレクトするべきです。</p>\n<p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_SSL_REDIRECT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_REDIRECT</span></code></a> 設定を True に設定すれば、 <code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> が すべての HTTP 接続を HTTPS に parmanent (HTTP 301) にリダイレクトしてくれます。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>パフォーマンス上の理由により、このようなリダイレクトは Django の外部、フロントエンドのロードバランサーや <a class=\"reference external\" href=\"https://nginx.org/\">nginx</a> などのリバースプロキシサーバーで実行した方が良いです。 <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_SSL_REDIRECT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_REDIRECT</span></code></a> は、これらのオプションが使用できないデプロイ環境で使われることを想定しています。</p>\n</aside>\n<p>If the <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_SSL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_HOST</span></code></a> setting has a value, all redirects will be\nsent to that host instead of the originally-requested host.</p>\n<p>If there are a few pages on your site that should be available over HTTP, and\nnot redirected to HTTPS, you can list regular expressions to match those URLs\nin the <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_REDIRECT_EXEMPT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REDIRECT_EXEMPT</span></code></a> setting.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>If you are deployed behind a load-balancer or reverse-proxy server and\nDjango can't seem to tell when a request actually is already secure, you\nmay need to set the <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-SECURE_PROXY_SSL_HEADER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_PROXY_SSL_HEADER</span></code></a> setting.</p>\n</aside>\n</section>\n</section>\n<section id=\"module-django.contrib.sessions.middleware\">\n<span id=\"session-middleware\"></span><h3>Session middleware<a class=\"heading-anchor\" href=\"#module-django.contrib.sessions.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sessions.middleware.SessionMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">SessionMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.sessions.middleware.SessionMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>セッションのサポートを有効にします。詳しくは <a class=\"reference internal\" href=\"/ja/3.2/topics/http/sessions/\"><span class=\"doc\">セッションのドキュメント</span></a> を読んでください。</p>\n</section>\n<section id=\"module-django.contrib.sites.middleware\">\n<span id=\"site-middleware\"></span><h3>Site middleware<a class=\"heading-anchor\" href=\"#module-django.contrib.sites.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sites.middleware.CurrentSiteMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CurrentSiteMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.sites.middleware.CurrentSiteMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>受信側のすべての <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> オブジェクトに、現在のサイトを表す <code class=\"docutils literal notranslate\"><span class=\"pre\">site</span></code> 属性を追加します。詳しくは <a class=\"reference internal\" href=\"/ja/3.2/ref/contrib/sites/#site-middleware\"><span class=\"std std-ref\">sites documentation</span></a> を読んでください。</p>\n</section>\n<section id=\"module-django.contrib.auth.middleware\">\n<span id=\"authentication-middleware\"></span><h3>Authentication middleware<a class=\"heading-anchor\" href=\"#module-django.contrib.auth.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.AuthenticationMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">AuthenticationMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.AuthenticationMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>受信側のすべての <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> オブジェクトに、現在のログイン中のユーザを表す <code class=\"docutils literal notranslate\"><span class=\"pre\">user</span></code> 属性を追加します。詳しくは <a class=\"reference internal\" href=\"/ja/3.2/topics/auth/default/#auth-web-requests\"><span class=\"std std-ref\">Authentication in Web requests</span></a> を読んでください。</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.RemoteUserMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">RemoteUserMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.RemoteUserMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Web サーバが提供する認証機能を利用するミドルウェアです。詳しい使い方については <a class=\"reference internal\" href=\"/ja/3.2/howto/auth-remote-user/\"><span class=\"doc\">REMOTE_USER を使用した認証</span></a> を読んでください。</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.PersistentRemoteUserMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">PersistentRemoteUserMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.PersistentRemoteUserMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>ログインページでのみ有効になる、Web サーバが提供する認証機能を利用するミドルウェアです。詳しくい使い方については <a class=\"reference internal\" href=\"/ja/3.2/howto/auth-remote-user/#persistent-remote-user-middleware-howto\"><span class=\"std std-ref\">ログインページでのみ REMOTE_USER を使用する</span></a> を読んでください。</p>\n</section>\n<section id=\"csrf-protection-middleware\">\n<h3>CSRF プロテクション middleware<a class=\"heading-anchor\" href=\"#csrf-protection-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.csrf.CsrfViewMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CsrfViewMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.csrf.CsrfViewMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>POST フォームに隠しフォームフィールドを追加し、リクエストの値が正しいかチェックすることで、Cross Site Request Forgery に対するプロテクションを追加します。詳しくは <a class=\"reference internal\" href=\"/ja/3.2/ref/csrf/\"><span class=\"doc\">Cross Site Request Forgery プロテクションのドキュメント</span></a> を読んでください。</p>\n</section>\n<section id=\"x-frame-options-middleware\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code> middleware<a class=\"heading-anchor\" href=\"#x-frame-options-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.clickjacking.XFrameOptionsMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">XFrameOptionsMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.clickjacking.XFrameOptionsMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>シンプルな <a class=\"reference internal\" href=\"/ja/3.2/ref/clickjacking/\"><span class=\"doc\">クリックジャッキングに対する X-Frame-Options ヘッダ経由のプロテクション</span></a> です。</p>\n</section>\n</section>\n<section id=\"middleware-ordering\">\n<span id=\"id11\"></span><h2>Middleware の順序<a class=\"heading-anchor\" href=\"#middleware-ordering\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django の多様なミドルウェアクラスの順序に関する注意点を挙げておきます。</p>\n<ol class=\"arabic\">\n<li><p><a class=\"reference internal\" href=\"#django.middleware.security.SecurityMiddleware\" title=\"django.middleware.security.SecurityMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code></a></p>\n<p>SSL リダイレクトを有効にしているなら、他のたくさんの必要のないミドルウェアが実行されないように、リストの先頭付近に置くべきです。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.cache.UpdateCacheMiddleware\" title=\"django.middleware.cache.UpdateCacheMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code></a></p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> ヘッダに変更を加えるミドルウェア (<code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">LocaleMiddleware</span></code>) の前に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.gzip.GZipMiddleware\" title=\"django.middleware.gzip.GZipMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code></a></p>\n<p>response body を変更・使用する可能性のあるミドルウェアの前に置きます。</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> ヘッダを修正するため、 <code class=\"docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code> の後に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.sessions.middleware.SessionMiddleware\" title=\"django.contrib.sessions.middleware.SessionMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code></a></p>\n<p>Before any middleware that may raise an exception to trigger an error\nview (such as <a class=\"reference internal\" href=\"/ja/3.2/ref/exceptions/#django.core.exceptions.PermissionDenied\" title=\"django.core.exceptions.PermissionDenied\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">PermissionDenied</span></code></a>) if you're\nusing <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-CSRF_USE_SESSIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_USE_SESSIONS</span></code></a>.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> ヘッダを修正するため、 <code class=\"docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code> の後に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.http.ConditionalGetMiddleware\" title=\"django.middleware.http.ConditionalGetMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ConditionalGetMiddleware</span></code></a></p>\n<p>Before any middleware that may change the response (it sets the <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code>\nheader).</p>\n<p>gzip されたコンテンツに対して <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code> ヘッダを計算しないように、 <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code> の後に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.locale.LocaleMiddleware\" title=\"django.middleware.locale.LocaleMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">LocaleMiddleware</span></code></a></p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code> (session データを使う) と <code class=\"docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> ヘッダを修正する) の後のできるだけ高い位置に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.common.CommonMiddleware\" title=\"django.middleware.common.CommonMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CommonMiddleware</span></code></a></p>\n<p>Before any middleware that may change the response (it sets the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code> header). A middleware that appears before\n<code class=\"docutils literal notranslate\"><span class=\"pre\">CommonMiddleware</span></code> and changes the response must reset <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code>.</p>\n<p><a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-APPEND_SLASH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APPEND_SLASH</span></code></a> か <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-PREPEND_WWW\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PREPEND_WWW</span></code></a> が <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> に設定されているとリダイレクトされるので、先頭の近くに置きます。</p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code> if you're using <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-CSRF_USE_SESSIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_USE_SESSIONS</span></code></a>.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.csrf.CsrfViewMiddleware\" title=\"django.middleware.csrf.CsrfViewMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware</span></code></a></p>\n<p>CSRF 攻撃が可能なすべての view ミドルウェアの前に置きます。</p>\n<p>Before <a class=\"reference internal\" href=\"#django.contrib.auth.middleware.RemoteUserMiddleware\" title=\"django.contrib.auth.middleware.RemoteUserMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RemoteUserMiddleware</span></code></a>, or any\nother authentication middleware that may perform a login, and hence rotate\nthe CSRF token, before calling down the middleware chain.</p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code> if you're using <a class=\"reference internal\" href=\"/ja/3.2/ref/settings/#std-setting-CSRF_USE_SESSIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_USE_SESSIONS</span></code></a>.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.auth.middleware.AuthenticationMiddleware\" title=\"django.contrib.auth.middleware.AuthenticationMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AuthenticationMiddleware</span></code></a></p>\n<p>session ストレージを使うので、 <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code> の後に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.messages.middleware.MessageMiddleware\" title=\"django.contrib.messages.middleware.MessageMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MessageMiddleware</span></code></a></p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code>: can use session-based storage.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.cache.FetchFromCacheMiddleware\" title=\"django.middleware.cache.FetchFromCacheMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FetchFromCacheMiddleware</span></code></a></p>\n<p>キャッシュのハッシュキーを生成するのに <code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> ヘッダを使用するため、このヘッダを修正するすべてのミドルウェアのあとに置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/contrib/flatpages/#django.contrib.flatpages.middleware.FlatpageFallbackMiddleware\" title=\"django.contrib.flatpages.middleware.FlatpageFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FlatpageFallbackMiddleware</span></code></a></p>\n<p>最後に実行されるタイプのミドルウェアなので、できるだけ下に置く必要があります。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"/ja/3.2/ref/contrib/redirects/#django.contrib.redirects.middleware.RedirectFallbackMiddleware\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RedirectFallbackMiddleware</span></code></a></p>\n<p>最後に実行されるタイプのミドルウェアなので、できるだけ下に置く必要があります。</p>\n</li>\n</ol>\n</section>","rootId":"module-django.middleware","toc":[{"title":"使用できる middleware","anchor":"available-middleware","children":[{"title":"Cache middleware","anchor":"module-django.middleware.cache","children":[]},{"title":"Common middleware","anchor":"module-django.middleware.common","children":[]},{"title":"GZip middleware","anchor":"module-django.middleware.gzip","children":[]},{"title":"Conditional GET middleware","anchor":"module-django.middleware.http","children":[]},{"title":"Locale ミドルウェア","anchor":"module-django.middleware.locale","children":[]},{"title":"Message ミドルウェア","anchor":"module-django.contrib.messages.middleware","children":[]},{"title":"Security middleware","anchor":"module-django.middleware.security","children":[{"title":"HTTP Strict Transport Security","anchor":"http-strict-transport-security","children":[]},{"title":"Referrer Policy","anchor":"referrer-policy","children":[]},{"title":"X-Content-Type-Options: nosniff","anchor":"x-content-type-options-nosniff","children":[]},{"title":"X-XSS-Protection: 1; mode=block","anchor":"x-xss-protection-1-mode-block","children":[]},{"title":"SSL リダイレクト","anchor":"ssl-redirect","children":[]}]},{"title":"Session middleware","anchor":"module-django.contrib.sessions.middleware","children":[]},{"title":"Site middleware","anchor":"module-django.contrib.sites.middleware","children":[]},{"title":"Authentication middleware","anchor":"module-django.contrib.auth.middleware","children":[]},{"title":"CSRF プロテクション middleware","anchor":"csrf-protection-middleware","children":[]},{"title":"X-Frame-Options middleware","anchor":"x-frame-options-middleware","children":[]}]},{"title":"Middleware の順序","anchor":"middleware-ordering","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/ja/3.2/ref/"}],"prev":{"docname":"ref/forms/validation","title":"フォームとフィールドの検証","url":"/ja/3.2/ref/forms/validation/"},"next":{"docname":"ref/migration-operations","title":"マイグレーション操作","url":"/ja/3.2/ref/migration-operations/"},"formats":{"html":"/ja/3.2/ref/middleware/","markdown":"/ja/3.2/ref/middleware.md","json":"/ja/3.2/ref/middleware.json"},"source":"https://github.com/django/django/blob/stable/3.2.x/docs/ref/middleware.txt","official":"https://docs.djangoproject.com/ja/3.2/ref/middleware/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}