密码的验证规则和自定义注解和验证器一样吗_js实现用户名和密码验证

密码的验证规则和自定义注解和验证器一样吗_js实现用户名和密码验证密码的验证规则和自定义注解和验证器一、Passay密码验证框架二、使用2.1、新建PasswordConstraintValidator校验器2.2、新建ValidPassword注解2.3、UserDto三、校验密码

密码的验证规则和自定义注解和验证器一样吗_js实现用户名和密码验证"

一、Passay密码验证框架

		<dependency>
            <groupId>org.passay</groupId>
            <artifactId>passay</artifactId>
            <version>1.6.0</version>
        </dependency>

二、使用

2.1、新建PasswordConstraintValidator校验器

package com.moss.uaa_security.validation;

import com.moss.uaa_security.annotation.ValidPassword;
import lombok.val;
import org.passay.*;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/** * @description * @author: lwj * @create: 2021-03-25 22:12 **/
public class PasswordConstraintValidator implements ConstraintValidator<ValidPassword, String> { 
   
    @Override
    public void initialize(ValidPassword constraintAnnotation) { 
   

    }

    @Override
    public boolean isValid(String password, ConstraintValidatorContext constraintValidatorContext) { 
   
        val validator = new PasswordValidator(Arrays.asList(
                //密码长度为8到30位
                new LengthRule(8, 30),
                //至少有一个英文的大写字母
                new CharacterRule(EnglishCharacterData.UpperCase, 1),
                //至少有一个英文的小写字母
                new CharacterRule(EnglishCharacterData.LowerCase, 1),
                //至少有一个英文的特殊字符
                new CharacterRule(EnglishCharacterData.Special, 1),
                //不允许有5个连续的英文字母
                new IllegalSequenceRule(EnglishSequenceData.Alphabetical, 5, false),
                //不允许有5个连续的数字
                new IllegalSequenceRule(EnglishSequenceData.Numerical, 5, false),
                //不允许有5个键盘连续的字母
                new IllegalSequenceRule(EnglishSequenceData.USQwerty, 5, false),
                //需要有空格
                new WhitespaceRule()
        ));
        val result = validator.validate(new PasswordData(password));
        return result.isValid();
    }
}

2.2、新建ValidPassword注解

package com.moss.uaa_security.annotation;

import com.moss.uaa_security.validation.PasswordConstraintValidator;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;

/** * @description * @author: lwj * @create: 2021-03-25 22:13 **/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PasswordConstraintValidator.class)
@Target({ 
   ElementType.TYPE, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface ValidPassword { 
   

    String message() default "Invalid Password";

    Class<?>[] groups() default { 
   };

    Class<? extends Payload>[] payload() default { 
   };

}

2.3、UserDto

package com.moss.uaa_security.domain.dto;

import com.moss.uaa_security.annotation.ValidEmail;
import com.moss.uaa_security.annotation.ValidPassword;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;

@Data
public class UserDto implements Serializable { 
   

    @NotNull
    @NotBlank
    @Size(min = 4, max = 50, message = "用户名长度必须在4~50个字符之间")
    private String username;
    @NotNull
    @ValidPassword
    private String password;
    @NotNull
    private String matchingPassword;
    @NotNull
    @ValidEmail
    private String email;
    @NotNull
    @NotBlank
    @Size(min = 4, max = 50, message = "用户名长度必须在4~50个字符之间")
    private String name;

}

三、校验密码和重复密码是否相同

3.1、UserDto

package com.moss.uaa_security.domain.dto;

import com.moss.uaa_security.annotation.ValidEmail;
import com.moss.uaa_security.annotation.ValidPassword;
import com.moss.uaa_security.annotation.ValidPasswordMatch;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;

@Data
@ValidPasswordMatch
public class UserDto implements Serializable { 
   

    @NotNull
    @NotBlank
    @Size(min = 4, max = 50, message = "用户名长度必须在4~50个字符之间")
    private String username;
    @NotNull
    @ValidPassword
    private String password;

    private String matchingPassword;
    @NotNull
    @ValidEmail
    private String email;
    @NotNull
    @NotBlank
    @Size(min = 4, max = 50, message = "用户名长度必须在4~50个字符之间")
    private String name;

}

3.2、PasswordMatchValidator

package com.moss.uaa_security.validation;

import com.moss.uaa_security.annotation.ValidPasswordMatch;
import com.moss.uaa_security.domain.dto.UserDto;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/** * @description * @author: lwj * @create: 2021-03-25 22:30 **/
public class PasswordMatchValidator implements ConstraintValidator<ValidPasswordMatch, UserDto> { 
   
    @Override
    public void initialize(ValidPasswordMatch constraintAnnotation) { 
   

    }

    @Override
    public boolean isValid(UserDto userDto, ConstraintValidatorContext constraintValidatorContext) { 
   
        return userDto.getPassword().equals(userDto.getMatchingPassword());
    }
}

3.3、ValidPasswordMatch

package com.moss.uaa_security.annotation;

import com.moss.uaa_security.validation.PasswordMatchValidator;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;

/** * @description * @author: lwj * @create: 2021-03-25 22:31 **/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PasswordMatchValidator.class)
@Target({ 
   ElementType.TYPE, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface ValidPasswordMatch { 
   

    String message() default "Password Not Match";

    Class<?>[] groups() default { 
   };

    Class<? extends Payload>[] payload() default { 
   };

}

今天的文章密码的验证规则和自定义注解和验证器一样吗_js实现用户名和密码验证分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/88843.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注