从请求构造函数中播放框架绑定

时间:2013-07-11 18:59:01

标签: playframework-2.1

我在从表单绑定数据时遇到问题,我的User类有一个构造函数,用于哈希密码,问题是如果我从表单中获取用户密码存储为纯文本。

我的用户构造函数:

 public User(String username, String completeName, String password,
        String email) {
    this.username = username.toLowerCase();
    this.email = email;
    this.registrationDate = new Date();
    this.completeName = completeName;
    try {
        String[] passAndSalt = PasswordHash.createHash(password).split(":");
        this.salt = passAndSalt[0];
        this.password = passAndSalt[1];
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }
}

我的控制器:

public static Result addUser() {
    Form<User> userForm = Form.form(User.class).bindFromRequest();
    if (!userForm.hasErrors()) {
        User formUser = userForm.get();
        formUser.save();
        return redirect(routes.Application.index());
    } else {
        return badRequest(registerUser.render(userForm));
    }
}

我可以在保存用户之前散列密码,但我想知道Play是否有办法使用我定义的构造函数。

使用Play!版本2.1.1

0 个答案:

没有答案