- Passay 教程
- Passay − 主頁
- Passay − 概覽
- Passay − 環境設定
- 驗證/生成
- Passay − 密碼驗證
- Passay − 自定義訊息
- Passay − M of N 規則
- Passay − 密碼生成
- 正匹配規則
- passay − AllowedCharacterRule
- Passay − AllowedRegexRule
- Passay −CharacterRule
- passay − LengthRule
- Passay − CharacterCharacteristicsRule
- Passay − LengthComplexityRule
- 負匹配規則
- Passay − lllegalCharacterRule
- Passay − NumberRangeRule
- Passay − WhitespaceRule
- Passay − DictionaryRule
- Passay − DictionarySubstringRule
- Passay − HistoryRule
- passay − RepeatCharacterRegexRule
- Passay − usernameRule
- Passay 有用資源
- Passay - 快速指南
- Passay - 資源
- Passay - 討論
Passay − 自定義訊息
Passay 庫提供了一個 MessageResolver API 來覆蓋驗證器所使用的預設訊息。它可以找到自定義屬性檔案的路徑,並且使用標準 鍵 來覆蓋所需訊息。
示例
以下示例顯示使用 Passay 庫驗證密碼以及顯示自定義訊息。
messages.properties
INSUFFICIENT_UPPERCASE=Password missing at least %1$s uppercase characters.
PassayExample.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.LengthRule;
import org.passay.MessageResolver;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.PropertiesMessageResolver;
import org.passay.Rule;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;
public class PassayExample {
public static void main(String[] args) throws FileNotFoundException, IOException {
List<Rule> rules = new ArrayList<>();
rules.add(new LengthRule(8, 16));
rules.add(new WhitespaceRule());
rules.add(new CharacterRule(EnglishCharacterData.UpperCase, 1));
rules.add(new CharacterRule(EnglishCharacterData.LowerCase, 1));
rules.add(new CharacterRule(EnglishCharacterData.Digit, 1));
rules.add(new CharacterRule(EnglishCharacterData.Special, 1));
Properties props = new Properties();
props.load(new FileInputStream("E:/Test/messages.properties"));
MessageResolver resolver = new PropertiesMessageResolver(props);
PasswordValidator validator = new PasswordValidator(resolver, rules);
PasswordData password = new PasswordData("microsoft@123");
RuleResult result = validator.validate(password);
if(result.isValid()){
System.out.println("Password validated.");
}else{
System.out.println("Invalid Password: " + validator.getMessages(result));
}
}
}
輸出
Invalid Password: [Password missing at least 1 uppercase characters.]
廣告