• Add the passay dependency:

pom.yml:

<properties>
  <passay.version>1.6.4</passay.version>
</properties>

<dependency>
  <groupId>org.passay</groupId>
  <artifactId>passay</artifactId>
  <version>${passay.version}</version>
</dependency>
  • Instantiate the password validator with these rules, for instance:

Between 8 and 16 charactes in length
At least 1 upper case letter
At least 1 lower case letter
At least 1 alphabetical letter
At least 1 digit
At least 1 special symbol

SomeClass.java:

private final PasswordValidator validator;
// ...

this.validator = new PasswordValidator(
  new LengthRule(8, 16),
  new CharacterRule(EnglishCharacterData.UpperCase, 1),
  new CharacterRule(EnglishCharacterData.LowerCase, 1),
  new CharacterRule(EnglishCharacterData.Alphabetical, 1),
  new CharacterRule(EnglishCharacterData.Digit, 1),
  new CharacterRule(EnglishCharacterData.Special, 1)
);

// ...
  • Validate an input:
return this.validator.validate(new PasswordData(password)).isValid();