---
title: "Hur man autentiserar sig mot Djangos användardatabas från Apache"
version: 6.1
locale: sv
source: https://docs.djangoproject.com/sv/6.1/howto/deployment/wsgi/apache-auth/
canonical: https://djangodocs.dev/sv/6.1/howto/deployment/wsgi/apache-auth/
---
# Hur man autentiserar sig mot Djangos användardatabas från Apache

Since keeping multiple authentication databases in sync is a common problem
when dealing with Apache, you can configure Apache to authenticate against
Django’s [authentication system](/sv/6.1/topics/auth/) directly. This
requires Apache version \>= 2.2 and `mod_wsgi` \>= 2.0. For example, you could:

- Servera statiska filer/mediefiler direkt från Apache endast till autentiserade användare.
- Autentisera åtkomst till ett [Subversion](https://subversion.apache.org/)-repository mot Django-användare med en viss behörighet.
- Tillåt vissa användare att ansluta till en WebDAV-delning som skapats med [mod\_dav](https://httpd.apache.org/docs/current/mod/mod_dav.html).

> **Note**
>
> Om du har installerat en [custom user model](/sv/6.1/topics/auth/customizing/#auth-custom-user) och vill använda denna standardautentiseringshanterare måste den stödja ett `is_active`-attribut. Om du vill använda gruppbaserad auktorisering måste din anpassade användare ha en relation med namnet ”groups”, som hänvisar till ett relaterat objekt som har ett ”name”-fält. Du kan också ange din egen anpassade mod\_wsgi auth-hanterare om din anpassade inte kan uppfylla dessa krav.

## Autentisering med `mod_wsgi`

> **Note**
>
> Användningen av `WSGIApplicationGroup %{GLOBAL}` i konfigurationerna nedan förutsätter att din Apache-instans endast kör en Django-applikation. Om du kör mer än en Django-applikation, se avsnittet [Defining Application Groups](https://modwsgi.readthedocs.io/en/develop/user-guides/configuration-guidelines.html#defining-application-groups) i mod\_wsgi-dokumenten för mer information om den här inställningen.

Se till att mod\_wsgi är installerat och aktiverat och att du har följt stegen för att konfigurera [Apache med mod\_wsgi](/sv/6.1/howto/deployment/wsgi/modwsgi/).

Redigera sedan Apache-konfigurationen så att du lägger till en plats som endast autentiserade användare ska kunna se:

```apache
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com

WSGIProcessGroup %{GLOBAL}
WSGIApplicationGroup %{GLOBAL}

<Location "/secret">
    AuthType Basic
    AuthName "Top Secret"
    Require valid-user
    AuthBasicProvider wsgi
    WSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py
</Location>
```

Direktivet `WSGIAuthUserScript` säger till mod\_wsgi att köra funktionen `check_password` i det angivna wsgi-skriptet och skicka användarnamnet och lösenordet som det får från prompten. I det här exemplet är `WSGIAuthUserScript` detsamma som `WSGIScriptAlias` som definierar din applikation [som skapas av django-admin startproject](/sv/6.1/howto/deployment/wsgi/).

> **Using Apache 2.2+ with authentication**
>
> Se till att `mod_auth_basic` och `mod_authz_user` är laddade.
>
> Dessa kan vara statiskt kompilerade i Apache, eller så kan du behöva använda LoadModule för att ladda dem dynamiskt i din `httpd.conf`:
>
> ```apache
> LoadModule auth_basic_module modules/mod_auth_basic.so
> LoadModule authz_user_module modules/mod_authz_user.so
> ```

Slutligen redigerar du ditt WSGI-skript `mysite.wsgi` för att knyta Apaches autentisering till din webbplats autentiseringsmekanismer genom att importera funktionen `check_password`:

```
import os

os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"

from django.contrib.auth.handlers.modwsgi import check_password

from django.core.handlers.wsgi import WSGIHandler

application = WSGIHandler()
```

Förfrågningar som börjar med `/secret/` kommer nu att kräva att en användare autentiserar sig.

I mod\_wsgi [access control mechanisms documentation](https://modwsgi.readthedocs.io/en/develop/user-guides/access-control-mechanisms.html) finns ytterligare detaljer och information om alternativa metoder för autentisering.

### Auktorisering med `mod_wsgi` och Django-grupper

mod\_wsgi tillhandahåller också funktionalitet för att begränsa en viss plats till medlemmar i en grupp.

I det här fallet bör Apache-konfigurationen se ut så här:

```apache
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py

WSGIProcessGroup %{GLOBAL}
WSGIApplicationGroup %{GLOBAL}

<Location "/secret">
    AuthType Basic
    AuthName "Top Secret"
    AuthBasicProvider wsgi
    WSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py
    WSGIAuthGroupScript /path/to/mysite.com/mysite/wsgi.py
    Require group secret-agents
    Require valid-user
</Location>
```

To support the `WSGIAuthGroupScript` directive, the same WSGI script
`mysite.wsgi` must also import the `groups_for_user` function which
returns a list of groups the given user belongs to.

```python
from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user
```

Förfrågningar om `/secret/` kommer nu också att kräva att användaren är medlem i gruppen ”secret-agents”.
