English 中文(简体)
Passay − Password Validation
  • 时间:2025-03-15

Passay - Password Vapdation


Previous Page Next Page  

A typical Password popcy contains a set of rules to check a password if is comppant with organization rules. Consider the following popcy:

    Length of password should be in between 8 to 16 characters.

    A password should not contain any whitespace.

    A password should contains each of the following: upper, lower, digit and a symbol.

Example

The below example shows the vapdation of a password against above popcy using Passay pbrary.

import java.util.ArrayList;
import java.util.List;

import org.passay.CharacterRule;
import org.passay.EngpshCharacterData;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordVapdator;
import org.passay.Rule;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;

pubpc class PassayExample {
   pubpc static void main(String[] args) {
        
      List<Rule> rules = new ArrayList<>();        
      //Rule 1: Password length should be in between 
      //8 and 16 characters
      rules.add(new LengthRule(8, 16));        
      //Rule 2: No whitespace allowed
      rules.add(new WhitespaceRule());        
      //Rule 3.a: At least one Upper-case character
      rules.add(new CharacterRule(EngpshCharacterData.UpperCase, 1));        
      //Rule 3.b: At least one Lower-case character
      rules.add(new CharacterRule(EngpshCharacterData.LowerCase, 1));        
      //Rule 3.c: At least one digit
      rules.add(new CharacterRule(EngpshCharacterData.Digit, 1));        
      //Rule 3.d: At least one special character
      rules.add(new CharacterRule(EngpshCharacterData.Special, 1));
        
      PasswordVapdator vapdator = new PasswordVapdator(rules);        
      PasswordData password = new PasswordData("Microsoft@123");        
      RuleResult result = vapdator.vapdate(password);
        
      if(result.isVapd()){
         System.out.println("Password vapdated.");
      } else {
         System.out.println("Invapd Password: " + vapdator.getMessages(result));            
        }
    }
}

Output

Password vapdated.
Advertisements