当前位置: 代码网 > it编程>编程语言>Java > springboot中使用ConstraintValidatorContext验证两个字段内容相同

springboot中使用ConstraintValidatorContext验证两个字段内容相同

2024年10月27日 Java 我要评论
场景我在开发修改密码功能,通过原密码和新密码及确认新密码,希望通过constraintvalidator这个方式来校验新密码和确认新密码,规则是这两个密码需要是相同的。参考文档https://gith

场景

我在开发修改密码功能,通过原密码和新密码及确认新密码,希望通过constraintvalidator这个方式来校验新密码和确认新密码,规则是这两个密码需要是相同的。

参考文档

实现

定义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注解出现了弱编码,这块需要再进行优化。

到此这篇关于springboot中使用constraintvalidatorcontext验证两个字段内容相同的文章就介绍到这了,更多相关springboot验证字段内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com