AUTH 使用登录验证

from django.db import models
from django.contrib.auth.models import User, AbstractUser
class UserInfo(AbstractUser):  #AUTH_USER_MODEL = 'app01.UserInfo' 去数据库中看
    phone = models.CharField(max_length=11)
    addr = models.CharField(max_length=128)

***************************************************************************************************************

from django.shortcuts import render, redirect, HttpResponse

# Create your views here.

from django.contrib import auth
from django.contrib.auth.decorators import login_required

from app01 import models

def login(request):
    if request.method == "POST":
        username = request.POST.get("username")
        pwd = request.POST.get("password")
        # 如何判断用户名和密码对不对
        user = auth.authenticate(username=username, password=pwd)
        if user:
            ret = user.is_authenticated()
            print(ret)
            print("-" * 120)

            # 将登录的用户封装到request.user
            auth.login(request, user)
            return redirect("/index/")
    return render(request, "login.html")


@login_required  #注意要在setting 里加 LOGIN_URL = "/login/"
def index(request):
    print(request.user.username)
    print("=" * 120)
    ret = request.user.is_authenticated()
    print(ret)
    re = request.user
    return render(request, "index.html",{"user":re})

def logout(request):
    auth.logout(request)
    return redirect("/login/")

def register(request):

    user_obj = models.UserInfo.objects.create_user(username="alex5", password="alexdsb")
    # 校验密码是否正确
    ret = user_obj.check_password("alex1234")
    print(ret)
    # 修改密码
    user_obj.set_password("alex3714")
    user_obj.save()
    return HttpResponse("o98k")
Google Authenticator是一种基于时间的一次性密码算法(TOTP)实现的身份验证系统,它可以用于Java应用程序中实现登录认证。 下面是一个使用Google Authenticator的Java示例代码: 首先,你需要添以下依赖: ``` <dependency> <groupId>com.warrenstrange</groupId> <artifactId>googleauth</artifactId> <version>1.0.0</version> </dependency> ``` 然后,你可以使用以下代码生成一个基于时间的一次性密码: ```java import com.warrenstrange.googleauth.GoogleAuthenticator; import com.warrenstrange.googleauth.GoogleAuthenticatorConfig; import com.warrenstrange.googleauth.GoogleAuthenticatorConfig.GoogleAuthenticatorConfigBuilder; import com.warrenstrange.googleauth.GoogleAuthenticatorKey; import com.warrenstrange.googleauth.GoogleAuthenticatorQRGenerator; ... // 创建一个Google Authenticator配置对象 GoogleAuthenticatorConfigBuilder gacb = new GoogleAuthenticatorConfigBuilder(); GoogleAuthenticatorConfig config = gacb.build(); // 创建一个Google Authenticator对象 GoogleAuthenticator gAuth = new GoogleAuthenticator(config); // 生成一个秘钥 GoogleAuthenticatorKey key = gAuth.createCredentials(); // 打印秘钥 System.out.println("秘钥: " + key.getKey()); // 生成一个二维码URL String qrCodeUrl = GoogleAuthenticatorQRGenerator.getOtpAuthURL("My App", "user@example.com", key); System.out.println("二维码URL: " + qrCodeUrl); // 生成一个基于时间的一次性密码 int code = gAuth.getTotpPassword(key.getKey()); System.out.println("密码: " + code); ``` 在实际应用中,你可以将生成的二维码URL显示给用户,用户可以使用Google Authenticator应用程序扫描该二维码,然后输入生成的一次性密码进行登录认证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值