我正在将一个用户对象绑定(bind)到 Spring Controller 的 POST 请求中。在该对象中有一个密码字段。我想检查该文件是否经过 BCrypt 编码,因为保存和更新这两个事件都映射到同一 Controller 。知道这怎么可能吗?
代码片段:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveUser(Model model, @Validated @ModelAttribute("user") SystemUser user, BindingResult result) {
}
最佳答案
根据 Spring 的 BCryptPasswordEncoder 类,您可以使用此正则表达式模式检查字符串是否已编码
Pattern BCRYPT_PATTERN = Pattern.compile("\\A\\$2a?\\$\\d\\d\\$[./0-9A-Za-z]{53}");
if (BCRYPT_PATTERN.matcher(stringToCheck).matches()) {
// stringToCheck is an encoded bcrypt password.
}
关于java - 如何在java中检查密码是否是BCrypt编码的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55292677/