美文网首页web服务器
注册加密及登录验证的解密

注册加密及登录验证的解密

作者: GHope | 来源:发表于2018-10-16 09:42 被阅读7次

    flask中对数据的加密使用generate_password_hashcheck_password_hash

    注册时的加密

    @user_blueprint.route('/register/', methods=['GET', 'POST'])
    def register():
        if request.method == 'GET':
            return render_template('register.html')
        if request.method == 'POST':
            username = request.form.get('username')
            password = request.form.get('password')
            if not all([username, password]):
                return render_template('register.html')
            # 保存注册信息
            user = User()
            user.username = username
    
            user.password = generate_password_hash(password)
            db.session.add(user)
            db.session.commit()
            return redirect(url_for('user.login'))
    

    登录时的解密

    @user_blueprint.route('/login/', methods=['GET', 'POST'])
    def login():
        if request.method == 'GET':
            return render_template('login.html')
    
        if request.method == 'POST':
            username = request.form.get('username')
            password = request.form.get('password')
            if not all([username, password]):
                return render_template('login.html')
    
            user = User.query.filter(User.username == username).first()
    
            if user and check_password_hash(user.password,password):
                session['login_status'] = 1
                return redirect(url_for('user.index'))
            else:
                return render_template('login.html')
    

    相关文章

      网友评论

        本文标题:注册加密及登录验证的解密

        本文链接:https://www.haomeiwen.com/subject/eowyaftx.html