Passay-quick-guide

提供:Dev Guides
移動先:案内検索

Passay-概要

*Passay* は、Javaベースのパスワード生成および検証ライブラリです。 パスワードを検証/生成するための包括的な機能リストを提供し、高度な設定が可能です。

Passayコンポーネント

Passay APIには3つのコアコンポーネントがあります。

  • ルール-パスワードポリシールールセットを定義する1つ以上のルール。
  • PasswordValidator -指定されたルールセットに対してパスワードを検証する検証コンポーネント。
  • PasswordGenerator -指定されたルールセットを満たすパスワードを生成するジェネレータコンポーネント。

ルールの概要

ルールは、パスワードの検証と生成の両方の基盤ブロックです。 ルールには大きく2つのカテゴリがあります。

  • 正の一致では、パスワードがルールを満たす必要があります。
  • 負の一致は、ルールを満たすパスワードを拒否します。

特徴

Passayライブラリが提供する機能の一部を以下に示します。

  • パスワードの検証-Passayライブラリは、構成可能なルールセットに対してパスワードを検証することにより、パスワードポリシーの実施を支援します。 一般的なユースケースのための既存のルールの豊富なセットがあります。 追加のケースでは、カスタムルールを実装するためのシンプルなRuleインターフェースを提供します。
  • パスワード生成-パスワードの生成にも使用できる構成可能なルールセットを提供します。
  • コマンドラインツール-パスワードポリシーの実施を自動化するツールを提供します。
  • 便利-使いやすい。
  • 拡張可能-すべてのPassayコンポーネントは拡張可能です。
  • 内部化をサポート-Passayのコンポーネントは国際化に対応しています。

Passay-環境設定

ローカル環境のセットアップ

Javaプログラミング言語用に環境をセットアップする場合は、このセクションで、マシンにJavaをダウンロードしてセットアップする方法を説明します。 環境をセットアップするには、以下の手順に従ってください。

Java SEは、https://java.sun.com/javase/downloads/index_jdk5.jsp [Javaのダウンロード]リンクから無料で入手できます。 そのため、オペレーティングシステムに基づいてバージョンをダウンロードします。

指示に従ってJavaをダウンロードし、*。exe *を実行して、マシンにJavaをインストールします。 マシンにJavaをインストールしたら、正しいインストールディレクトリを指すように環境変数を設定する必要があります-

Windows 2000/XPのパスのセットアップ

_c:\ Program Files \ java \ jdk_ディレクトリにJavaがインストールされていると想定しています-

  • 「マイコンピュータ」を右クリックして、「プロパティ」を選択します。
  • 「詳細」タブの下の「環境変数」ボタンをクリックします。
  • ここで、「Path」変数を変更して、Java実行可能ファイルへのパスも含まれるようにします。 たとえば、パスが現在「C:\ WINDOWS \ SYSTEM32」に設定されている場合、パスを「C:\ WINDOWS \ SYSTEM32; c:\ Program Files \ java \ jdk \ bin」に変更します。

Windows 95/98/MEのパスのセットアップ

_c:\ Program Files \ java \ jdk_ディレクトリにJavaがインストールされていると想定しています-

  • 「C:\ autoexec.bat」ファイルを編集し、最後に次の行を追加します-「SET PATH =%PATH%; C:\ Program Files \ java \ jdk \ bin」

Linux、UNIX、Solaris、FreeBSDのパスのセットアップ

環境変数PATHは、Javaバイナリがインストールされている場所を指すように設定する必要があります。 これがうまくいかない場合は、シェルのドキュメントを参照してください。

たとえば、bashをシェルとして使用する場合は、 '。bashrcの最後に次の行を追加します:export PATH =/path/to/java:$ PATH'

人気のあるJavaエディター

Javaプログラムを作成するには、テキストエディターが必要です。 市場には多くの洗練されたIDEがあります。 しかし、今のところ、次のいずれかを検討することができます-

Passay Archiveをダウンロード

Passay jarファイルの最新バージョンをhttps://mvnrepository.com/artifact/org.passay/passay[Maven Repository-]からダウンロードします。 このチュートリアルでは、passay-1.4.0.jarがダウンロードされ、C:\> passayフォルダーにコピーされます。

OS Archive name
Windows passay-1.4.0.jar
Linux passay-1.4.0.jar
Mac passay-1.4.0.jar

Passay環境を設定する

*PASSAY* 環境変数を設定して、Passay jarがマシンに保存されているベースディレクトリの場所を指すようにします。 次のように、さまざまなオペレーティングシステムのPassayフォルダーにpassay-1.4.0.jarを抽出したと仮定します。
OS Output
Windows Set the environment variable PASSAY to C:\Passay
Linux export PASSAY=/usr/local/Passay
Mac export PASSAY=/Library/Passay

CLASSPATH変数を設定する

Passay jarの場所を指すように CLASSPATH 環境変数を設定します。 次のように、さまざまなオペレーティングシステムのPassayフォルダーにpassay-1.4.0.jarを保存しているとします。

OS Output
Windows Set the environment variable CLASSPATH to %CLASSPATH%;%Passay%\passay-1.4.0.jar;.;
Linux export CLASSPATH=$CLASSPATH:$PASSAY/passay-1.4.0.jar:.
Mac export CLASSPATH=$CLASSPATH:$PASSAY/passay-1.4.0.jar:.

Passay-パスワード検証

典型的なパスワードポリシーには、組織のルールに準拠している場合にパスワードをチェックするための一連のルールが含まれています。 次のポリシーを考慮してください。

  • パスワードの長さは8〜16文字にする必要があります。
  • パスワードには空白を含めないでください。
  • パスワードには、上、下、数字、記号のそれぞれが含まれている必要があります。

次の例は、Passayライブラリを使用した上記のポリシーに対するパスワードの検証を示しています。

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

import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.Rule;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;

public class PassayExample {
   public 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(EnglishCharacterData.UpperCase, 1));
     //Rule 3.b: At least one Lower-case character
      rules.add(new CharacterRule(EnglishCharacterData.LowerCase, 1));
     //Rule 3.c: At least one digit
      rules.add(new CharacterRule(EnglishCharacterData.Digit, 1));
     //Rule 3.d: At least one special character
      rules.add(new CharacterRule(EnglishCharacterData.Special, 1));

      PasswordValidator validator = new PasswordValidator(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));
        }
    }
}

出力

Password validated.

Passay-カスタマイズされたメッセージ

Passayライブラリーは、バリデーターが使用するデフォルトのメッセージをオーバーライドするMessageResolver APIを提供します。 カスタムプロパティファイルへのパスを取得し、標準のhttp://www.passay.org/reference/[keys]を使用して必要なメッセージをオーバーライドできます。

以下の例は、パスワードの検証とPassayライブラリを使用したカスタムメッセージを示しています。

messages.properties

INSUFFICIENT_UPPERCASE=Password missing at least %1$s uppercase characters.
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.]

Passay-MのNルール

多くの場合、パスワードポリシーは、パスワードなどの特定のルールのうちの最小ルールへの準拠を義務付けており、少なくともM of Nルールに準拠する必要があります。 次のポリシーを考慮してください。

  • パスワードの長さは8〜16文字にする必要があります。
  • パスワードには空白を含めないでください。
  • パスワードには、上、下、数字、または記号のうち少なくとも3つを含める必要があります。

次の例は、Passayライブラリを使用した上記のポリシーに対するパスワードの検証を示しています。

import java.io.FileNotFoundException;
import java.io.IOException;

import org.passay.CharacterCharacteristicsRule;
import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.Rule;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;

public class PassayExample {
   public static void main(String[] args) throws FileNotFoundException, IOException {
     //Rule 1: Password length should be in between
     //8 and 16 characters
      Rule rule1 = new LengthRule(8, 16);
     //Rule 2: No whitespace allowed
      Rule rule2 = new WhitespaceRule();
      CharacterCharacteristicsRule rule3 = new CharacterCharacteristicsRule();
     //M - Mandatory characters count
      rule3.setNumberOfCharacteristics(3);
     //Rule 3.a: One Upper-case character
      rule3.getRules().add(new CharacterRule(EnglishCharacterData.UpperCase, 1));
     //Rule 3.b: One Lower-case character
      rule3.getRules().add(new CharacterRule(EnglishCharacterData.LowerCase, 1));
     //Rule 3.c: One digit
      rule3.getRules().add(new CharacterRule(EnglishCharacterData.Digit, 1));
     //Rule 3.d: One special character
      rule3.getRules().add(new CharacterRule(EnglishCharacterData.Special, 1));

      PasswordValidator validator = new PasswordValidator(rule1, rule2, rule3);
      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));
      }
   }
}

出力

Password validated.

Passay-パスワード生成

*PasswordGenerator* は、指定されたポリシーを使用してパスワードを生成するのに役立ちます。 次のポリシーを考慮してください。
  • パスワードの長さは8文字でなければなりません。
  • パスワードには、上、下、数字、記号のそれぞれが含まれている必要があります。

次の例は、Passayライブラリを使用した上記のポリシーに対するパスワードの生成を示しています。

import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.PasswordGenerator;

public class PassayExample {
   public static void main(String[] args) {
      CharacterRule alphabets = new CharacterRule(EnglishCharacterData.Alphabetical);
      CharacterRule digits = new CharacterRule(EnglishCharacterData.Digit);
      CharacterRule special = new CharacterRule(EnglishCharacterData.Special);

      PasswordGenerator passwordGenerator = new PasswordGenerator();
      String password = passwordGenerator.generatePassword(8, alphabets, digits, special);
      System.out.println(password);
   }
}

出力

?\DE~@c3

Passay-AllowedCharacterRule

*AllowedCharacterRule* を使用すると、パスワードに含めることができる文字を指定できます。 次の例を考えてください。

次の例は、Passayライブラリを使用した上記のポリシーに対するパスワードの検証を示しています。

import org.passay.AllowedCharacterRule;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.Rule;
import org.passay.RuleResult;

public class PassayExample {
   public static void main(String[] args) {
     //Rule: Password should contains only a, b and c
      Rule rule1 = new AllowedCharacterRule(new char[] {'a', 'b', 'c'});
     //8 and 16 characters
      Rule rule2 = new LengthRule(8, 16);

      PasswordValidator validator = new PasswordValidator(rule1, rule2);
      PasswordData password = new PasswordData("abcabcab1");
      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 contains the illegal character '1'.]

Passay-AllowedRegexRule

*AllowedRegexRule* を使用すると、パスワードが満たすべき通常のパターンを指定できます。 次の例を考えてください。

次の例は、Passayライブラリを使用した上記のポリシーに対するパスワードの検証を示しています。

import org.passay.AllowedRegexRule;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.Rule;
import org.passay.RuleResult;

public class PassayExample {
   public static void main(String[] args) {
     //Rule: Password should contains alphabets only
      Rule rule1 = new AllowedRegexRule("^[A-Za-z]+$");
     //8 and 16 characters
      Rule rule2 = new LengthRule(8, 16);

      PasswordValidator validator = new PasswordValidator(rule1, rule2);
      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 must match pattern '^[A-Za-z]+$'.]

Passay-CharacterRule

*CharacterRule* は、一連の文字と最小番号の定義に役立ちます。 パスワードに必要な文字数。

次の例は、Passayライブラリを使用した上記のポリシーに対するパスワードの検証を示しています。

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

import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.Rule;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;

public class PassayExample {
   public 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(EnglishCharacterData.UpperCase, 1));
     //Rule 3.b: At least one Lower-case character
      rules.add(new CharacterRule(EnglishCharacterData.LowerCase, 1));
     //Rule 3.c: At least one digit
      rules.add(new CharacterRule(EnglishCharacterData.Digit, 1));
     //Rule 3.d: At least one special character
      rules.add(new CharacterRule(EnglishCharacterData.Special, 1));

      PasswordValidator validator = new PasswordValidator(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));
      }
   }
}

出力

Password validated.

Passay-LengthRule

*LengthRule* は、パスワードの最小長と最大長を定義するのに役立ちます。

次の例は、Passayライブラリを使用した上記のポリシーに対するパスワードの検証を示しています。

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

import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.Rule;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;

public class PassayExample {
   public 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(EnglishCharacterData.UpperCase, 1));
     //Rule 3.b: At least one Lower-case character
      rules.add(new CharacterRule(EnglishCharacterData.LowerCase, 1));
     //Rule 3.c: At least one digit
      rules.add(new CharacterRule(EnglishCharacterData.Digit, 1));
     //Rule 3.d: At least one special character
      rules.add(new CharacterRule(EnglishCharacterData.Special, 1));

      PasswordValidator validator = new PasswordValidator(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));
      }
   }
}

出力

Password validated.

Passay-キャラクター特性ルール

*CharacterCharacteristicsRule* は、パスワードがN個の定義済みルールを満たしているかどうかを定義するのに役立ちます。

次の例は、Passayライブラリを使用した上記のポリシーに対するパスワードの検証を示しています。

import java.io.FileNotFoundException;
import java.io.IOException;

import org.passay.CharacterCharacteristicsRule;
import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.Rule;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;

public class PassayExample {
   public static void main(String[] args) throws FileNotFoundException, IOException {
     //Rule 1: Password length should be in between
     //8 and 16 characters
      Rule rule1 = new LengthRule(8, 16);
     //Rule 2: No whitespace allowed
      Rule rule2 = new WhitespaceRule();
      CharacterCharacteristicsRule rule3 = new CharacterCharacteristicsRule();
     //M - Mandatory characters count
      rule3.setNumberOfCharacteristics(3);
     //Rule 3.a: One Upper-case character
      rule3.getRules().add(new CharacterRule(EnglishCharacterData.UpperCase, 1));
     //Rule 3.b: One Lower-case character
      rule3.getRules().add(new CharacterRule(EnglishCharacterData.LowerCase, 1));
     //Rule 3.c: One digit
      rule3.getRules().add(new CharacterRule(EnglishCharacterData.Digit, 1));
     //Rule 3.d: One special character
      rule3.getRules().add(new CharacterRule(EnglishCharacterData.Special, 1));

      PasswordValidator validator = new PasswordValidator(rule1, rule2, rule3);
      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));
      }
   }
}

出力

Password validated.

Passay-LengthComplexityRule

*LengthComplexityRule* は、長さに基づいてパスワードに適用可能なルールを定義するのに役立ちます。 次のポリシーを考慮してください。
  • パスワードの長さが1〜5文字の場合、小文字のアルファベットのみが許可されます。
  • パスワードの長さが6〜8文字の場合、a、b、cのみが許可されます。

次の例は、Passayライブラリを使用した上記のポリシーに対するパスワードの検証を示しています。

import org.passay.AllowedCharacterRule;
import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.LengthComplexityRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.RuleResult;

public class PassayExample {
   public static void main(String[] args) {
      LengthComplexityRule lengthComplexityRule = new LengthComplexityRule();
     //Rule: Password of 1 to 5 characters should contains lower case alphabets only
      lengthComplexityRule.addRules("[1,5]",
         new CharacterRule(EnglishCharacterData.LowerCase, 5));
     //8 and 16 characters
      lengthComplexityRule.addRules("[6,8]",
         new AllowedCharacterRule(new char[] { 'a', 'b', 'c' }));
      PasswordValidator validator = new PasswordValidator(lengthComplexityRule);
      PasswordData password = new PasswordData("abcdef");
      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 contains the illegal character 'd'.,
Password contains the illegal character 'e'.,
Password contains the illegal character 'f'.,
Password meets 0 complexity rules, but 1 are required.]

Passay-IllegalCharacterRule

*IllegalCharacterRule* を使用すると、パスワードで許可されていない文字を指定できます。 次の例を考えてください。

import org.passay.IllegalCharacterRule;
import org.passay.NumberRangeRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;

public class PassayExample {
   public static void main(String[] args) {
     //Rule: Special characters like &, <, > are not allowed in a password
      IllegalCharacterRule illegalCharacterRule
         = new IllegalCharacterRule(new char[] {'&', '<', '>'});

     //Rule: 1 to 5 numbers are not allowed
      NumberRangeRule numberRangeRule = new NumberRangeRule(1, 5);

     //Rule: White spaces are not allowed
      WhitespaceRule whitespaceRule = new WhitespaceRule();

      PasswordValidator validator
         = new PasswordValidator(illegalCharacterRule,numberRangeRule,whitespaceRule);
      PasswordData password = new PasswordData("abc&4d  ef6");
      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 contains the illegal character '&'.,
Password contains the number '4'.,
Password contains a whitespace character.]

Passay-NumberRangeRule

*NumberRangeRule* を使用すると、パスワードで許可されていない番号の範囲を指定できます。 次の例を考えてください。

import org.passay.IllegalCharacterRule;
import org.passay.NumberRangeRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;

public class PassayExample {
   public static void main(String[] args) {
     //Rule: Special characters like &, <, > are not allowed in a password
      IllegalCharacterRule illegalCharacterRule
         = new IllegalCharacterRule(new char[] {'&', '<', '>'});

     //Rule: 1 to 5 numbers are not allowed
      NumberRangeRule numberRangeRule = new NumberRangeRule(1, 5);

     //Rule: White spaces are not allowed
      WhitespaceRule whitespaceRule = new WhitespaceRule();

      PasswordValidator validator
         = new PasswordValidator(illegalCharacterRule,numberRangeRule,whitespaceRule);
      PasswordData password = new PasswordData("abc&4d  ef6");
      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 contains the illegal character '&'.,
Password contains the number '4'.,
Password contains a whitespace character.]

Passay-WhitespaceRule

*WhitespaceRule* を使用すると、パスワードに空白を使用できないように指定できます。 次の例を考えてください。

import org.passay.IllegalCharacterRule;
import org.passay.NumberRangeRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;

public class PassayExample {
   public static void main(String[] args) {
     //Rule: Special characters like &, <, > are not allowed in a password
      IllegalCharacterRule illegalCharacterRule
         = new IllegalCharacterRule(new char[] {'&', '<', '>'});

     //Rule: 1 to 5 numbers are not allowed
      NumberRangeRule numberRangeRule = new NumberRangeRule(1, 5);

     //Rule: White spaces are not allowed
      WhitespaceRule whitespaceRule = new WhitespaceRule();

      PasswordValidator validator
         = new PasswordValidator(illegalCharacterRule,numberRangeRule,whitespaceRule);
      PasswordData password = new PasswordData("abc&4d  ef6");
      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 contains the illegal character '&'.,
Password contains the number '4'.,
Password contains a whitespace character.]

Passay-DictionaryRule

*DictionaryRule* では、特定の単語がパスワードとして指定されていないかどうかを確認できます。 次の例を考えてください。

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 PassayExample {
   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));
      }
   }
}

出力

Invalid Password: [Password contains the dictionary word 'password'.]

Passay-DictionarySubstringRule

*DictionarySubstringRule* では、特定の単語がパスワードの一部ではないかどうかを確認できます。 次の例を考えてください。

import org.passay.DictionarySubstringRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.RuleResult;
import org.passay.dictionary.ArrayWordList;
import org.passay.dictionary.WordListDictionary;

public class PassayExample {
   public static void main(String[] args) {
      WordListDictionary wordListDictionary = new WordListDictionary(
         new ArrayWordList(new String[] { "password", "username" }));
      DictionarySubstringRule dictionaryRule = new DictionarySubstringRule(wordListDictionary);
      PasswordValidator validator = new PasswordValidator(dictionaryRule);
      PasswordData password = new PasswordData("password@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 contains the dictionary word 'password'.]

Passay-HistoryRule

*HistoryRule* を使用すると、指定されたパスワードが過去に使用されていないかどうかを確認できます。 次の例を考えてください。

import org.passay.HistoryRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.RuleResult;
import org.passay.SourceRule;

public class PassayExample {
   public static void main(String[] args) {
      SourceRule sourceRule = new SourceRule();
      HistoryRule historyRule = new HistoryRule();
      PasswordValidator validator = new PasswordValidator(sourceRule, historyRule);
      PasswordData password = new PasswordData("password@123");
      password.setPasswordReferences(
         new PasswordData.SourceReference("source", "password"),
         new PasswordData.HistoricalReference("password@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 matches one of 1 previous passwords.]

Passay-RepeatCharacterRegexRule

*RepeatCharacterRegexRule* を使用すると、指定されたパスワードにASCII文字が繰り返されているかどうかを確認できます。 次の例を考えてください。

import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.RepeatCharacterRegexRule;
import org.passay.Rule;
import org.passay.RuleResult;

public class PassayExample {
   public static void main(String[] args) {
     //Rule: Password should not contain repeated entries
      Rule rule1 = new RepeatCharacterRegexRule(3);
     //8 and 16 characters
      Rule rule2 = new LengthRule(8, 16);

      PasswordValidator validator = new PasswordValidator(rule1, rule2);
      PasswordData password = new PasswordData("aaefhehhhhh");
      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 matches the illegal pattern 'hhh'.]

Passay-UsernameRule

*UsernameRule* は、パスワードにユーザー名が含まれないようにします。 次の例を考えてください。

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

public class PassayExample {
   public static void main(String[] args) {
     //Rule: Password should not contain user-name
      Rule rule = new UsernameRule();

      PasswordValidator validator = new PasswordValidator(rule);
      PasswordData password = new PasswordData("microsoft");
      password.setUsername("micro");
      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 contains the user id 'micro'.]