---
title: "如何从 Apache 对 Django 的用户数据库进行认证"
version: 6.1
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/6.1/howto/deployment/wsgi/apache-auth/
canonical: https://djangodocs.dev/zh-hans/6.1/howto/deployment/wsgi/apache-auth/
---
# 如何从 Apache 对 Django 的用户数据库进行认证

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](/zh-hans/6.1/topics/auth/) directly. This
requires Apache version \>= 2.2 and `mod_wsgi` \>= 2.0. For example, you could:

- 仅为已授权的用户直接从 Apache 提供 static/media 文件。
- 仅为有特定权限的 Django 用户提供 [Subversion](https://subversion.apache.org/) 仓库访问。
- 允许某些用户连接到 [mod\_dav](https://httpd.apache.org/docs/current/mod/mod_dav.html) 创建的 WebDAV 共享。

> **Note**
>
> 若你已安装了一个 [自定义用户模型](/zh-hans/6.1/topics/auth/customizing/#auth-custom-user)，且想使用其默认认证处理器，它必须要支持 `is_active` 属性。若你想使用用户组授权，自定义用户必须有个关联名 'groups\`，指向一个拥有 'name' 字段的关联对象。若自定义的无法满足上述要求，你也可以指定自定义 mod\_wsgi 认证处理器。

## 用 `mod_wsgi` 进行授权认证

> **Note**
>
> 以下配置文件中的 `WSGIApplicationGroup %{GLOBAL}` 假定 Apache 实例仅运行了一个 Django 应用。若你运行了不止一个 Django 应用，请参考 mod\_wigi 文档的 [定义应用集群](https://modwsgi.readthedocs.io/en/develop/user-guides/configuration-guidelines.html#defining-application-groups) 章节获取更多配置信息。

确保 mod\_wsgi 已经安装并激活，并且你已经按照步骤设置了 [Apache 的 mod\_wsgi](/zh-hans/6.1/howto/deployment/wsgi/modwsgi/)。

然后，编辑 Apache 配置，添加只允许授权用户查看的位置：

```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>
```

`WSGIAuthUserScript` 指令告诉 mod\_wsgi 在指定 wsgi 脚本中执行 `check_password` 函数，并传递从提示符获取的用户名和密码。在本例中， `WSGIAuthUserScript` 与 `WSGIScriptAlias` 一样，后者 [由 django-admin startproject 创建](/zh-hans/6.1/howto/deployment/wsgi/)，定义了应用。

> **Using Apache 2.2+ with authentication**
>
> 确认 `mod_auth_basic` 和 `mod_authz_user` 被加载了。
>
> 这些可能被静态编译进 Apache，或者用 LoadModule 在 `httpd.conf` 动态加载它们：
>
> ```apache
> LoadModule auth_basic_module modules/mod_auth_basic.so
> LoadModule authz_user_module modules/mod_authz_user.so
> ```

最后，编辑 WSGI 脚本 `mysite.wsgi`，通过导入 `check_password` 函数，将 Apache 的认证授权机制接续在你站点的授权机制之后:

```
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()
```

以 `/secret/` 开头的请求现在会要求用户认证。

mod\_wsgi [可达性控制机制文档](https://modwsgi.readthedocs.io/en/develop/user-guides/access-control-mechanisms.html) 提供了其它授权机制和方法的更多细节和信息。

### 利用 `mod_wsgi` 和 Django 用户组(groups)进行授权

mod\_wsgi 也提供了将组成员限制至特定路径的功能。

在本例中，Apache 配置应该看起来像这样：

```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
```

对 `/secret` 的请求现在也会要求用户是 "secret-agents" 用户组的成员。
