Playframework play框架-securesocial userpass实现

Playframework play框架-securesocial userpass实现,playframework,playframework-2.1,securesocial,Playframework,Playframework 2.1,Securesocial,我正在使用Play 2.1和securesocial master snapshot 我已经实现了UserService的查找和保存(扩展UserServicePlugin),如下所示: 查找方法如下: def find(userId: UserId): Option[Identity] = { val user = User.findByUserId(userId); user match { case Some(user) => { val

我正在使用Play 2.1和securesocial master snapshot

我已经实现了UserService的查找和保存(扩展UserServicePlugin),如下所示:

查找方法如下:

 def find(userId: UserId): Option[Identity] = {
    val user = User.findByUserId(userId);
    user match {
      case Some(user) => {
        val socialUser = new SocialUser(userId, null, null, user.name, Option(user.email), Option(user.photo), AuthenticationMethod("userPassword"), null, null, Some(PasswordInfo(PasswordHasher.BCryptHasher, BCrypt.hashpw(user.password, BCrypt.gensalt(10)))))
        Option(socialUser)
      }
      case None => {
         None
      }
    }
  }
 def save(user: Identity): Identity = {
    user.id.providerId match {
      case "facebook" => {

      }
      case "google" => {

      }
      case "twitter" => {
      }

      case "userpass" => {
        val eUser = User.findByEmail(user.id.id) match {
          case Some(eUser) => {
            //Existing User - update only
          }
          case None => {
            val appUser: User = new User(NotAssigned, "student", user.id.providerId, user.fullName, user.id.id, user.passwordInfo.get.password, null, null, null, null, null, "active")
            User.create(appUser)
          }
        }
      }
    }
    user
  }
 def find(userId: UserId): Option[Identity] = {
    val user = User.findByUserId(userId);
    user match {
      case Some(user) => {
        val socialUser = new SocialUser(userId, null, null, user.name, Option(user.email), Option(user.photo), AuthenticationMethod("userPassword"), null, null, Some(PasswordInfo(PasswordHasher.BCryptHasher, user.password)))
        Option(socialUser)
      }
      case None => {
         None
      }
    }
  }
保存方法如下:

 def find(userId: UserId): Option[Identity] = {
    val user = User.findByUserId(userId);
    user match {
      case Some(user) => {
        val socialUser = new SocialUser(userId, null, null, user.name, Option(user.email), Option(user.photo), AuthenticationMethod("userPassword"), null, null, Some(PasswordInfo(PasswordHasher.BCryptHasher, BCrypt.hashpw(user.password, BCrypt.gensalt(10)))))
        Option(socialUser)
      }
      case None => {
         None
      }
    }
  }
 def save(user: Identity): Identity = {
    user.id.providerId match {
      case "facebook" => {

      }
      case "google" => {

      }
      case "twitter" => {
      }

      case "userpass" => {
        val eUser = User.findByEmail(user.id.id) match {
          case Some(eUser) => {
            //Existing User - update only
          }
          case None => {
            val appUser: User = new User(NotAssigned, "student", user.id.providerId, user.fullName, user.id.id, user.passwordInfo.get.password, null, null, null, null, null, "active")
            User.create(appUser)
          }
        }
      }
    }
    user
  }
 def find(userId: UserId): Option[Identity] = {
    val user = User.findByUserId(userId);
    user match {
      case Some(user) => {
        val socialUser = new SocialUser(userId, null, null, user.name, Option(user.email), Option(user.photo), AuthenticationMethod("userPassword"), null, null, Some(PasswordInfo(PasswordHasher.BCryptHasher, user.password)))
        Option(socialUser)
      }
      case None => {
         None
      }
    }
  }
在将密码保存(注册)到数据库时,我不确定是否应该对其进行加密,上面总是说“您输入的凭据无效”

但是,如果我在下面的find方法中使用单词“password”(字符串)而不是user.password(来自数据库),它会正确验证凭据,并让我登录:

    val socialUser = new SocialUser(userId, null, null, user.name, Option(user.email), Option(user.photo), AuthenticationMethod("userPassword"), null, null, Some(PasswordInfo(PasswordHasher.BCryptHasher, BCrypt.hashpw("password", BCrypt.gensalt(10)))))
在这里,我认为它再次尝试加密数据库中的密码(已加密)。。。我想,我应该要么保存密码而不加密,要么获取用户在登录页面中输入的密码,以便在find方法中使用。谁能帮帮我,谢谢

我相信下面是所有社交网络的两个入口&用户通行证提供商

供应商入口点 GET/authenticate/:provider securesocial.controllers.ProviderController.authenticate(provider)

POST/authenticate/:提供程序securesocial.controllers.ProviderController.authenticateByPost(提供程序)

我认为这些实现都附带了securesocial插件?还是应该是相同的?这是最好的解决方案吗

我正在使用MySQL,下面是我的表:

create table t_users (
    id int unsigned not null auto_increment,
    user_type enum('admin', 'user') not null default 'user',
    login_type set('userpass', 'facebook', 'google', 'twitter') not null default 'userpass',
    name varchar(64) not null,
    email varchar(128) null,
    password varchar(128),
    mobile varchar(10) null,
    facebook varchar(64) null,
    google varchar(64) null,
    twitter varchar(64) null,
    photo varchar(128),
    status enum('registered', 'active', 'suspended', 'deleted') not null default 'registered',
    modified timestamp not null,
    last_login timestamp not null,
    primary key (id),
    unique(email),
    unique(facebook),
    unique(google),
    unique(twitter)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

您需要在某个地方使用
BCrypt.checkpw(提供密码、哈希密码)


因此,是的,您应该将哈希密码保存到数据库中。然后,当用户在登录屏幕上提供密码时,您应该从数据库中检索该用户的哈希密码,调用checkpw,如果返回true,则登录该用户。

正确的查找方法如下:

 def find(userId: UserId): Option[Identity] = {
    val user = User.findByUserId(userId);
    user match {
      case Some(user) => {
        val socialUser = new SocialUser(userId, null, null, user.name, Option(user.email), Option(user.photo), AuthenticationMethod("userPassword"), null, null, Some(PasswordInfo(PasswordHasher.BCryptHasher, BCrypt.hashpw(user.password, BCrypt.gensalt(10)))))
        Option(socialUser)
      }
      case None => {
         None
      }
    }
  }
 def save(user: Identity): Identity = {
    user.id.providerId match {
      case "facebook" => {

      }
      case "google" => {

      }
      case "twitter" => {
      }

      case "userpass" => {
        val eUser = User.findByEmail(user.id.id) match {
          case Some(eUser) => {
            //Existing User - update only
          }
          case None => {
            val appUser: User = new User(NotAssigned, "student", user.id.providerId, user.fullName, user.id.id, user.passwordInfo.get.password, null, null, null, null, null, "active")
            User.create(appUser)
          }
        }
      }
    }
    user
  }
 def find(userId: UserId): Option[Identity] = {
    val user = User.findByUserId(userId);
    user match {
      case Some(user) => {
        val socialUser = new SocialUser(userId, null, null, user.name, Option(user.email), Option(user.photo), AuthenticationMethod("userPassword"), null, null, Some(PasswordInfo(PasswordHasher.BCryptHasher, user.password)))
        Option(socialUser)
      }
      case None => {
         None
      }
    }
  }

POST/authenticate/:provider securesocial.controllers.ProviderController.authenticatepost(provider)
上面的一行是UserPass的默认实现身份验证?它是?如果是这样的话,你是说我们应该用上面的checkpw方法更新或替换它吗?SecureSocial应该自动为你做所有的哈希。看来find()方法是错误的。在您的代码中,看起来您正在对已经散列的密码进行散列。您应该能够复制演示项目并根据需要进行修改以开始:我看到了示例,他们没有使用密码或bcrypt或任何东西,但我发现了问题,我正在尝试再次加密密码,如
bcrypt.hashpw(user.password,bcrypt.gensalt(10))
实际上应该是
用户。密码
我不知道我为什么这么做:(谢谢讨论:)我真的很感激。