springboot中使用ConstraintValidatorContext驗(yàn)證兩個(gè)字段內(nèi)容相同
場景
我在開發(fā)修改密碼功能,通過原密碼和新密碼及確認(rèn)新密碼,希望通過ConstraintValidator這個(gè)方式來校驗(yàn)新密碼和確認(rèn)新密碼,規(guī)則是這兩個(gè)密碼需要是相同的。
參考文檔
- 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
實(shí)現(xiàn)
定義Matches注解
@Constraint(validatedBy = SameContentMatchesValidator.class)
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface SameContentMatches {
String message() default "內(nèi)容不一致";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String field(); // 新增屬性,指定要比較的字段
}定義DTO對(duì)象
@Data
public class UserModifyPasswordDTO implements UserDTO {
@NotNull
private String userName;
@NotNull
private String password;
private String newPassword;
@SameContentMatches(field = "newPassword")
private String confirmPassword;
}定義MatchesValidator對(duì)象,實(shí)現(xiàn)驗(yàn)證的代碼邏輯
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類中,無法獲取到當(dāng)前對(duì)象,除非把SameContentMatches注解作用到當(dāng)前類上面,而非字段上面。
- 這個(gè)問題應(yīng)該主是無法解決的,因?yàn)槟銛r截的是字段,在這個(gè)ConstraintValidatorContext處理的都是和當(dāng)前字段有關(guān)的信息
應(yīng)用到類上,代碼調(diào)整,問題解決
@Constraint(validatedBy = SameContentMatchesValidator.class)
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface SameContentMatches {
String message() default "內(nèi)容不一致";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* 源字段名
* @return
*/
String sourceField();
/**
* 目標(biāo)字段名
* @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注解出現(xiàn)了弱編碼,這塊需要再進(jìn)行優(yōu)化。
到此這篇關(guān)于springboot中使用ConstraintValidatorContext驗(yàn)證兩個(gè)字段內(nèi)容相同的文章就介紹到這了,更多相關(guān)springboot驗(yàn)證字段內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot?整合數(shù)據(jù)源的具體實(shí)踐
本文主要介紹了SpringBoot?整合數(shù)據(jù)源的具體實(shí)踐,利用?Spring?Boot?的自動(dòng)配置和簡化的注解來簡化數(shù)據(jù)源配置工作,從而更專注于應(yīng)用程序的業(yè)務(wù)邏輯開發(fā),感興趣的可以了解一下2023-11-11
IDEA生成可運(yùn)行jar包(包含第三方j(luò)ar包)流程詳解
這篇文章主要介紹了IDEA生成可運(yùn)行jar包(包含第三方j(luò)ar包)流程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
使用Spring boot 的profile功能實(shí)現(xiàn)多環(huán)境配置自動(dòng)切換
這篇文章主要介紹了使用Spring boot 的profile功能實(shí)現(xiàn)多環(huán)境配置自動(dòng)切換的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-11-11
SpringBoot應(yīng)用優(yōu)雅停機(jī)的3種標(biāo)準(zhǔn)方案
所謂優(yōu)雅停機(jī)(Graceful Shutdown),就是讓應(yīng)用在關(guān)閉前停止接收新請(qǐng)求;把正在處理的請(qǐng)求執(zhí)行完;釋放線程池、連接池、MQ 消費(fèi)者等資源;最后再安全退出,今天講清楚 SpringBoot優(yōu)雅停機(jī)的3種標(biāo)準(zhǔn)方案,生產(chǎn)直接抄配置,需要的朋友可以參考下2026-03-03

