我有一个管理面板,我可以添加用户。 在插入表单我有用户名,密码(没有确认密码),以及一些普通的领域,如姓名,....春天+休眠更新密码
当我插入一个新的用户,我想,Hibernate会对其进行验证:
- 的用户名在数据库不为空
- 唯一的用户名(与上表中的唯一索引)
- 密码不为空(就目前来看,没有长度控制)
直到这里,everyt兴工作正常。
由于将来我会在编辑窗体中存储加密密码, ,我有正常的可编辑字段,如用户名,名称, ,但我没有把密码。相反,我把新的密码字段,以防万一我想改变它(不是强制性的)。
我编辑表单:
<!--
- username
-->
<div class="form-group">
<label class="col-sm-4 control-label">Username*:</label>
<div class="col-sm-8" ><input class="form-control" type="text" data-th-field="*{username}" placeholder="Username" th:errorclass="'has-error-input'"></input></div>
</div>
<!--
- password
- fingo password
-->
<div class="form-group">
<label class="col-sm-4 control-label">Password:</label>
<div class="col-sm-8" >******</div>
</div>
<!--
- new password
- (if i want to change it)
-->
<div class="form-group">
<label class="col-sm-4 control-label">New Password:</label>
<div class="col-sm-8" ><input class="form-control" type="password" id="password_new" name="password_new" placeholder="New Password"></input></div>
</div>
正如你可以看到password_new领域是一个普通的标签,而无需使用弹簧符号。我将在控制器中检索这个参数,我将检查是否写入了新密码或者不是。
现在的问题是:当我提交表单时,我得到验证错误,因为密码为空。 我问你:
我是一个很好的方式做到这一点?
是否可以跳过验证(密码)在休眠更新, 但在插入时设置为强制?然后更新除密码 以外的所有字段(您会看到我评论的行
userToUpdate.setPassword(user.getPassword());
)?可以从编辑的形式可以更好地消除新的密码字段,并在一个网页,我只更新密码更改 密码?
在包含加密密码的编辑表单中设置一个名为'password'的隐藏字段是不是愚蠢/不安全的想法,所以它不会给我验证错误?想一下,我认为这不是一个好主意,因为有人可以看到加密的密码,只是看页面的源代码。加密密码“清除”可能会危险/不安全?
User.java(模型)
@NotNull(message = "PASSWORD cannot be null")
@NotEmpty(message = "PASSWORD cannot be null")
@Size(max = 50, message = "Max 50 char")
@Column(name = "password", length = 50)
private String password;
UserDao.java
@Override
public void updateUser(User user) throws UserNotFoundException {
User userToUpdate = getUser(user.getId());
userToUpdate.setUsername(user.getUsername());
//userToUpdate.setPassword(user.getPassword());
userToUpdate.setNome(user.getNome());
userToUpdate.setCognome(user.getCognome());
userToUpdate.setEmail(user.getEmail());
userToUpdate.setEnabled(user.getEnabled());
userToUpdate.setRole(user.getRole());
getCurrentSession().update(userToUpdate);
}
UserController中。java的
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String editingUser(@Valid @ModelAttribute User user,
BindingResult result, RedirectAttributes redirectAttrs,
@RequestParam(value = "action", required = true) String action,
@RequestParam(value = "password_new") String password_new
){
logger.info("IN: User/edit-POST: " + action);
List<Role> user_roles = RoleService.getRoles();
if (action.equals("abort"))
{
/**
* message
*/
String message = "Edit not saved";
redirectAttrs.addFlashAttribute("message", message);
redirectAttrs.addFlashAttribute("message_class", "alert-warning");
redirectAttrs.addFlashAttribute("user_roles", user_roles);
}
else if (result.hasErrors())
{
logger.info("User-edit error: " + result.toString());
redirectAttrs.addFlashAttribute("org.springframework.validation.BindingResult.user", result);
redirectAttrs.addFlashAttribute("user", user);
/**
* as in my list page i have also the insert form,
* i need to populate the select
*/
redirectAttrs.addFlashAttribute("user_roles", user_roles);
return "redirect:/users/edit?id=" + user.getId();
}
else if (action.equals("save"))
{
try
{
logger.info("User/edit-POST: " + user.toString());
/**
* i see if i changed the password
*/
logger.info("new password: " + password_new);
if (!password_new.equals(""))
{
user.setPassword(password_new);
}
else
{
//nothing, or what?
}
/**
* now i can update the user in DB
*/
UserService.updateUser(user);
/**
* message
*/
String message = "User edited with success";
redirectAttrs.addFlashAttribute("message", message);
redirectAttrs.addFlashAttribute("message_class", "alert-success");
/**
* as in my list page i have also the insert form,
* i need to populate the select
*/
redirectAttrs.addFlashAttribute("user_roles", user_roles);
}
catch (UserNotFoundException e)
{
logger.info("User/edit-POST: id [" + user.getId() + "] not found");
String message = "User id [" + user.getId() + "] not found!";
redirectAttrs.addFlashAttribute("message", message);
redirectAttrs.addFlashAttribute("message_class", "alert-danger");
}
}
return "redirect:/users/list";
}
非常感谢你
PS。因为这是我在spring/hibernate/java中的第一个应用程序,现在我正在存储普通密码,将来我会加密它们。
你的意思是说创建自定义验证器? – Amogh
你的答案是完美的,我仍然只有一个问题:当一个字段验证失败(如空的用户名),我不能通过错误出现编辑窗体...我总是使用'redirectAttrs.addFlashAttribute(“org.springframework。 validation.BindingResult.user“,result);'但是在这里我看到错误在'constraintViolations' – SaganTheBest
解决'import org.springframework.validation.Validator;''validator.validate(user,result); '。感谢您的支持! – SaganTheBest