简体   繁体   English

复杂密码的正则表达式

[英]Regular expression for complex password

I need to validate passwords that needs to fulfill the following rules: 我需要验证需要满足以下规则的密码:

  • Minimum length is 8 characters 最小长度为8个字符
  • All characters need to be unique 所有角色都必须是唯一的
  • At least one of the characters is capitalized 至少有一个角色是大写的
  • At least one character is a non-alphabetic character, a digit or a hyphen 至少一个字符是非字母字符,数字或连字符
  • Non-alphabetic characters should not be placed as the first two or last two characters 不应将非字母字符作为前两个或后两个字符

I can't figure out how to validate this using regular expressions. 我无法弄清楚如何使用正则表达式验证这一点。 Can anybody help me? 有谁能够帮助我?

^(?=.*[A-Z])(?=.*(?:\d|-))(?!.*(.).*\1)[a-zA-Z]{2}.{4,}[a-zA-Z]{2}$

Try this.See demo. 试试这个。看看演示。

https://regex101.com/r/eZ0yP4/12 https://regex101.com/r/eZ0yP4/12

As you can see step by step all conditions are met with the help of lookahead. 正如您可以逐步看到的所有条件都在前瞻的帮助下得到满足。

(?=.*[AZ]) ===at least one Capital (?=.*[AZ]) ===至少有一个资本

(?=.*(?:\\d|-)) ===at least one digit or - (?=.*(?:\\d|-)) ===至少一位数或-

(?!.*(.).*\\1) === no duplicates (?!.*(.).*\\1) ===没有重复

[a-zA-Z]{2}.{4,}[a-zA-Z]{2} ===alphabetic characters as first two and last two. [a-zA-Z]{2}.{4,}[a-zA-Z]{2} ===字母字符作为前两个和后两个字符。

You could use vt password library . 你可以使用vt密码库 Code is quite long, but it is easy to modify it and it is quite maintainable. 代码很长,但很容易修改它,并且它非常易于维护。

// password must be between 8 and 16 chars long
LengthRule lengthRule = new LengthRule(8, 16);

// don't allow whitespace
WhitespaceRule whitespaceRule = new WhitespaceRule();

// control allowed characters
CharacterCharacteristicsRule charRule = new CharacterCharacteristicsRule();
// require at least 1 digit in passwords
charRule.getRules().add(new DigitCharacterRule(1));
// require at least 1 non-alphanumeric char
charRule.getRules().add(new NonAlphanumericCharacterRule(1));
// require at least 1 upper case char
charRule.getRules().add(new UppercaseCharacterRule(1));
// require at least 1 lower case char
charRule.getRules().add(new LowercaseCharacterRule(1));
// require at least 3 of the previous rules be met
charRule.setNumberOfCharacteristics(3);
// don't allow alphabetical sequences
AlphabeticalSequenceRule alphaSeqRule = new AlphabeticalSequenceRule();

// don't allow numerical sequences of length 3
NumericalSequenceRule numSeqRule = new NumericalSequenceRule(3);

// don't allow qwerty sequences
QwertySequenceRule qwertySeqRule = new QwertySequenceRule();

// don't allow 4 repeat characters
RepeatCharacterRegexRule repeatRule = new RepeatCharacterRegexRule(4);

// group all rules together in a List
List<Rule> ruleList = new ArrayList<Rule>();
ruleList.add(lengthRule);
ruleList.add(whitespaceRule);
ruleList.add(charRule);
ruleList.add(alphaSeqRule);
ruleList.add(numSeqRule);
ruleList.add(qwertySeqRule);
ruleList.add(repeatRule);

PasswordValidator validator = new PasswordValidator(ruleList);
PasswordData passwordData = new PasswordData(new Password("testpassword"));

RuleResult result = validator.validate(passwordData);
if (result.isValid()) {
  System.out.println("Valid password");
} else {
  System.out.println("Invalid password:");
  for (String msg : validator.getMessages(result)) {
    System.out.println(msg);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM