{"title":"Django中的用户认证","version":"4.2","locale":"zh-hans","docname":"topics/auth/index","url":"/zh-hans/4.2/topics/auth/","canonical":"https://djangodocs.dev/zh-hans/4.2/topics/auth/","summary":"Django 自带一个用户验证系统。它负责处理用户账号、组、权限和基于cookie的用户会话。文档的这部分解释了默认的实现如何开箱即用，以及如何扩展和自定义以满足你的项目需求。 概况 Link to this heading # Django…","html":"<h1>Django中的用户认证<a class=\"heading-anchor\" href=\"#user-authentication-in-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<div class=\"toctree-wrapper compound\">\n</div>\n<p id=\"module-django.contrib.auth\">Django 自带一个用户验证系统。它负责处理用户账号、组、权限和基于cookie的用户会话。文档的这部分解释了默认的实现如何开箱即用，以及如何扩展和自定义以满足你的项目需求。</p>\n<section id=\"overview\">\n<h2>概况<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django 验证系统处理验证和授权。简单来说，验证检验用户是否是他们的用户，授权决定已验证用户能做什么。这里的术语验证用于指代这两个任务。</p>\n<p>认证系统由以下部分组成：</p>\n<ul class=\"simple\">\n<li><p>用户</p></li>\n<li><p>权限：二进制（是/否）标识指定用户是否可以执行特定任务。</p></li>\n<li><p>组：将标签和权限应用于多个用户的一般方法。</p></li>\n<li><p>可配置的密码哈希化系统</p></li>\n<li><p>为登录用户或限制内容提供表单和视图工具</p></li>\n<li><p>可插拔的后端系统</p></li>\n</ul>\n<p>Django 里的验证系统旨在通用化，不提供一些常见的 web 验证系统的特性。其中一些常见问题的解决方案已在第三方包中实现。</p>\n<ul class=\"simple\">\n<li><p>密码强度检查</p></li>\n<li><p>限制登录尝试</p></li>\n<li><p>针对第三方的身份验证（例如OAuth）</p></li>\n<li><p>对象级权限</p></li>\n</ul>\n</section>\n<section id=\"installation\">\n<h2>安装<a class=\"heading-anchor\" href=\"#installation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>验证系统被捆绑为 <code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.auth</span></code> 的 Django contrib 模块。默认情况下，所需的配置以及包含在 <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">django-admin</span> <span class=\"pre\">startproject</span></code></a> 生成的 <code class=\"file docutils literal notranslate\"><span class=\"pre\">settings.py</span></code> 中，在 <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> 配置列出了以下两个条目：</p>\n<ol class=\"arabic simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'django.contrib.auth'</span></code> 包含了验证框架的内核和它的默认模型。</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'django.contrib.contenttypes'</span></code> 是 Django <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/contrib/contenttypes/\"><span class=\"doc\">content type system</span></a> ，允许你创建的模型和权限相关联。</p></li>\n</ol>\n<p>这些条目在你的 <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a> 设置中：</p>\n<ol class=\"arabic simple\">\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.2/ref/middleware/#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> 通过请求管理 <a class=\"reference internal\" href=\"/zh-hans/4.2/topics/http/sessions/\"><span class=\"doc\">sessions</span></a> 。</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.2/ref/middleware/#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></li>\n</ol>\n<p>有了这些设置，运行命令 <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span> <span class=\"pre\">migrate</span></code> 为auth相关模型创建必要的数据表，并为已安装应用中定义的任何模型创建许可。</p>\n</section>\n<section id=\"usage\">\n<h2>用法<a class=\"heading-anchor\" href=\"#usage\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><a class=\"reference internal\" href=\"/zh-hans/4.2/topics/auth/default/\"><span class=\"doc\">使用 Django 的默认实现</span></a></p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.2/topics/auth/default/#user-objects\"><span class=\"std std-ref\">使用 User 对象</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.2/topics/auth/default/#topic-authorization\"><span class=\"std std-ref\">权限和认证</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.2/topics/auth/default/#auth-web-requests\"><span class=\"std std-ref\">请求中的认证</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.2/topics/auth/default/#auth-admin\"><span class=\"std std-ref\">在管理系统中管理用户</span></a></p></li>\n</ul>\n<p><a class=\"reference internal\" href=\"/zh-hans/4.2/ref/contrib/auth/\"><span class=\"doc\">默认实现的 API 参考</span></a></p>\n<p><a class=\"reference internal\" href=\"/zh-hans/4.2/topics/auth/customizing/\"><span class=\"doc\">自定义 Users 和认证</span></a></p>\n<p><a class=\"reference internal\" href=\"/zh-hans/4.2/topics/auth/passwords/\"><span class=\"doc\">Django 中的密码管理</span></a></p>\n</section>","rootId":"user-authentication-in-django","toc":[{"title":"概况","anchor":"overview","children":[]},{"title":"安装","anchor":"installation","children":[]},{"title":"用法","anchor":"usage","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/4.2/topics/"}],"prev":{"docname":"topics/testing/advanced","title":"进阶测试主题","url":"/zh-hans/4.2/topics/testing/advanced/"},"next":{"docname":"topics/auth/default","title":"使用 Django 的验证系统","url":"/zh-hans/4.2/topics/auth/default/"},"formats":{"html":"/zh-hans/4.2/topics/auth/","markdown":"/zh-hans/4.2/topics/auth.md","json":"/zh-hans/4.2/topics/auth.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/topics/auth/index.txt","official":"https://docs.djangoproject.com/zh-hans/4.2/topics/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"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}