[英]Spring Boot custom validator doesn't return to Controller
我正在开发基本的重置密码流程。 这是我的 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:
@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 结果代码。 我在 Controller 中的resetPassword
方法没有被调用,所以我的 Thymeleaf 页面得到了 Whitelabel 错误页面。 当我输入的密码少于 8 个字符时,也会发生同样的情况。 我究竟做错了什么?
感谢您的帮助!
尝试在ResetPasswordController
class 上添加@Validated
注释( @Controller
注释之前/之后)。 这应该启用验证
您将@FieldsValueMatch
定义为类级约束。 可能是生成的默认约束违规导致了问题,因为在这种情况下,没有为创建的约束违规指定显式属性路径。
当我输入的密码少于 8 个字符时,也会发生同样的情况。
无论其他字段级验证(= @Size
)如何, @FieldsValueMatch
都会在任何情况下执行,这可能就是您仍然面临同样问题的原因。
因此,调整FieldsValueMatchValidator
实现 - 通过设置创建的约束违规的属性路径并提供自定义错误消息 - 应该可以解决问题:
public class FieldsValueMatchValidator implements
ConstraintValidator<FieldsValueMatch, Object> {
// ...
private String message;
@Override
public void initialize(FieldsValueMatch constraintAnnotation) {
// ...
this.message = constraintAnnotation.message();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
//...
boolean valid;
if (fieldValue != null) {
valid = fieldValue.equals(fieldMatchValue);
} else {
valid = fieldMatchValue == null;
}
if (!valid){
context.buildConstraintViolationWithTemplate(this.message) // setting the custom message
.addPropertyNode(this.field) // setting property path
.addConstraintViolation() // creating the new constraint violation
.disableDefaultConstraintViolation() // disabling the default constraint violation
;
}
return valid;
}
}
检查您的方法 arguments 序列。
您必须在验证的方法参数之后立即声明一个
Errors
或BindingResult
参数。
因此,如果您的代码实际上如下所示,您的问题将被重现。
@PostMapping("/reset-password-result")
public String resetPassword(@Valid PasswordResetForm passwordResetForm, Model model,
BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "reset-password";
}
// reset password logic
return "redirect:/reset-password-success";
}
感谢所有的答复。 无缘无故,它刚刚开始工作而我没有任何改变......我不知道发生了什么,也许我需要重新启动我的 web 浏览器或类似的东西......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.