Passay - DictionaryRule
DictionaryRule helps allows to check if certain words are not specified as password.
Example - Validating Password as per Dictionary Rule
The below example shows validation of an invalid password as per Dictionary Rule.
PassayDemo.java
package com.tutorialspoint;
import org.passay.DictionaryRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.RuleResult;
import org.passay.dictionary.ArrayWordList;
import org.passay.dictionary.WordListDictionary;
public class PassayDemo {
public static void main(String[] args) {
WordListDictionary wordListDictionary = new WordListDictionary(
new ArrayWordList(new String[] { "password", "username" }));
DictionaryRule dictionaryRule = new DictionaryRule(wordListDictionary);
PasswordValidator validator = new PasswordValidator(dictionaryRule);
PasswordData password = new PasswordData("password");
RuleResult result = validator.validate(password);
if(result.isValid()){
System.out.println("Password validated.");
}else{
System.out.println("Invalid Password: " + validator.getMessages(result));
}
}
}
Output
Compile and run the code to verify the result −
Invalid Password: [Password contains the dictionary word 'password'.]
Example - Validating a Valid Password as per Dictionary Rule.
The below example shows validation of a valid password as per Dictionary Rule.
PassayDemo.java
package com.tutorialspoint;
import org.passay.DictionaryRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.RuleResult;
import org.passay.dictionary.ArrayWordList;
import org.passay.dictionary.WordListDictionary;
public class PassayDemo {
public static void main(String[] args) {
WordListDictionary wordListDictionary = new WordListDictionary(
new ArrayWordList(new String[] { "password", "username" }));
DictionaryRule dictionaryRule = new DictionaryRule(wordListDictionary);
PasswordValidator validator = new PasswordValidator(dictionaryRule);
PasswordData password = new PasswordData("password1323");
RuleResult result = validator.validate(password);
if(result.isValid()){
System.out.println("Password validated.");
}else{
System.out.println("Invalid Password: " + validator.getMessages(result));
}
}
}
Output
Compile and run the code to verify the result −
Password validated.
Advertisements