Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 密码验证服务_Java_Spring_String_Validation_Passwords - Fatal编程技术网

Java 密码验证服务

Java 密码验证服务,java,spring,string,validation,passwords,Java,Spring,String,Validation,Passwords,我需要编写密码验证服务,这是为了接受某些规则:我编写了代码: @Service public class PasswordValidatonServiceImpl implements PasswordValidationService { public static final String EMPTY_OR_NULL_PASSWORD = "Password Should not be empty"; public static final String ERROR_PAS

我需要编写密码验证服务,这是为了接受某些规则:我编写了代码:

@Service
public class PasswordValidatonServiceImpl implements PasswordValidationService {

    public static final String EMPTY_OR_NULL_PASSWORD = "Password Should not be empty";
    public static final String ERROR_PASSWORD_LENGTH = "Password must be betwee 5 and 12 characters long.";
    public static final String ERROR_PASSWORD_CASE = "Password must only contain lowercase letters.";
    public static final String ERROR_LETTER_AND_DIGIT = "Password must contain both a letter and a digit.";
    public static final String ERROR_PASSWORD_SEQUENCE_REPEATED = "Password must not contain any sequence of characters immediately followed by the same sequence.";


    private Pattern checkCasePattern = Pattern.compile("[A-Z]");
    private Pattern checkLetterAndDigit = Pattern
            .compile("(?=.*[a-z])(?=.*[0-9])");
    private Pattern checkSequenceRepetition = Pattern.compile("(\\w{2,})\\1");

    /**
     * @param password
     * @return List<String> This method calls 4 more methods which validates
     *         password and return list of errors if any.
     */
    public List<String> validatePassword(String password) {
        List<String> failures = new ArrayList<String>();
        if (StringUtils.isEmpty(password)) {
            failures.add(EMPTY_OR_NULL_PASSWORD);
            return failures;
        } else {
            checkLength(password, failures);
            checkCase(password, failures);
            checkLetterAndDigit(password, failures);
            checkSequenceRepetition(password, failures);
            return failures;
        }
    }

    /**
     * @param password
     * @param failures
     *            This method will validate if there are any repeated character
     *            sequence, if found it will add error message to failures list.
     */
    private void checkSequenceRepetition(String password, List<String> failures) {
        Matcher matcher = checkSequenceRepetition.matcher(password);
        if (matcher.find()) {
            failures.add(ERROR_PASSWORD_SEQUENCE_REPEATED);
        }
    }

    /**
     * @param password
     * @param failures
     *            This method will validate both letters and characters in
     *            password, if not found add a error message to the failures
     *            list.
     */
    private void checkLetterAndDigit(String password, List<String> failures) {
        Matcher matcher = checkLetterAndDigit.matcher(password);
        if (!matcher.find()) {
            failures.add(ERROR_LETTER_AND_DIGIT);
        }
    }

    /**
     * @param password
     * @param failures
     *            This Method checks upper case and lower case letters in the
     *            password if there are any Upper case letters it will add error
     *            message to failures list.
     */
    private void checkCase(String password, List<String> failures) {
        Matcher matcher = checkCasePattern.matcher(password);
        if (matcher.find()) {
            failures.add(ERROR_PASSWORD_CASE);
        }
    }

    /**
     * @param string
     * @param failures
     *            This Method will checks the length of the string, if string is
     *            less than 5 or more than 12 characters then it will add error
     *            message into failures list
     */
    private void checkLength(String string, List<String> failures) {
        if (string.length() < 5 || string.length() > 12) {
            failures.add(ERROR_PASSWORD_LENGTH);
        }
    }
}
@服务
公共类PasswordValidationServiceImpl实现PasswordValidationService{
公共静态最终字符串为空\u或\u NULL\u PASSWORD=“密码不应为空”;
公共静态最终字符串错误\u PASSWORD\u LENGTH=“密码长度必须介于5到12个字符之间。”;
public static final String ERROR\u PASSWORD\u CASE=“密码必须仅包含小写字母。”;
公共静态最终字符串错误\u LETTER\u和\u DIGIT=“密码必须同时包含字母和数字。”;
public static final String ERROR\u PASSWORD\u SEQUENCE\u REPEATED=“密码不能包含紧跟同一序列的任何字符序列。”;
私有模式checkCasePattern=Pattern.compile(“[A-Z]”);
专用图案CheckletAndDigit=图案
.编译((?=.[a-z])(?=.[0-9]);
私有模式checkSequenceRepeation=Pattern.compile(“(\\w{2,})\\1”);
/**
*@param密码
*@return List此方法调用4个以上的方法进行验证
*密码和返回错误列表(如果有)。
*/
公共列表验证密码(字符串密码){
列表失败=新建ArrayList();
if(StringUtils.isEmpty(密码)){
添加(空密码或空密码);
返回失败;
}否则{
检查长度(密码、失败);
checkCase(密码、失败);
Checkletter和Digit(密码、失败);
CheckSequenceRepeation(密码、失败);
返回失败;
}
}
/**
*@param密码
*@param失败
*此方法将验证是否存在任何重复字符
*序列,如果找到它将向失败列表添加错误消息。
*/
私有void checksequencerepeation(字符串密码,列表失败){
Matcher Matcher=checkSequenceRepeation.Matcher(密码);
if(matcher.find()){
失败。添加(错误\密码\序列\重复);
}
}
/**
*@param密码
*@param失败
*此方法将验证中的字母和字符
*密码,如果找不到,则向失败添加错误消息
*名单。
*/
私有无效的checkLetterAndDigit(字符串密码,列表失败){
Matcher Matcher=支票和数字。Matcher(密码);
如果(!matcher.find()){
失败。添加(错误字母和数字);
}
}
/**
*@param密码
*@param失败
*此方法检查文本中的大写和小写字母
*密码如果有任何大写字母,它将添加错误
*将消息发送到故障列表。
*/
私有void checkCase(字符串密码,列表失败){
Matcher Matcher=checkCasePattern.Matcher(密码);
if(matcher.find()){
失败。添加(错误\密码\案例);
}
}
/**
*@param字符串
*@param失败
*如果字符串为空,此方法将检查字符串的长度
*少于5个或超过12个字符,则会添加错误
*将消息放入故障列表
*/
私有void checkLength(字符串、列表失败){
if(string.length()<5 | | string.length()>12){
失败。添加(错误\密码\长度);
}
}
}

现在我的要求是使这个类是可扩展的,所以,在将来,如果我想添加更多规则/删除一些规则,代码更改应该是最小的。我怎样才能做到这一点?如有任何建议,我们将不胜感激

您可以将PasswordValidationService定义为某种新的抽象类PasswordRule列表或集合

这样,PasswordValidationService将在且仅当满足每个PasswordRule时返回“password is valid”

如果要添加新规则,只需将其定义为新的PasswordRules并将其添加到PasswordValidationService实例中

编辑:添加了示例代码

每个新规则都需要实现的抽象类:

public abstract class PasswordRule{
    private String errorString;

    abstract public boolean check(String password){
        //implement the rule
    }

    public String getError(){
        return errorString;
    }
}
扩展PasswordRule抽象类的类,即密码不为空:

public class PasswordNotEmpty extends PasswordRule{
    private String errorString;

    public PasswordNotEmpty(){
        errorString = "Password Should not be empty";
    }

    public boolean check(String password){
        return StringUtils.isEmpty(password);
    }
}
最后是PasswordValidationService:

public class PasswordValidator implements PasswordValidationService{
    private Set<PasswordRule> rules = new HashSet<PasswordRules>();

    public PasswordValidator(PasswordRule... args){
        for(PasswordRule r : args)
            rules.add(r);
    }

    public List<String> validate(String password){
        List<String> failures = new ArrayList<String>();
        for(PasswordRule r : rules)
            if(!r.check(password))
                failures.add(r.getError());
        return failures;
    }
}
公共类PasswordValidator实现PasswordValidationService{
私有集规则=新HashSet();
公共密码验证器(密码规则…参数){
for(密码规则r:args)
规则.添加(r);
}
公共列表验证(字符串密码){
列表失败=新建ArrayList();
for(密码规则r:规则)
如果(!r.check(密码))
添加(r.getError());
返回失败;
}
}
它的用途将与此类似:

PasswordRule rule1 = new PasswordNotEmpty();
PasswordValidationService v = new PasswordValidator(rule1);
List<String> errors = v.validate("somePassword");
PasswordRule规则1=new PasswordNotEmpty();
PasswordValidationService v=新的PasswordValidator(规则1);
列表错误=v.validate(“somePassword”);

您可以将PasswordValidationService定义为某种新的抽象类PasswordRule列表或集合

这样,PasswordValidationService将在且仅当满足每个PasswordRule时返回“password is valid”

如果要添加新规则,只需将其定义为新的PasswordRules并将其添加到PasswordValidationService实例中

编辑:添加了示例代码

每个新规则都需要实现的抽象类:

public abstract class PasswordRule{
    private String errorString;

    abstract public boolean check(String password){
        //implement the rule
    }

    public String getError(){
        return errorString;
    }
}
扩展PasswordRule抽象类的类,即密码不为空:

public class PasswordNotEmpty extends PasswordRule{
    private String errorString;

    public PasswordNotEmpty(){
        errorString = "Password Should not be empty";
    }

    public boolean check(String password){
        return StringUtils.isEmpty(password);
    }
}
最后是PasswordValidationService:

public class PasswordValidator implements PasswordValidationService{
    private Set<PasswordRule> rules = new HashSet<PasswordRules>();

    public PasswordValidator(PasswordRule... args){
        for(PasswordRule r : args)
            rules.add(r);
    }

    public List<String> validate(String password){
        List<String> failures = new ArrayList<String>();
        for(PasswordRule r : rules)
            if(!r.check(password))
                failures.add(r.getError());
        return failures;
    }
}
公共类PasswordValidator实现PasswordValidationService{
私有集规则=新HashSet();
公共密码验证器(密码规则…参数){
for(密码规则r:args)
规则.添加(r);
}