{"title":"Authenticating against Django’s user database from Apache","version":"3.2","locale":"it","docname":"howto/deployment/wsgi/apache-auth","url":"/it/3.2/howto/deployment/wsgi/apache-auth/","canonical":"https://djangodocs.dev/it/3.2/howto/deployment/wsgi/apache-auth/","summary":"Since keeping multiple authentication databases in sync is a common problem when dealing with Apache, you can configure Apache to authenticate against Django’s…","html":"<h1>Authenticating against Django’s user database from Apache<a class=\"heading-anchor\" href=\"#authenticating-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>Since keeping multiple authentication databases in sync is a common problem when\ndealing with Apache, you can configure Apache to authenticate against Django’s\n<a class=\"reference internal\" href=\"/it/3.2/topics/auth/\"><span class=\"doc\">authentication system</span></a> directly. This requires Apache\nversion &gt;= 2.2 and mod_wsgi &gt;= 2.0. For example, you could:</p>\n<ul class=\"simple\">\n<li><p>Serve static/media files directly from Apache only to authenticated users.</p></li>\n<li><p>Authenticate access to a <a class=\"reference external\" href=\"https://subversion.apache.org/\">Subversion</a> repository against Django users with\na certain permission.</p></li>\n<li><p>Allow certain users to connect to a WebDAV share created with <a class=\"reference external\" href=\"https://httpd.apache.org/docs/2.2/mod/mod_dav.html\">mod_dav</a>.</p></li>\n</ul>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>If you have installed a <a class=\"reference internal\" href=\"/it/3.2/topics/auth/customizing/#auth-custom-user\"><span class=\"std std-ref\">custom user model</span></a> and\nwant to use this default auth handler, it must support an <code class=\"docutils literal notranslate\"><span class=\"pre\">is_active</span></code>\nattribute. If you want to use group based authorization, your custom user\nmust have a relation named “groups”, referring to a related object that has\na “name” field. You can also specify your own custom mod_wsgi\nauth handler if your custom cannot conform to these requirements.</p>\n</aside>\n<section id=\"authentication-with-mod-wsgi\">\n<h2>Autenticazione con <code class=\"docutils literal notranslate\"><span class=\"pre\">mod_wsgi</span></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\">Nota</p>\n<p>The use of <code class=\"docutils literal notranslate\"><span class=\"pre\">WSGIApplicationGroup</span> <span class=\"pre\">%{GLOBAL}</span></code> in the configurations below\npresumes that your Apache instance is running only one Django application.\nIf you are running more than one Django application, please refer to the\n<a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/user-guides/configuration-guidelines.html#defining-application-groups\">Defining Application Groups</a> section of the mod_wsgi docs for more\ninformation about this setting.</p>\n</aside>\n<p>Make sure that mod_wsgi is installed and activated and that you have\nfollowed the steps to setup <a class=\"reference internal\" href=\"/it/3.2/howto/deployment/wsgi/modwsgi/\"><span class=\"doc\">Apache with mod_wsgi</span></a>.</p>\n<p>Next, edit your Apache configuration to add a location that you want\nonly authenticated users to be able to view:</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=\"w\"> </span>/<span class=\"w\"> </span><span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n<span class=\"nb\">WSGIPythonPath</span><span class=\"w\"> </span><span class=\"sx\">/path/to/mysite.com</span>\n\n<span class=\"nb\">WSGIProcessGroup</span><span class=\"w\"> </span>%{GLOBAL}\n<span class=\"nb\">WSGIApplicationGroup</span><span class=\"w\"> </span>%{GLOBAL}\n\n<span class=\"nt\">&lt;Location</span><span class=\"w\"> </span><span class=\"s\">&quot;/secret&quot;</span><span class=\"nt\">&gt;</span>\n<span class=\"w\">    </span><span class=\"nb\">AuthType</span><span class=\"w\"> </span>Basic\n<span class=\"w\">    </span><span class=\"nb\">AuthName</span><span class=\"w\"> </span><span class=\"s2\">&quot;Top Secret&quot;</span>\n<span class=\"w\">    </span><span class=\"nb\">Require</span><span class=\"w\"> </span>valid-user\n<span class=\"w\">    </span><span class=\"nb\">AuthBasicProvider</span><span class=\"w\"> </span>wsgi\n<span class=\"w\">    </span><span class=\"nb\">WSGIAuthUserScript</span><span class=\"w\"> </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>The <code class=\"docutils literal notranslate\"><span class=\"pre\">WSGIAuthUserScript</span></code> directive tells mod_wsgi to execute the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">check_password</span></code> function in specified wsgi script, passing the user name and\npassword that it receives from the prompt. In this example, the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">WSGIAuthUserScript</span></code> is the same as the <code class=\"docutils literal notranslate\"><span class=\"pre\">WSGIScriptAlias</span></code> that defines your\napplication <a class=\"reference internal\" href=\"/it/3.2/howto/deployment/wsgi/\"><span class=\"doc\">that is created by django-admin startproject</span></a>.</p>\n<aside class=\"admonition-using-apache-2-2-with-authentication admonition\">\n<p class=\"admonition-title\">Utilizzo di Apache 2.2 con autenticazione</p>\n<p>Controlla bene che  <code class=\"docutils literal notranslate\"><span class=\"pre\">mod_auth_basic</span></code> e <code class=\"docutils literal notranslate\"><span class=\"pre\">mod_authz_user</span></code> siano caricati</p>\n<p>These might be compiled statically into Apache, or you might need to use\nLoadModule to load them dynamically in your <code class=\"docutils literal notranslate\"><span class=\"pre\">httpd.conf</span></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><span class=\"w\"> </span>auth_basic_module<span class=\"w\"> </span>modules/mod_auth_basic.so\n<span class=\"nb\">LoadModule</span><span class=\"w\"> </span>authz_user_module<span class=\"w\"> </span>modules/mod_authz_user.so\n</code></pre></div>\n</aside>\n<p>Finally, edit your WSGI script <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite.wsgi</span></code> to tie Apache’s authentication\nto your site’s authentication mechanisms by importing the <code class=\"docutils literal notranslate\"><span class=\"pre\">check_password</span></code>\nfunction:</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=\"w\"> </span><span class=\"nn\">os</span>\n\n<span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">environ</span><span class=\"p\">[</span><span class=\"s1\">&#39;DJANGO_SETTINGS_MODULE&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;mysite.settings&#39;</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.auth.handlers.modwsgi</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">check_password</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.handlers.wsgi</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">WSGIHandler</span>\n<span class=\"n\">application</span> <span class=\"o\">=</span> <span class=\"n\">WSGIHandler</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>Le richieste che iniziano con  <code class=\"docutils literal notranslate\"><span class=\"pre\">/secret/</span></code> richiederanno un utente per l’autenticazione</p>\n<p>The mod_wsgi <a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/user-guides/access-control-mechanisms.html\">access control mechanisms documentation</a> provides additional\ndetails and information about alternative methods of authentication.</p>\n<section id=\"authorization-with-mod-wsgi-and-django-groups\">\n<h3>Autorizzazione con <code class=\"docutils literal notranslate\"><span class=\"pre\">mod_wsgi</span></code> e gruppi 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 fornisce anche funzionalita di restringere una location particolare a un membro del gruppo.</p>\n<p>In questo caso, la configurazione di Apache dovrebbe essere cosi :</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=\"w\"> </span>/<span class=\"w\"> </span><span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n\n<span class=\"nb\">WSGIProcessGroup</span><span class=\"w\"> </span>%{GLOBAL}\n<span class=\"nb\">WSGIApplicationGroup</span><span class=\"w\"> </span>%{GLOBAL}\n\n<span class=\"nt\">&lt;Location</span><span class=\"w\"> </span><span class=\"s\">&quot;/secret&quot;</span><span class=\"nt\">&gt;</span>\n<span class=\"w\">    </span><span class=\"nb\">AuthType</span><span class=\"w\"> </span>Basic\n<span class=\"w\">    </span><span class=\"nb\">AuthName</span><span class=\"w\"> </span><span class=\"s2\">&quot;Top Secret&quot;</span>\n<span class=\"w\">    </span><span class=\"nb\">AuthBasicProvider</span><span class=\"w\"> </span>wsgi\n<span class=\"w\">    </span><span class=\"nb\">WSGIAuthUserScript</span><span class=\"w\"> </span><span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n<span class=\"w\">    </span><span class=\"nb\">WSGIAuthGroupScript</span><span class=\"w\"> </span><span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n<span class=\"w\">    </span><span class=\"nb\">Require</span><span class=\"w\"> </span><span class=\"k\">group</span><span class=\"w\"> </span>secret-agents\n<span class=\"w\">    </span><span class=\"nb\">Require</span><span class=\"w\"> </span>valid-user\n<span class=\"nt\">&lt;/Location&gt;</span>\n</code></pre></div>\n<p>To support the <code class=\"docutils literal notranslate\"><span class=\"pre\">WSGIAuthGroupScript</span></code> directive, the same WSGI script\n<code class=\"docutils literal notranslate\"><span class=\"pre\">mysite.wsgi</span></code> must also import the <code class=\"docutils literal notranslate\"><span class=\"pre\">groups_for_user</span></code> function which\nreturns a list groups the given user belongs to.</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=\"w\"> </span><span class=\"nn\">django.contrib.auth.handlers.modwsgi</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">check_password</span><span class=\"p\">,</span> <span class=\"n\">groups_for_user</span>\n</code></pre></div>\n<p>Requests for <code class=\"docutils literal notranslate\"><span class=\"pre\">/secret/</span></code> will now also require user to be a member of the\n«secret-agents» group.</p>\n</section>\n</section>","rootId":"authenticating-against-django-s-user-database-from-apache","toc":[{"title":"Autenticazione con mod_wsgi","anchor":"authentication-with-mod-wsgi","children":[{"title":"Autorizzazione con mod_wsgi e gruppi Django","anchor":"authorization-with-mod-wsgi-and-django-groups","children":[]}]}],"breadcrumbs":[{"docname":"howto/index","title":"«How-to» guides","url":"/it/3.2/howto/"},{"docname":"howto/deployment/index","title":"Deploying Django","url":"/it/3.2/howto/deployment/"},{"docname":"howto/deployment/wsgi/index","title":"How to deploy with WSGI","url":"/it/3.2/howto/deployment/wsgi/"}],"prev":{"docname":"howto/deployment/wsgi/modwsgi","title":"How to use Django with Apache and mod_wsgi","url":"/it/3.2/howto/deployment/wsgi/modwsgi/"},"next":{"docname":"howto/deployment/asgi/index","title":"How to deploy with ASGI","url":"/it/3.2/howto/deployment/asgi/"},"formats":{"html":"/it/3.2/howto/deployment/wsgi/apache-auth/","markdown":"/it/3.2/howto/deployment/wsgi/apache-auth.md","json":"/it/3.2/howto/deployment/wsgi/apache-auth.json"},"source":"https://github.com/django/django/blob/stable/3.2.x/docs/howto/deployment/wsgi/apache-auth.txt","official":"https://docs.djangoproject.com/it/3.2/howto/deployment/wsgi/apache-auth/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}