Java JSR303:尝试自定义约束冲突以与类级关系约束验证器中的子路径关联

Java JSR303:尝试自定义约束冲突以与类级关系约束验证器中的子路径关联,java,constraints,bean-validation,Java,Constraints,Bean Validation,我正在使用JSR303,并创建了一个类级约束,该约束将密码与表单中的确认进行比较,我将在这里将其命名为@SameAs约束。理想情况下,我希望将约束与预期目标(confirmPassword)相关联,但显然,封闭bean不可用于提取密码属性因此,类级别的约束 我感兴趣地阅读了其他演示如何利用类级约束来验证关系的帖子,但找不到任何解释如何将约束冲突定制为与子路径(在本例中为关系中的两个字段之一)关联的内容 我的问题是:如何将约束冲突中的消息与“confirmPassword”字段而不是顶级对象相关联

我正在使用JSR303,并创建了一个类级约束,该约束将密码与表单中的确认进行比较,我将在这里将其命名为@SameAs约束。理想情况下,我希望将约束与预期目标(confirmPassword)相关联,但显然,封闭bean不可用于提取密码属性因此,类级别的约束

我感兴趣地阅读了其他演示如何利用类级约束来验证关系的帖子,但找不到任何解释如何将约束冲突定制为与子路径(在本例中为关系中的两个字段之一)关联的内容

我的问题是:如何将约束冲突中的消息与“confirmPassword”字段而不是顶级对象相关联?我尝试使用javax.Validator.validate(target,context)的上下文参数,但是在@SameAs的验证器中添加一个节点会导致级联中的下一个约束出现异常(尝试提取confirmPassword->orderNumber属性,而不是order->orderNumber)

目前,我已经求助于一个丑陋的乱七八糟的方法,创建了一个额外的属性来存储约束消息,该消息是在web层上的confirmPassword输入字段附近提取出来使用的

当然我在这里遗漏了一些东西……请看下面的例子

谢谢你的评论

范例

@Constraint( validatedBy = { SamePwdAsValidator.class})
public interface SamePwdAs {//...
}

//Using passwords in an order doesn't make sense - only for demo purpose
@SamePwdAs( message = "Password and confirmation must match" ...)
public class Order {

    @NotNull
    @Size(....)
    String pwd;

    //where I would really like to use @SameAs, and associate a violation 
    String pwdConfirm;

    @NotNull (...)
    @Pattern (....)
    String orderNumber;

    //...getters/setters
}

public class SamePwdAsValidator implements javax.validation.Validator {
//...
  public boolean isValid( Object target, ValidationContext ctx) {
   String tgt = target.getPwd(), other = target.getPwdConfirm()
   boolean isValid = tgt.equals( other);

   if ( !isValid) {
      //try to configure the context subpath for pwdConfirm to associate this constraint violation with: I tried 
      //ctx.addNode( 'pwdConfirm').addConstraintViolation() which doesn't work, as the next validator will
      //bump into trying to extract Order.pwdConfirm.orderNumber and throw a NoPropertyFoundException or the like
   }

   return isValid;
}

我将给出两个答案

回答1,努力回答您的问题:

我使用的实用方法如下:

public static final void recreateConstraintViolation(ConstraintValidatorContext constraintValidatorContext, String errorCode, String fieldName) {
        constraintValidatorContext.disableDefaultConstraintViolation();
        constraintValidatorContext.buildConstraintViolationWithTemplate(errorCode).
        addNode(fieldName).addConstraintViolation();

}
然后,在验证程序中比较两个密码:

if ( !isValid) {
    for (String fieldName : fieldNames) {
        CustomConstraintUtils.recreateConstraintViolation(constraintValidatorContext, "password.password2.mismatch", fieldName);
    }
    return false;
}
答案2

我建议您使用像Spring security这样的安全框架来处理密码匹配,因为我假设您的用例是处理用户登录的