• 密码保护


    1.更新User对象,设置对内的_password

    class User(db.Model):

        __tablename__ = 'user' 

        _password = db.Column(db.String(200), nullable=False) #内部使用

    class User(db.Model):
        __tablename__ = 'user_test'
        id=db.Column(db.Integer,primary_key=True,autoincrement=True)
        username=db.Column(db.String(20),nullable=False)
        _password = db.Column(db.String(200), nullable=False)
        nickname = db.Column(db.String(50))

    2.编写对外的password

    from werkzeug.security import generate_password_hash, check_password_hash

        @property

        def password(self):  #外部使用,取值

            return self._password

        @password.setter

        def password(self, row_password):#外部使用,赋值

            self._password = generate_password_hash(row_password)

    from werkzeug.security import generate_password_hash,check_password_hash
    
    app = Flask(__name__)
    #app.config['SECRET_KEY'] = '1234567'
    app.config.from_object(config)
    db = SQLAlchemy(app)
    
    class User(db.Model):
        __tablename__ = 'user_test'
        id=db.Column(db.Integer,primary_key=True,autoincrement=True)
        username=db.Column(db.String(20),nullable=False)
        _password = db.Column(db.String(200), nullable=False)
        nickname = db.Column(db.String(50))
    
    
        @property
        def password(self):
            return self._password
    
        @password.setter
        def password(self,row_password):
            self._password=generate_password_hash(row_password)
    
    

    3.密码验证的方法:

        def check_password(self, row_password): #密码验证

            result = check_password_hash(self._password,row_password)

            return result

      def check_password(self,row_password):
            result=check_password_hash(self._password,row_password)
            return result

    4.登录验证:

            password1 = request.form.get('password')

            user = User.query.filter(User.username == username).first()

            if user:

                if user.check_password(password1):

    @app.route('/login/',methods=['GET','POST'])
    def login():
        if request.method == 'GET':
            return render_template('login.html')
        else:
            username = request.form.get('username')
            password = request.form.get('password')
            user = User.query.filter(User.username==username).first()
            if user:
                if user.check_password(password):
                    session['user'] = username;
                    session['userid']=user.id
                    session.parmanent=True
                    return redirect(url_for('index'))
                else:
                    return 'password error!'
            else:
                return u'error username'
  • 相关阅读:
    dynamics crm 2011升级过程中发现的一些问题
    报价单上的“创建订单”按钮Disabled
    C/C++ Qt TableDelegate 自定义代理组件
    C/C++ Qt TabWidget 实现多窗体创建
    C/C++ Qt MdiArea 多窗体组件应用
    C/C++ Qt Tree与Tab组件实现分页菜单
    C/C++ Qt ListWidget 增加右键菜单
    C/C++ Qt StatusBar 底部状态栏应用
    C/C++ Qt TableWidget 表格组件应用
    VS2012 + Qt 4.8.3 + OpenSSL
  • 原文地址:https://www.cnblogs.com/123hyf/p/8085033.html
Copyright © 2020-2023  润新知