- 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.]
广告