首页 > 其他分享 >springboot~ConstraintValidatorContext验证两个字段内容相同

springboot~ConstraintValidatorContext验证两个字段内容相同

时间:2024-10-14 14:43:31浏览次数:1  
标签:destinationField String SameContentMatches 验证 sourceField private ConstraintVali

场景

我在开发修改密码功能,通过原密码和新密码及确认新密码,希望通过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注解出现了弱编码,这块需要再进行优化。

标签:destinationField,String,SameContentMatches,验证,sourceField,private,ConstraintVali
From: https://www.cnblogs.com/lori/p/18464161

相关文章

  • SpringBoot基础(五):集成JUnit5
    SpringBoot基础系列文章SpringBoot基础(一):快速入门SpringBoot基础(二):配置文件详解SpringBoot基础(三):Logback日志SpringBoot基础(四):bean的多种加载方式SpringBoot基础(五):集成JUnit5目录一、JUnit5介绍1、JUnit5组成结构2、什么是单元测试二、SpringBoot整合J......
  • 国防科大:反事实验证LLM在RAG的生成质量
    ......
  • idea社区版配置springboot项目问题分析及处理
    前言记录一次使用IDEA社区版配置SpringBoot项目的经历,包括遇到的问题及解决过程IDEA版本:IntelliJIDEA2024.2.3(CommunityEdition)问题描述1IDEA社区版中并不支持Spring项目的创建等其他操作,在导入项目后无法自动识别,需要手动进行配置解决过程1项目导入后,在项目结构中以M......
  • SpringBoot框架下的智能人事管理平台:开发与实践
    2相关技术2.1MYSQL数据库MySQL是一个真正的多用户、多线程SQL数据库服务器。是基于SQL的客户/服务器模式的关系数据库管理系统,它的有点有有功能强大、使用简单、管理方便、安全可靠性高、运行速度快、多线程、跨平台性、完全网络化、稳定性等,非常适用于Web站点或者其他......
  • 第03章 SpringBoot获取请求参数
    我们首先创建“SpringBootRequestDemo”工程。然后我们修改编码格式以及Maven仓库地址,我们省略这个过程了。接着我们再添加spring-boot-starter-parent,spring-boot-starter-web,spring-boot-starter-thymeleaf依赖库<?xmlversion="1.0"encoding="UTF-8"?><projectxm......
  • 第04章 SpringBoot集成JDBC
    首先,我们新创建一个“SpringBootJdbcDemo”的Maven工程。然后我们修改编码格式以及Maven仓库地址,我们省略这个过程了。接着我们再添加spring-boot-starter-parent,spring-boot-starter-web,spring-boot-starter-thymeleaf依赖库,然后我们还需要添加本章节要学习的spring-bo......
  • 097基于java ssm springboot汽车配件销售商城管理系统(源码+文档+运行视频+讲解视频)
    项目技术:Springboot+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows......
  • 100基于java ssm springboot体检预约系统体检套餐报告体检论坛(源码+文档+运行视频+讲
    项目技术:Springboot+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows......
  • 101基于java ssm springboot协同过滤算法高考志愿填报系统(源码+文档+运行视频+讲解视
    项目技术:Springboot+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows......
  • 091基于java ssm springboot考研互助平台系统招生信息交流互动(源码+文档+运行视频+讲
    项目技术:Springboot+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows......