Spring 启动自定义验证器不会 return 到控制器

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
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 方法没有被调用,所以我得到的是 Whitelabel 错误页面,而不是我的 Thymeleaf 页面。当我输入的密码少于 8 个字符时,也会发生同样的情况。我做错了什么?

非常感谢你的帮助!

尝试在 ResetPasswordController class 上添加 @Validated 注释(before/after @Controller 注释)。这应该启用验证

检查您的方法参数序列。

Method Arguments

You must declare an Errors, or BindingResult argument immediately after the validated method argument.

因此,如果您的代码实际上如下所示,您的问题将会重现。

    @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";
    }

您将 @FieldsValueMatch 定义为 class 级约束。可能是生成的默认约束违规导致了问题,因为在这种情况下,没有为创建的约束违规指定明确的 属性 路径。

The same happens when I put password shorter than 8 characters.

无论其他字段级验证(=@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;
    }
}

感谢所有回复。无缘无故,它在我这边没有任何改变就开始工作了……我不知道发生了什么,也许我需要重新启动我的网络浏览器或类似的东西……