【问题标题】:Slick match on encrypted variable加密变量的巧妙匹配
【发布时间】:2013-01-21 16:09:02
【问题描述】:

如何使用 Slick 选择加密变量。

我的数据库中有一个 BCrypt 编码的密码。

说明我的意图:

def login(name: String, password: String) = Action {
...
  for {
    u <- Users if u.name === name && BCrypt.checkpw(password, u.password)
  } yield u

当然,slick 抱怨 u.password 是一个提升的列而不是字符串。

你打算如何解决这个问题?

【问题讨论】:

    标签: scala encryption playframework-2.0 playframework-2.1 slick


    【解决方案1】:

    实际上我设法解决了我的问题。

      def login(name: String, password: String) = Action {
        database withSession {
          (for {
            u <- Users if u.name === name 
          } yield u).list
        } match {
          case Nil => Ok("No user found")
          case head :: tail => 
            if(BCrypt.checkpw(password, head.password))
              Ok("accepted").withSession("userid" -> head.id.get.toString)
            else
              Ok("Incorrect password")
        }
      }
    

    【讨论】:

      【解决方案2】:

      我有同样的问题,但在模型课上。

      由于您的解决方案启发了我,因此我将其发布在此处,希望对您有所帮助。

      def authenticate(email: String, password: String): Future[Option[User]] = db.run {
          Users.filter{_.email === email}.result.headOption
        } map {
          case Some(u) if BCrypt.checkpw(password, u.password) => Some(u)
          case _ => None
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-10
        • 2017-01-07
        • 1970-01-01
        • 1970-01-01
        • 2011-01-19
        • 2015-03-11
        • 1970-01-01
        相关资源
        最近更新 更多