zoukankan      html  css  js  c++  java
  • Django自带的用户认证auth模块

    一、介绍

    复制代码
    基本上在任何网站上,都无可避免的需要设计实现网站的用户系统。此时我们需要实现包括用户注册、用户登录、用户认证、注销、修改密码等功能。
    
    使用Django,我们可以不需要自己写这些功能,因为Django内置了强大的用户认证系统--auth,它默认使用 auth_user 表来存储用户数据。
    
    from django.contrib import auth
    
    注意:命令行创建超级用户 python manage.py createsuperuser
    复制代码

    二、authenticate() 

    复制代码
    提供了用户认证功能,即验证用户名以及密码是否正确,一般需要username 、password两个关键字参数。
    如果认证成功(用户名和密码正确有效),便会返回一个 User 对象。
    
    authenticate()会在该 User 对象上设置一个属性(id)来标识后端已经认证了该用户,且该信息在后续的登录过程中是需要的。
    
    用法:
    user_obj = authenticate(username=username,password=pwd)
    复制代码

    三、login(request, user)

    复制代码
    该函数接受一个HttpRequest对象,以及一个经过认证的User对象。
    该函数实现一个用户登录的功能。它本质上会在后端为该用户生成相关session数据。
    
    用法:auth.login(request, user_obj)
    
    示例:
    from django.shortcuts import render, HttpResponse, redirect
    from django.contrib import auth
    
    def login(request):
        if request.method == "POST":
            username = request.POST.get('username')
            pwd = request.POST.get('password')
            # 调用auth模块的认证方法,判断用户名和密码是否正确,正确返回一个user_obj
            user_obj = auth.authenticate(username=username, password=pwd)
            if user_obj:
                # 登录成功,设置Session数据
                auth.login(request, user_obj)
                return HttpResponse('登录成功')
            else:
                return render(request, 'login.html', {'error_msg': '用户名或者密码错误'})
        return render(request, 'login.html')
    
    注意:
    只要使用login(request, user_obj)之后,request.user就能拿到当前登录的用户对象。否则request.user得到的是一个匿名用户对象(AnonymousUser Object)。
    详细原理请查看 AuthenticationMiddleware 中间件源码。
    复制代码

    四、logout(request) 

    复制代码
    该函数接受一个HttpRequest对象,无返回值。
    当调用该函数时,当前请求的session信息会全部清除。该用户即使没有登录,使用该函数也不会报错。
    
    用法:
    
    from django.contrib import auth
       
    def logout(request):
        auth.logout(request)
        return redirect('/login/')
    复制代码

    五、login_requierd()

    复制代码
    auth 给我们提供的一个装饰器工具,用来快捷的给某个视图添加登录校验。
    
    用法:
    from django.contrib.auth.decorators import login_required
          
    @login_required
    def index(request):
        return render(request, 'index.html')
    
    若用户没有登录,则会跳转到django默认的登录URL '/accounts/login/ ' 并传递当前访问url的绝对路径 (登陆成功后,会重定向到该路径)。
    如果需要自定义登录的URL,则需要在settings.py文件中通过LOGIN_URL进行修改。
    
    示例:
    LOGIN_URL = '/login/'  # 这里配置成你项目登录页面的路由
    复制代码

    六、create_user()

    复制代码
    auth 提供的一个创建新用户的方法,需要提供必要参数(username、password)等。
    
    用法:
    from django.contrib.auth.models import User
    user = User.objects.create_user(username='用户名',password='密码',email='邮箱',...)
    
    
    示例:
    def reg(request):
        if request.method == 'POST':
            username = request.POST.get('username')
            pwd = request.POST.get('password')
            # 假设数据都经过有效性校验了
            # 去数据库创建一条记录
            User.objects.create_user(username=username, password=pwd)  # create_user创建普通用户
            # User.objects.create_superuser(username=username, password=pwd)  # create_superuser创建超级用户
            # 创建成功,跳转登录页年
            return redirect('/login/')
    
        return render(request, 'reg.html')
    复制代码

    七、create_superuser()

    复制代码
    auth 提供的一个创建新的超级用户的方法,需要提供必要参数(username、password)等。
    
    用法:
    from django.contrib.auth.models import User
    user_obj = User.objects.create_superuser(username='用户名',password='密码',email='邮箱',...)
    复制代码

    八、check_password(raw_password)

    复制代码
    auth 提供的一个检查密码是否正确的方法,需要提供当前请求用户的密码。
    密码正确返回True,否则返回False。
    
    用法:
    ok = user_obj.check_password('密码')
    或者直接针对当前请求的user对象校验原密码是否正确:
    ok = request.user.check_password(raw_password='原密码')
    复制代码

    九、set_password(raw_password)

    复制代码
    auth 提供的一个修改密码的方法,接收要设置的新密码 作为参数。
    注意:设置完一定要调用用户对象的save方法!!!
    
    用法:
    user_obj.set_password('新密码')
    user_obj.save()
    复制代码

    十、is_authenticated()

    复制代码
    用来判断当前请求是否通过了认证。
    
    用法:
    
    def my_view(request):
      if not request.user.is_authenticated():
        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
    复制代码

    十一、用户对象的属性

    复制代码
    从user_obj = request.user可以拿到当前登录的用户对象,用户对象user_obj能够拿到认证所用用户表的数据属性,比如username, password等。
    
    其他常用属性含义如下:
    is_staff : 用户是否拥有网站的管理权限.
    
    is_active : 是否允许用户登录, 设置为 False,可以在不删除用户的前提下禁止用户登录。
    复制代码

    十二、如何拓展auth默认的表

    复制代码
    1、介绍
    虽然内置的认证系统很好用,但是auth_user表字段都是固定的那几个,如果我想要加一个存储用户手机号的字段怎么办?
    方法一:新建另外一张表然后通过一对一和内置的auth_user表关联
    方法二:通过继承内置的AbstractUser类,来定义一个自己的Model类。
    这样既能根据项目需求灵活的设计用户表,又能使用Django强大的认证系统了。
    
    2、示例:
    models.py
    from django.db import models
    from django.contrib.auth.models import AbstractUser
    
    
    class UserInfo(AbstractUser):
        # 这里定义拓展的字段
        gender = models.PositiveIntegerField(choices=((0, ''),(1, ''), (2, '保密')))
        phone = models.CharField(max_length=11)
    
    3、注意:
    按上面的方式扩展了内置的auth_user表之后,一定要在settings.py中告诉Django,我现在使用我新定义的UserInfo表来做用户认证。写法如下:
    # 引用Django自带的User表,继承使用时需要设置
    AUTH_USER_MODEL = "app名.UserInfo"
    
    
    4、自定义认证系统默认使用的数据表之后,我们就可以像使用默认的auth_user表那样使用我们的UserInfo表了。比如:
    
    创建普通用户:
    UserInfo.objects.create_user(username='用户名', password='密码')
    
    创建超级用户:
    UserInfo.objects.create_superuser(username='用户名', password='密码')
    
    
    5、再次注意:
    一旦我们指定了新的认证系统所使用的表,我们就需要重新在数据库中创建该表,而不能继续使用原来默认的auth_user表了。
    默认的auth_user表就会被你自定义的表替代,即auth_user表不存在了,我们使用auth模块的方法,修改的就是新的表。
    继承AbstractUser是在User表的基础上拓展字段,User表原来的字段还是有的,但是如果我就不想要User表的某些字段,而且还要拓展我需要的字段呢?
    怎么办?就需要继承AbstractBaseUser, PermissionsMixin重写一个表,表内的字段完全是你设计的字段
    from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
    
    # 定义UserProfile这个类的管理类
    class UserManager(BaseUserManager):
        use_in_migrations = True
    
        def _create_user(self, email, password, **extra_fields):
            """
            Creates and saves a User with the given email and password.
            """
            if not email:
                raise ValueError('The given email must be set')
            email = self.normalize_email(email)
            user = self.model(email=email, **extra_fields)  # 创建对象
            user.set_password(password)  # 把密码加密之后再写入数据库
            user.save(using=self._db)  # 保存到数据库
            return user
    
        def create_user(self, email, password=None, **extra_fields):
            extra_fields.setdefault('is_staff', False)  # 给字典设置默认值
            extra_fields.setdefault('is_superuser', False)
            return self._create_user(email, password, **extra_fields)
    
        def create_superuser(self, email, password, **extra_fields):
            extra_fields.setdefault('is_staff', True)
            extra_fields.setdefault('is_superuser', True)
    
            if extra_fields.get('is_staff') is not True:
                raise ValueError('Superuser must have is_staff=True.')
            if extra_fields.get('is_superuser') is not True:
                raise ValueError('Superuser must have is_superuser=True.')
            return self._create_user(email, password, **extra_fields)
    
    
    class UserProfile(AbstractBaseUser, PermissionsMixin):
        email = models.EmailField(
            max_length=255,
            unique=True,
            validators=[RegexValidator(r'^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*.[a-zA-Z0-9]{2,6}$', '邮箱格式不正确'),]
        )
        is_staff = models.BooleanField(
            _('staff status'),
            default=False,
            help_text=_('Designates whether the user can log into this admin site.'),
        )
        is_active = models.BooleanField(
            _('active'),
            default=True,
            help_text=_(
                'Designates whether this user should be treated as active. '
                'Unselect this instead of deleting accounts.'
            ),
        )
        name = models.CharField('名字', max_length=32)
        department = models.ForeignKey('Department', default=None, blank=True, null=True)
        mobile = models.CharField(
            '手机',
            max_length=32,
            default=None,
            blank=True,
            null=True,
            validators=[RegexValidator(r'^1[3-9]d{9}$', '手机格式不正确')]
        )
    
        memo = models.TextField('备注', blank=True, null=True, default=None)
        date_joined = models.DateTimeField(auto_now_add=True)
    
        EMAIL_FIELD = 'email'  # 发送邮件的字段
        USERNAME_FIELD = 'email'  # 用来唯一确定auth中的用户
        REQUIRED_FIELDS = ['name']  # auth指定除了上面两个配置项的字段还有哪些字段需要必填
    
        class Meta:
            verbose_name = '账户信息'
            verbose_name_plural = "账户信息"
    
        def clean(self):
            super(UserProfile, self).clean()
            # 对邮件进行校验
            self.email = self.__class__.objects.normalize_email(self.email)
    
        def get_full_name(self):
            # The user is identified by their email address
            return self.name
    
        def get_short_name(self):
            # The user is identified by their email address
            return self.email
    
        def __str__(self):  # __unicode__ on Python 2
            return self.name
    
        # 给ORM添加管理类
        objects = UserManager()
    View Code
    
    
    复制代码

    十三、修改密码示例

    @login_required
    def set_password(request):
        user = request.user
        err_msg = ''
        if request.method == 'POST':
            old_password = request.POST.get('old_password')
            new_password = request.POST.get('new_password')
            re_password = request.POST.get('re_password')
            # 检查旧密码是否正确
            if user.check_password(old_password):
                if not new_password:
                    err_msg = '新密码不能为空'
                elif new_password != re_password:
                    err_msg = '两次密码不一致'
                else:
                    user.set_password(new_password)
                    user.save()
                    return redirect("/login/")
            else:
                err_msg = '原密码输入错误'
        return render(request, 'set_password.html', {'err_msg': err_msg})
    View Code
  • 相关阅读:
    策略模式
    装饰模式
    责任链模式
    算法_快速排序
    算法_二分查找
    数据结构学习笔记_表
    集合遍历remove时ConcurrentModificationException异常
    LinkedHashMap笔记
    Windows相关命令
    高效率幂运算
  • 原文地址:https://www.cnblogs.com/yidashi110/p/10091848.html
Copyright © 2011-2022 走看看