場景
我在開發修改密碼功能,透過原密碼和新密碼及確認新密碼,希望透過ConstraintValidator這個方式來校驗新密碼和確認新密碼,規則是這兩個密碼需要是相同的。
參考文件
- https://github.com/micronaut-projects/micronaut-core/issues/3243
- https://stackoverflow.com/questions/37750656/how-to-access-a-field-which-is-described-in-annotation-property
- https://discourse.hibernate.org/t/how-can-i-retrieve-current-validation-contexts-groups-in-a-validator/414/4
實現
定義Matches註解
@Constraint(validatedBy = SameContentMatchesValidator.class)
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface SameContentMatches {
String message() default "內容不一致";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String field(); // 新增屬性,指定要比較的欄位
}
定義DTO物件
@Data
public class UserModifyPasswordDTO implements UserDTO {
@NotNull
private String userName;
@NotNull
private String password;
private String newPassword;
@SameContentMatches(field = "newPassword")
private String confirmPassword;
}
定義MatchesValidator物件,實現驗證的程式碼邏輯
public class SameContentMatchesValidator implements ConstraintValidator<SameContentMatches, String> {
private String field;
@Override
public void initialize(SameContentMatches constraintAnnotation) {
this.field = constraintAnnotation.field();
}
@Override
public boolean isValid(String object, final ConstraintValidatorContext context) {
return true;
}
}
遇到的問題
- 在MatchesValidator類中,無法獲取到當前物件,除非把SameContentMatches註解作用到當前類上面,而非欄位上面。
- 這個問題應該主是無法解決的,因為你攔截的是欄位,在這個ConstraintValidatorContext處理的都是和當前欄位有關的資訊
應用到類上,程式碼調整,問題解決
@Constraint(validatedBy = SameContentMatchesValidator.class)
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface SameContentMatches {
String message() default "內容不一致";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* 源欄位名
* @return
*/
String sourceField();
/**
* 目標欄位名
* @return
*/
String destinationField();
}
public class SameContentMatchesValidator implements ConstraintValidator<SameContentMatches, Object> {
private String sourceField;
private String destinationField;
@Override
public void initialize(SameContentMatches constraintAnnotation) {
this.sourceField = constraintAnnotation.sourceField();
this.destinationField = constraintAnnotation.destinationField();
}
@Override
public boolean isValid(Object o, final ConstraintValidatorContext context) {
final Object sourceFieldVal = BeanUtil.getProperty(o, this.sourceField);
final Object destinationFieldVal = BeanUtil.getProperty(o, this.destinationField);
return sourceFieldVal.equals(destinationFieldVal);
}
}
@Data
@SameContentMatches(sourceField = "confirmPassword", destinationField = "newPassword")
public class UserModifyPasswordDTO implements UserDTO {
@NotNull
private String userName;
@NotNull
private String password;
private String newPassword;
private String confirmPassword;
}
上面的程式碼SameContentMatches註解出現了弱編碼,這塊需要再進行最佳化。