我正在为密码策略编写代码。
政策规定您不能拥有与已使用过的相同的字母。 例如:密码 - 您不能使用密码,因为它有两个 '
我怎样才能做到这一点?
编辑:
这是我的完整实现:
private static final String PASSWORD_DUPLICATE_CHARACTERS = "^(?:([a-zA-Z])(?!.*\\1))$";
pattern = Pattern.compile(PASSWORD_DUPLICATE_CHARACTERS);
this.checkForDuplicateLetters(LDAPNewUserPassword);
private boolean checkForDuplicateLetters(final String newPassword) throws LDAPException{
LoggingEnt userEnt = new LoggingEnt();
String userid = userEnt.getUseridCode();
boolean foundDuplicate = false;
matcher = pattern.matcher(newPassword);
if (newPassword.matches(PASSWORD_DUPLICATE_LETTERS)){
foundDuplicate = true;
userEnt.setMsg1("Duplicate.");
throw new LDAPException("Invalid password combination for " + userid, LDAPException.INVALID_CREDENTIALS);//BAristo
} else {
userEnt.setMsg1("Your password has been successfully changed.");
}
return matcher.matches();
}
最佳答案
使用这个正则表达式:
private static final String PASSWORD_PATTERN_LOWER_8 = "^(?:([a-zA-Z])(?!.*\\1))$";
关于java - 如何匹配字符串中的1个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17609398/