【发布时间】:2021-03-26 10:17:44
【问题描述】:
我正在开发基本的重置密码流程。这是我的 DTO:
@Getter
@Setter
@FieldsValueMatch(field = "password", fieldMatch = "confirmPassword", message = "Passwords do not match")
public class PasswordResetForm {
@Size(min = 8, message = "Password needs to have at least 8 characters")
private String password;
private String confirmPassword;
}
有FieldsValueMatch注解:
@Constraint(validatedBy = FieldsValueMatchValidator.class)
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldsValueMatch {
String message() default "Fields values don't match!";
String field();
String fieldMatch();
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
和验证器:
public class FieldsValueMatchValidator implements ConstraintValidator<FieldsValueMatch, Object> {
private String field;
private String fieldMatch;
@Override
public void initialize(FieldsValueMatch constraintAnnotation) {
this.field = constraintAnnotation.field();
this.fieldMatch = constraintAnnotation.fieldMatch();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
Object fieldValue = new BeanWrapperImpl(value).getPropertyValue(field);
Object fieldMatchValue = new BeanWrapperImpl(value).getPropertyValue(fieldMatch);
if (fieldValue != null) {
return fieldValue.equals(fieldMatchValue);
} else {
return fieldMatchValue == null;
}
}
}
这是我的控制器:
@Controller
public class ResetPasswordController {
@ModelAttribute("passwordResetForm")
public PasswordResetForm passwordResetForm() {
return new PasswordResetForm();
}
@GetMapping("/reset-password")
public String showResetPasswordForm(final Model model) {
return "reset-password";
}
@PostMapping("/reset-password-result")
public String resetPassword(@Valid final PasswordResetForm passwordResetForm,
final BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "reset-password";
}
// reset password logic
return "redirect:/reset-password-success";
}
}
以及 Thymeleaf 页面的一部分:
<form th:action="@{/reset-password-result}" th:object="${passwordResetForm}" method="post">
<div>
<div class="input-group">
<input id="password"
class="form-input"
placeholder="Set a new password"
type="password"
th:field="*{password}"/>
</div>
<div th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></div>
</div>
<div>
<div class="input-group">
<input id="confirmPassword"
class="form-input"
placeholder="Re-type a new password"
type="password"
th:field="*{confirmPassword}"/>
</div>
<div th:if="${#fields.hasErrors('confirmPassword')}" th:errors="*{confirmPassword}"></div>
</div>
<div class="form-group">
<button type="submit" class="form-btn">SET</button>
</div>
</form>
现在,当我在两个输入中输入不同的密码时,我在终端中收到以下消息:
2021-03-26 11:13:39.315 WARN 1340 [nio-8080-exec-7]
s.w.s.h.AbstractHandlerExceptionResolver : Resolved
[org.springframework.validation.BindException:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Error in object 'passwordResetForm': codes
[FieldsValueMatch.passwordResetForm,FieldsValueMatch]; arguments
[org.springframework.context.support.DefaultMessageSourceResolvable: codes [passwordResetForm.,]; arguments []; default message [],password,confirmPassword]; default message [Passwords do not match]]
和即时 400 结果代码。我在控制器中的resetPassword 方法没有被调用,所以我的 Thymeleaf 页面我得到了 Whitelabel 错误页面。当我输入的密码少于 8 个字符时,也会发生同样的情况。我做错了什么?
感谢您的帮助!
【问题讨论】:
-
你试过not把valid放在
PasswordResetForm上吗? -
是的,在这种情况下验证不起作用
-
验证在其他地方对您有用吗?也许你面对的是this issue?
-
FWIW,您的代码在粘贴到全新的 Spring Boot 2.4.4 项目时似乎可以工作。
标签: java spring spring-boot spring-mvc thymeleaf