使用playframework存储二进制密码哈希值 [英] storing binary password hashes with playframework
问题描述
如何使用CRUD模块将二进制哈希密码存储在二进制类型列中?在纯JAVA中通常我使用Apache Commons编解码器库将二进制哈希存储到postgresql bytea列中,但我不能生成带有Model类或二进制哈希的bytea列...请帮助。
How i store a binary hash password in a binary type column with the CRUD module? In pure JAVA Usually i use the Apache Commons codec libs to store the binary hash into a postgresql bytea column, but i cannot generate neither the bytea column with the Model class or the binary hash... Any help please.
在Model类中,我将属性设置为bytea,注释如下:
In the Model class i set the property as bytea with annotations as follows
@Required
@Column(nullable=false, columnDefinition="BYTEA NOT NULL")
@Password
public byte[] hash;
,但CRUD控制器不显示文本框以输入要进行哈希处理的密码输入。
, but the CRUD controller dont display a textbox to put an input for the password to be hashed.
推荐答案
为密码创建一个单独的 @Transient String
字段从CRUD输入填写:
Create a separate @Transient String
field for the password, to be filled in from CRUD input:
@Password
@Transient
public String password;
然后从你的 @Required
注释中删除哈希字段。它根本不需要从CRUD填写。
Then remove the @Required
annotation from your hash field. It does not need to be filled in from CRUD at all.
接下来,创建一个新方法并使用 @PrePersist $ c注释它$ c>,因此它将在模型保存之前执行:
Next, create a new method and annotate it with @PrePersist
, so it will be executed before the model is saved:
@PrePersist
public void prePersist() throws Exception {
this.hash = Codec.hexMD5(this.password.getBytes());
}
更新:
要在CRUD视图中显示 @Transient
字段,您需要自定义空白
查看控制器的CRUD。打开shell /命令提示符并执行以下命令(将your_controller替换为控制器类的实际名称):
To have the @Transient
field appear on your CRUD view, you need to customize the blank
view of CRUD of your controller. Open a shell / command prompt and execute the following command (replacing "your_controller" with the actual name of your controller class):
play crud:ov --template your_controller/blank
这应该将CRUD blank.html模板复制到<应用程序中的code> views / your_controller / blank.html 。打开该文件,查找#{crud.form /}
标记,并在其下面添加以下代码:
This should copy the CRUD blank.html template to views/your_controller/blank.html
in your application. Open that file, look for the #{crud.form /}
tag, and add the following code below it:
<div class="crudField crud_password">
<label for="object_password">&{'label.password'}</label>
<input id="object_password" type="password" name="object.password" value="" />
<span class="crudHelp"></span>
</div>
现在,密码字段应显示在其他表单字段之后。
The password field should now appear after your other form fields.
这篇关于使用playframework存储二进制密码哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!