我正在使用passay库根据一系列要求验证密码:
<dependencies>
<dependency>
<groupId>org.passay</groupId>
<artifactId>passay</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
我的要求之一是,密码中不得包含紧随其后的任何字符序列。例如:
我尝试使用RepeatCharactersRule
(javadocs),但是该Rule仅适用于相同字符的序列(它将找到'aaa',而不是'abab')。
密码是否有执行此操作的规则,或者我必须编写自定义规则?
答案 0 :(得分:1)
嗯,我不认为这样做,但是您不必编写自己的规则,如果需要,请尝试以下操作:
public class RepeatingSequenceRegexTest {
final PasswordValidator validator = new PasswordValidator(
new IllegalRegexRule("(.{1,})\\1")
);
@Test
public void checks() {
assertTrue(validator.validate(new PasswordData("normal")).isValid());
assertTrue(validator.validate(new PasswordData("abcdef")).isValid());
assertTrue(validator.validate(new PasswordData("abacadaeaf")).isValid());
assertTrue(validator.validate(new PasswordData("xyzxy")).isValid());
assertTrue(validator.validate(new PasswordData("12345")).isValid());
assertTrue(validator.validate(new PasswordData("121314")).isValid());
assertTrue(validator.validate(new PasswordData("1234123121")).isValid());
assertTrue(validator.validate(new PasswordData("123xy123")).isValid());
assertTrue(validator.validate(new PasswordData("1")).isValid());
assertTrue(validator.validate(new PasswordData("")).isValid());
assertFalse(validator.validate(new PasswordData("apple")).isValid());
assertFalse(validator.validate(new PasswordData("banana")).isValid());
assertFalse(validator.validate(new PasswordData("112233")).isValid());
assertFalse(validator.validate(new PasswordData("123123")).isValid());
assertFalse(validator.validate(new PasswordData("1212")).isValid());
assertFalse(validator.validate(new PasswordData("11")).isValid());
assertFalse(validator.validate(new PasswordData("123xy123xy")).isValid());
}
}