zoukankan      html  css  js  c++  java
  • 有参装饰器实现登录用户文件验证和验证失败锁定

    定义有参数装饰器为被装饰函数添加认证的功能:用户信息的来源可以是文件或者ldap,三次验证失败锁定用户。

    def input_msg():
        '''打印用户要输入的信息,
        返回用户名、密码'''
        username = input("username: ")
        password = input("password: ")
        return username, password
    
    def locked(username):
        '''从user_times.db文件中查询用户名,
        判断用户名是否已被锁定,
        锁定返回False,没锁定返回True'''
        flag_l = True
        with open("user_times.db",mode="r",encoding="utf8") as file:
            for i in file:
                if username == i:
                    flag_l = False
            if flag_l:
                return flag_l
            else:
                return flag_l
    
    error_name_dic = [] #存储用户名密码为错误的用户名的列表
    
    def instert(username):
        '''将用户名密码初错误的用户名插入列表,
        并判该用户名在列表中是否大于等于3次,
        大于等于将该用户名追加入user_times.db'''
        error_name_dic.append(username)
        count = 0
        for i in error_name_dic:
            if i == username:
                count += 1
            if count >= 3:
                with open("user_times.db", mode="a", encoding="utf8") as file:
                    file.write("
    ")
                    file.write(username)
    
    
    def login(username, password):
        '''从user.db中判断输入用户名、
        密码是够正确,正确返回True,
        不正确返回False'''
        flag = False
        error_name = ""
        with open("user.db", mode="r", encoding="utf8") as file:
            for i in file:
                i = eval(i)
                if username == i["username"] and password == str(i["password"]):
                    flag = True
                if username == i["username"] and password != str(i["password"]):
                    error_name = username
            if flag:
                return flag, error_name
            else:
                return flag, error_name
    
    def auth_out(auth_type):
        '''带参数的装饰器,返回auth'''
        def auth(func):
            '''@auth 等于 index = auth(index) ,返回warpper'''
            def warpper(*args, **kwargs):
                '''index实现用户认证'''
                if auth_type == "file":
                    while True:
                        username,password = input_msg()
                        flag_l = locked(username)
                        if flag_l:
                            flag, error_name = login(username, password)
                            if flag:
                                func(*args, **kwargs)
                                return func
                            else:
                                instert(error_name)
                                print("login error...")
                        else:
                            print("%s had been locked." % username)
                elif auth_type == "ldap":
                    print("We don't have ldap.")
            return warpper
        return auth
    
    @auth_out(auth_type = "file")
    def index():
        print("Welcome,this is index page.")
    
    index()
    
  • 相关阅读:
    Mongodb
    Java原子类
    volatile
    uniapp输入空格
    看不见的的html
    小程序隐藏scroll-view滚动条的方法
    云函数调用云函数 openid不存在
    vue路由中 Navigating to current location ("/xxx") is not allowed
    Vue: 单页面应用如何保持登录状态
    letter-spacing
  • 原文地址:https://www.cnblogs.com/sunqim16/p/6691298.html
Copyright © 2011-2022 走看看