码迷,mamicode.com
首页 > 其他好文 > 详细

给密码加密和解密的方法

时间:2020-05-24 09:53:17      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:rom   hash   beans   web   route   class   sel   elf   info   

from werkzeug.security import generate_password_hash,check_password_hash

 

#book模型,传入db核心对象把Book插入db
class User(UserMixin,Base):
  __tablename__ = ‘user‘
  id = Column(Integer,primary_key=True)
  nickname = Column(String(24), nullable=False)
  phone_number = Column(String(18), unique=True)
  _password=Column("password",String(228), nullable=False)
  email = Column(String(50), unique=True, nullable=False)
  confirmed = Column(Boolean, default=False)
  beans = Column(Float, default=0)
  send_counter = Column(Integer, default=0)
  receive_counter = Column(Integer, default=0)
  wx_open_id =Column(String(50))
  wx_name =Column(String(32))


@property
def password(self):
  return self._password

@password.setter
def password(self,raw):
  self._password=generate_password_hash(raw)

def check_password(self,raw):
  return check_password_hash(self._password,raw)

装饰器原理: https://www.cnblogs.com/kaibindirver/p/12892064.html

视图函数

@web.route(‘/register‘, methods=[‘GET‘, ‘POST‘])
def register():
  form=RegisterForm(request.form)
  if request.method =="POST" and form.validate():
    user=User()
    user.set_attrs(form.data)
    db.session.add(user)
    db.session.commit()
    return redirect(url_for("web.login"))

return render_template("auth/register.html",form=form)

 

@web.route(‘/login‘, methods=[‘GET‘, ‘POST‘])
def login():
  form=LoginForm(request.form)
  if request.method =="POST" and form.validate():
    user= User.query.filter_by(email=form.email.data).first()
    if user and user.check_password(form.password.data):
      login_user(user)
      flash("登录成功")
  else:
    flash("账号不存在或密码错误")

  return render_template("auth/login.html",form=form)

给密码加密和解密的方法

标签:rom   hash   beans   web   route   class   sel   elf   info   

原文地址:https://www.cnblogs.com/kaibindirver/p/12945369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!