Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 播放1.2.4+;CRUD模块:生成哈希密码_Java_Jpa_Hash_Playframework_Crud - Fatal编程技术网

Java 播放1.2.4+;CRUD模块:生成哈希密码

Java 播放1.2.4+;CRUD模块:生成哈希密码,java,jpa,hash,playframework,crud,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){ t

我有一个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;
}
}


但是,当我创建一个用户时,它会在数据库中存储一个明文密码,而不是一个经过盐渍和哈希处理的密码?知道为什么吗

您应该从
用户
控制器类中的CRUD类中重新编写
create()
save()
方法

也许解决方案是这样的:

@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());
   }
}
@CRUD.For(User.class)
公共类用户扩展CRUD{
/**
*重新实现Create(C)方法
*@抛出异常
*/
公共静态void create()引发异常{
//获取模型类型
ObjectType=ObjectType.get(getControllerClass());
notFoundIfNull(类型);//如果框架无法确定模型类型,则出现render not found错误
构造函数=type.entityClass.getDeclaredConstructor();
constructor.setAccessible(true);
//创建模型的新实例
用户对象=(用户)构造函数。newInstance();
//绑定提交表单中的所有参数值
Binder.bindBean(params.getRootParamNode(),“object”,object);
//散列密码
object.password=Crypto.passwordHash(object.email+object.password);
//检查模型的有效性
有效(对象);
if(validation.hasErrors()){
renderArgs.put(“error”、play.i18n.Messages.get(“crud.hasErrors”);
试一试{
呈现(request.controller.replace(“.”,“/”+“/blank.html”),类型,对象);
}捕获(TemplateNotFounde异常){
呈现(“CRUD/blank.html”,类型,对象);
}
}
对象。_save();//最后,保存模型
success(play.i18n.Messages.get(“crud.created”,type.modelName));
if(params.get(“\u save”)!=null){
重定向(request.controller+“.list”);
}
if(params.get(“\u saveandaddother”)!=null){
重定向(request.controller+“.blank”);
}
重定向(request.controller+“.show”,object._key());
}
/**
*重新实施更新(U)方法
*@param-id
*@抛出异常
*/
公共静态无效保存(字符串id)引发异常{
//获取模型类型
ObjectType=ObjectType.get(getControllerClass());
notFoundIfNull(类型);//如果框架无法确定模型类型,则出现render not found错误
//查找要更新的模型
用户对象=(用户)类型.findById(id);
notFoundIfNull(object);//如果框架无法确定模型记录,则出现render not found错误
//绑定提交表单中的所有参数值
Binder.bindBean(params.getRootParamNode(),“object”,object);
//散列密码
object.password=Crypto.passwordHash(object.email+object.password);
//检查模型的有效性
有效(对象);
if(validation.hasErrors()){
renderArgs.put(“error”、play.i18n.Messages.get(“crud.hasErrors”);
试一试{
呈现(request.controller.replace(“.”,“/”+“/show.html”,类型,对象);
}捕获(TemplateNotFounde异常){
呈现(“CRUD/show.html”,类型,对象);
}
}
对象。_save();//最后,保存更改
flash.success(play.i18n.Messages.get(“crud.saved”,type.modelName));
if(params.get(“\u save”)!=null){
重定向(request.controller+“.list”);
}
重定向(request.controller+“.show”,object._key());
}
}
以后是否有任何操作(即“CRUD”)设置
密码
字段?也就是说,构造函数可能不是设置派生值的合适位置。。
@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());
   }
}