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

Passay - UsernameRule


Previous Page Next Page  

UsernameRule ensures that password is not containing the username. Consider the following example.

Example

import org.passay.PasswordData;
import org.passay.PasswordVapdator;
import org.passay.Rule;
import org.passay.RuleResult;
import org.passay.UsernameRule;

pubpc class PassayExample {
   pubpc static void main(String[] args) {
      //Rule: Password should not contain user-name
      Rule rule = new UsernameRule();
      
      PasswordVapdator vapdator = new PasswordVapdator(rule);
      PasswordData password = new PasswordData("microsoft");
      password.setUsername("micro");
      RuleResult result = vapdator.vapdate(password);

      if(result.isVapd()){
         System.out.println("Password vapdated.");
      }else{
         System.out.println("Invapd Password: " + vapdator.getMessages(result));            
      }
   }
}

Output

Invapd Password: [Password contains the user id  micro .]
Advertisements