播放1.2.4 + CRUD模块:生成散列密码

时间:2013-03-04 23:58:50

标签: java jpa hash playframework crud

我有一个user.java类

@javax.persistence.Entity
@Table(name="users")
public class User extends Model implements RoleHolder {


   public User(String email, String password, String firstName, String lastName, Status status, List<UserRole> roles){
    this.email = email;
    this.password = Crypto.passwordHash(password+email);
    this.firstName = firstName;
    this.lastName = lastName;
    this.status = status; 
    this.roles = roles;
}

还有users.java

public class Users extends CRUD {   

}

但是,当我创建一个用户时,它会在数据库中存储明文密码而不是盐水和散列密码?任何想法为什么?

1 个答案:

答案 0 :(得分:0)

您应该从create()控制器类中的CRUD类重写save()Users方法。

也许解决方案看起来像这样:

@CRUD.For(User.class)
public class Users extends CRUD {
   /**
    * Re-implement Create (C) method
    * @throws Exception
    */
   public static void create() throws Exception {
      // Get model type
      ObjectType type = ObjectType.get(getControllerClass());
      notFoundIfNull(type); // render not found error if framework can't determine model type
      Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
      constructor.setAccessible(true);

      // Create new instance of model
      User object = (User) constructor.newInstance();
      // Bind all parameter value from submitted form
      Binder.bindBean(params.getRootParamNode(), "object", object);
      // Hash the password
      object.password = Crypto.passwordHash(object.email + object.password);

      // Check validity of model
      validation.valid(object);
      if (validation.hasErrors()) {
         renderArgs.put("error", play.i18n.Messages.get("crud.hasErrors"));
         try {
            render(request.controller.replace(".", "/") + "/blank.html", type, object);
         } catch (TemplateNotFoundException e) {
            render("CRUD/blank.html", type, object);
         }
      }
      object._save(); // Finally, save the model
      flash.success(play.i18n.Messages.get("crud.created", type.modelName));
      if (params.get("_save") != null) {
         redirect(request.controller + ".list");
      }
      if (params.get("_saveAndAddAnother") != null) {
         redirect(request.controller + ".blank");
      }
      redirect(request.controller + ".show", object._key());
   }

   /**
    * Re-implement Update (U) mehod
    * @param id
    * @throws Exception
    */
   public static void save(String id) throws Exception {
      // Get model type
      ObjectType type = ObjectType.get(getControllerClass());
      notFoundIfNull(type); // render not found error if framework can't determine model type

      // Find the model to be updated
      User object = (User) type.findById(id);
      notFoundIfNull(object); // render not found error if framework can't determine model record
      // Bind all parameter value from submitted form
      Binder.bindBean(params.getRootParamNode(), "object", object);
      // Hash the password
      object.password = Crypto.passwordHash(object.email + object.password);

      // Check validity of model
      validation.valid(object);
      if (validation.hasErrors()) {
         renderArgs.put("error", play.i18n.Messages.get("crud.hasErrors"));
         try {
            render(request.controller.replace(".", "/") + "/show.html", type, object);
         } catch (TemplateNotFoundException e) {
            render("CRUD/show.html", type, object);
         }
      }
      object._save(); // Finally, save changes
      flash.success(play.i18n.Messages.get("crud.saved", type.modelName));
      if (params.get("_save") != null) {
         redirect(request.controller + ".list");
      }
      redirect(request.controller + ".show", object._key());
   }
}