密码保护
Posted 学无止境真难
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了密码保护相关的知识,希望对你有一定的参考价值。
1.更新User对象,设置对内的_password
class User(db.Model):
__tablename__ = \'user\'
_password = db.Column(db.String(200), nullable=False) #内部使用
class User(db.Model): __tablename__ = \'user\' 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
@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):
def login(): if request.method == \'GET\': return render_template(\'login.html\') else: usern = request.form.get(\'username\') passw = request.form.get(\'password\') user = User.query.filter(User.username == usern).first() if user: if user.check_password(passw): session[\'user\'] = usern session[\'user_id\'] = user.id session.permanent = True return redirect(url_for(\'index\')) else: return u\'password error\' else: return u\'error username\'
以上是关于密码保护的主要内容,如果未能解决你的问题,请参考以下文章
Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段
连接MySQL出现错误:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)(代码片段