{"title":"Django のユーザーデータベースに対する Apache からの認証","version":"5.1","locale":"ja","docname":"howto/deployment/wsgi/apache-auth","url":"/ja/5.1/howto/deployment/wsgi/apache-auth/","canonical":"https://djangodocs.dev/ja/5.1/howto/deployment/wsgi/apache-auth/","summary":"複数の認証データベースを同期させることは Apache を扱う上でよくある問題なので、 Django の認証システムに対して直接認証するように Apache を設定できます。","html":"<h1>Django のユーザーデータベースに対する Apache からの認証<a class=\"heading-anchor\" href=\"#how-to-authenticate-against-django-s-user-database-from-apache\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>複数の認証データベースを同期させることは Apache を扱う上でよくある問題なので、 Django の <a class=\"reference internal\" href=\"/ja/5.1/topics/auth/\"><span class=\"doc\">認証システム</span></a> に対して直接認証するように Apache を設定できます。これには Apache version &gt;= 2.2 と mod_wsgi &gt;= 2.0 が必要です。例えば下記のようなことができます:</p>\n<ul class=\"simple\">\n<li><p>認証されたユーザにのみ、静的/メディアファイルを Apache から直接提供する。</p></li>\n<li><p>特定のパーミッションを持つ Django ユーザに対して、 <a class=\"reference external\" href=\"https://subversion.apache.org/\">Subversion</a> リポジトリへのアクセスを認証する。</p></li>\n<li><p><a class=\"reference external\" href=\"https://httpd.apache.org/docs/2.2/mod/mod_dav.html\">mod_dav</a> で作成された WebDAV 共有に特定のユーザが接続できるように許可する。</p></li>\n</ul>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>もし <a class=\"reference internal\" href=\"/ja/5.1/topics/auth/customizing/#auth-custom-user\"><span class=\"std std-ref\">カスタムユーザーモデル</span></a> をインストールしていて、このデフォルトの認証ハンドラを使用したい場合は、 <code class=\"docutils literal notranslate\">is_active</code> 属性をサポートしている必要があります。グループベースの認可を使用したい場合、カスタムユーザは 'group' という名前のリレーションを持ち、'name' フィールドを持つリレーション先のオブジェクトを参照する必要があります。カスタムの mod_wsgi 認証ハンドラがこれらの要件に適合しない場合は、 独自のカスタムの mod_wsgi 認証ハンドラを指定することもできます。</p>\n</aside>\n<section id=\"authentication-with-mod-wsgi\">\n<h2><code class=\"docutils literal notranslate\">mod_wsgi</code> による認証<a class=\"heading-anchor\" href=\"#authentication-with-mod-wsgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>以下の設定における <code class=\"docutils literal notranslate\">WSGIApplicationGroup %{GLOBAL}</code> の使用は、 Apache インスタンスが 1 つの Django アプリケーションだけを実行していることを想定しています。複数の Django アプリケーションを実行している場合は、この設定については mod_wsgi ドキュメントの <a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/user-guides/configuration-guidelines.html#defining-application-groups\">Defining Application Groups</a> セクションを参照してください。</p>\n</aside>\n<p>mod_wsgiがインストールされ、有効になっていることと、 <a class=\"reference internal\" href=\"/ja/5.1/howto/deployment/wsgi/modwsgi/\"><span class=\"doc\">Apache と mod_wsgi</span></a> をセットアップするステップに従っていることを確認してください。</p>\n<p>次に、Apacheの設定を編集して、認証されたユーザだけが閲覧できるようにしたい場所を追加します:</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</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=\"Apache code\"><code><span class=\"nb\">WSGIScriptAlias</span> / <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n<span class=\"nb\">WSGIPythonPath</span> <span class=\"sx\">/path/to/mysite.com</span>\n\n<span class=\"nb\">WSGIProcessGroup</span> %{GLOBAL}\n<span class=\"nb\">WSGIApplicationGroup</span> %{GLOBAL}\n\n<span class=\"nt\">&lt;Location</span> <span class=\"s\">&quot;/secret&quot;</span><span class=\"nt\">&gt;</span>\n    <span class=\"nb\">AuthType</span> Basic\n    <span class=\"nb\">AuthName</span> <span class=\"s2\">&quot;Top Secret&quot;</span>\n    <span class=\"nb\">Require</span> valid-user\n    <span class=\"nb\">AuthBasicProvider</span> wsgi\n    <span class=\"nb\">WSGIAuthUserScript</span> <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n<span class=\"nt\">&lt;/Location&gt;</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">WSGIAuthUserScript</code> ディレクティブは、プロンプトから受け取ったユーザ名とパスワードを渡して、指定した wsgi スクリプトの <code class=\"docutils literal notranslate\">check_password</code> 関数を実行するように mod_wsgi に指示します。この例では、 <code class=\"docutils literal notranslate\">WSGIAuthUserScript</code> は <a class=\"reference internal\" href=\"/ja/5.1/howto/deployment/wsgi/\"><span class=\"doc\">django-admin startproject で作成される</span></a> アプリケーションを定義する <code class=\"docutils literal notranslate\">WSGIScriptAlias</code> と同じです。</p>\n<aside class=\"admonition-using-apache-2-2-with-authentication admonition\" role=\"note\">\n<p class=\"admonition-title\">Apache 2.2 を認証とともに使う</p>\n<p><code class=\"docutils literal notranslate\">mod_auth_basic</code> と <code class=\"docutils literal notranslate\">mod_authz_user</code> がロードされていることを確認してください。</p>\n<p>これらは Apache に静的にコンパイルされているかもしれませんし、LoadModule を使って <code class=\"docutils literal notranslate\">httpd.conf</code> で動的にロードする必要があるかもしれません:</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</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=\"Apache code\"><code><span class=\"nb\">LoadModule</span> auth_basic_module modules/mod_auth_basic.so\n<span class=\"nb\">LoadModule</span> authz_user_module modules/mod_authz_user.so\n</code></pre></div>\n</aside>\n<p>最後に、WSGIスクリプト <code class=\"docutils literal notranslate\">mysite.wsgi</code> を編集して、 <code class=\"docutils literal notranslate\">check_password</code> 関数をインポートすることで、Apacheの認証とサイトの認証メカニズムを紐づけます:</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\">import</span> <span class=\"nn\">os</span>\n\nos<span class=\"o\">.</span>environ<span class=\"p\">[</span><span class=\"s2\">&quot;DJANGO_SETTINGS_MODULE&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;mysite.settings&quot;</span>\n\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.handlers.modwsgi</span> <span class=\"kn\">import</span> check_password\n\n<span class=\"kn\">from</span> <span class=\"nn\">django.core.handlers.wsgi</span> <span class=\"kn\">import</span> WSGIHandler\n\napplication <span class=\"o\">=</span> WSGIHandler<span class=\"p\">()</span>\n</code></pre></div>\n<p>これで、 <code class=\"docutils literal notranslate\">/secret/</code> で始まるリクエストはユーザー認証が必要になりました。</p>\n<p>mod_wsgi の <a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/user-guides/access-control-mechanisms.html\">access control mechanisms documentation</a> に、認証の代替方法についての詳細と情報があります。</p>\n<section id=\"authorization-with-mod-wsgi-and-django-groups\">\n<h3><code class=\"docutils literal notranslate\">mod_wsgi</code> と Django グループを使った認可<a class=\"heading-anchor\" href=\"#authorization-with-mod-wsgi-and-django-groups\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>mod_wsgi は、グループのメンバーに特定の場所を制限する機能も提供します。</p>\n<p>この場合、Apacheの設定は次のようになります:</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</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=\"Apache code\"><code><span class=\"nb\">WSGIScriptAlias</span> / <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n\n<span class=\"nb\">WSGIProcessGroup</span> %{GLOBAL}\n<span class=\"nb\">WSGIApplicationGroup</span> %{GLOBAL}\n\n<span class=\"nt\">&lt;Location</span> <span class=\"s\">&quot;/secret&quot;</span><span class=\"nt\">&gt;</span>\n    <span class=\"nb\">AuthType</span> Basic\n    <span class=\"nb\">AuthName</span> <span class=\"s2\">&quot;Top Secret&quot;</span>\n    <span class=\"nb\">AuthBasicProvider</span> wsgi\n    <span class=\"nb\">WSGIAuthUserScript</span> <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n    <span class=\"nb\">WSGIAuthGroupScript</span> <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n    <span class=\"nb\">Require</span> <span class=\"k\">group</span> secret-agents\n    <span class=\"nb\">Require</span> valid-user\n<span class=\"nt\">&lt;/Location&gt;</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">WSGIAuthGroupScript</code> ディレクティブをサポートするために、同じ WSGI スクリプト <code class=\"docutils literal notranslate\">mysite.wsgi</code> は、与えられたユーザが属しているグループのリストを返す <code class=\"docutils literal notranslate\">groups_for_user</code> 関数もインポートする必要があります。</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.handlers.modwsgi</span> <span class=\"kn\">import</span> check_password<span class=\"p\">,</span> groups_for_user\n</code></pre></div>\n<p>これで、 <code class=\"docutils literal notranslate\">/secret/</code> に対するリクエストは、ユーザーが &quot;secret-agents&quot; グループのメンバーであることも要求するようになりました。</p>\n</section>\n</section>","rootId":"how-to-authenticate-against-django-s-user-database-from-apache","toc":[{"title":"mod_wsgi による認証","anchor":"authentication-with-mod-wsgi","children":[{"title":"mod_wsgi と Django グループを使った認可","anchor":"authorization-with-mod-wsgi-and-django-groups","children":[]}]}],"breadcrumbs":[{"docname":"howto/index","title":"How-to ガイド","url":"/ja/5.1/howto/"},{"docname":"howto/deployment/index","title":"Djangoをデプロイするには","url":"/ja/5.1/howto/deployment/"},{"docname":"howto/deployment/wsgi/index","title":"WSGI とともにデプロイするには","url":"/ja/5.1/howto/deployment/wsgi/"}],"prev":{"docname":"howto/deployment/wsgi/modwsgi","title":"Django を Apache と mod_wsgi とともに使うには？","url":"/ja/5.1/howto/deployment/wsgi/modwsgi/"},"next":{"docname":"howto/deployment/asgi/index","title":"ASGI とともにデプロイするには","url":"/ja/5.1/howto/deployment/asgi/"},"formats":{"html":"/ja/5.1/howto/deployment/wsgi/apache-auth/","markdown":"/ja/5.1/howto/deployment/wsgi/apache-auth.md","json":"/ja/5.1/howto/deployment/wsgi/apache-auth.json"},"source":"https://github.com/django/django/blob/stable/5.1.x/docs/howto/deployment/wsgi/apache-auth.txt","official":"https://docs.djangoproject.com/ja/5.1/howto/deployment/wsgi/apache-auth/","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"]}