首页>代码>springboot自定义注解实现重置密码验证功能>/field-match/src/main/java/com/memorynotfound/spring/security/constraint/FieldMatchValidator.java
package com.memorynotfound.spring.security.constraint; import org.apache.commons.beanutils.BeanUtils; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object> { private String firstFieldName; private String secondFieldName; private String message; @Override public void initialize(final FieldMatch constraintAnnotation) { firstFieldName = constraintAnnotation.first(); secondFieldName = constraintAnnotation.second(); message = constraintAnnotation.message(); } @Override public boolean isValid(final Object value, final ConstraintValidatorContext context) { boolean valid = true; try { final Object firstObj = BeanUtils.getProperty(value, firstFieldName); final Object secondObj = BeanUtils.getProperty(value, secondFieldName); valid = firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj); } catch (final Exception ignore) { // ignore } if (!valid){ context.buildConstraintViolationWithTemplate(message) .addPropertyNode(firstFieldName) .addConstraintViolation() .disableDefaultConstraintViolation(); } return valid; } }
最近下载更多
annazhang LV29
2023年4月8日
whfuai LV14
2022年9月17日
苏屿999 LV9
2021年10月21日
弹指又十年 LV8
2021年10月17日
Curryhuang LV3
2021年6月22日
最近浏览更多
微信网友_6754091131867136
2023年11月27日
暂无贡献等级
chenlie LV2
2023年11月21日
漫步的海星 LV4
2023年9月26日
dapeng0011 LV13
2023年6月19日
annazhang LV29
2023年4月8日
zxc131313 LV12
2023年2月2日
ROCK森 LV12
2022年11月7日
秋枫花落叶 LV2
2022年11月3日
wuying8208 LV15
2022年10月31日
whfuai LV14
2022年9月17日