密码验证 8 位数字,包含大写、小写和一个特殊字符

新手上路,请多包涵

所以我写了一个让用户输入密码的方法,这个密码必须通过以下规范:

1. 长度至少为 8 位数字

2. 大写

3. 小写

4. 有特殊数字

我不确定为什么当我输入它时,输出不考虑特殊字符并引发错误。

到目前为止,这是我的代码:

 public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a given  password : ");
    String passwordhere = in.nextLine();
    System.out.print("Please re-enter the password to confirm : ");
    String confirmhere = in.nextLine();
    System.out.println("your password is: " + passwordhere);

    while (!passwordhere.equals(confirmhere) || !isValid(passwordhere)) {
        System.out.println("The password entered here  is invalid");
        System.out.print("Please enter the password again.it must be valid : ");
        String Passwordhere = in.nextLine();
        System.out.print("Please re-enter the password to confirm : ");

    }
}

public static boolean isValid(String passwordhere) {

    if (passwordhere.length() < 8) {
        return false;
    } else {

        for (int p = 0; p < passwordhere.length(); p++) {
            if (Character.isUpperCase(passwordhere.charAt(p))) {
            }
        }
        for (int q = 0; q < passwordhere.length(); q++) {
            if (Character.isLowerCase(passwordhere.charAt(q))) {
            }
        }
        for (int r = 0; r < passwordhere.length(); r++) {
            if (Character.isDigit(passwordhere.charAt(r))) {
            }
        }
        for (int s = 0; s < passwordhere.length(); s++) {
            if (Character.isSpecialCharacter(passwordhere.charAt(s))) {
            }
            }
            return true;
        }
}

另外,另一个问题是,假设用户输入 bob123 作为他们的密码。

我怎样才能让循环告诉用户它需要什么是正确的密码?

在上面的示例中,它缺少一个大写字母和一个符号(*&^..等)。

每次用户输入密码时,我如何添加它以打印出来,直到他们获得正确的密码以通过代码的所有规范?

原文由 Progamminnoob 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

你应该清楚地提到你的要求我不知道你的要求。请找到我下面的解决方案`

 public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a given  password : ");
    String passwordhere = in.nextLine();
    System.out.print("Please re-enter the password to confirm : ");
    String confirmhere = in.nextLine();

    List<String> errorList = new ArrayList<String>();

    while (!isValid(passwordhere, confirmhere, errorList)) {
        System.out.println("The password entered here  is invalid");
        for (String error : errorList) {
            System.out.println(error);
        }

        System.out.print("Please enter a given  password : ");
        passwordhere = in.nextLine();
        System.out.print("Please re-enter the password to confirm : ");
        confirmhere = in.nextLine();
    }
    System.out.println("your password is: " + passwordhere);

}

public static boolean isValid(String passwordhere, String confirmhere, List<String> errorList) {

    Pattern specailCharPatten = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
    Pattern UpperCasePatten = Pattern.compile("[A-Z ]");
    Pattern lowerCasePatten = Pattern.compile("[a-z ]");
    Pattern digitCasePatten = Pattern.compile("[0-9 ]");
    errorList.clear();

    boolean flag=true;

    if (!passwordhere.equals(confirmhere)) {
        errorList.add("password and confirm password does not match");
        flag=false;
    }
    if (passwordhere.length() < 8) {
        errorList.add("Password lenght must have alleast 8 character !!");
        flag=false;
    }
    if (!specailCharPatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one specail character !!");
        flag=false;
    }
    if (!UpperCasePatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one uppercase character !!");
        flag=false;
    }
    if (!lowerCasePatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one lowercase character !!");
        flag=false;
    }
    if (!digitCasePatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one digit character !!");
        flag=false;
    }

    return flag;

}

原文由 Ankur 发布,翻译遵循 CC BY-SA 3.0 许可协议

使用这个 bean 验证库进行密码验证:

https://github.com/ankurpathak/password-validation https://mvnrepository.com/artifact/com.github.ankurpathak.password/password-validation

它提供了许多约束来处理密码验证,并且在不久的将来会添加更多约束:

  1. ContainDigit:验证密码是否包含指定位数。
  2. ContainLowercase:验证密码是否包含指定数量的小写字母。
  3. ContainSpecial:验证密码是否包含指定数量的特殊符号。
  4. ContainUppercase:验证密码是否包含指定数量的大写字母。
  5. NotContainWhitespace:验证不应有任何空格。
  6. PasswordMatches:验证密码和确认密码是否相等。您还可以通过使用标志 showErrorOnConfirmPassword(默认为 true)移动约束以确认密码字段。

默认情况下,所有约束都会忽略空白,这样它将由 NotBlank 标准 bean 验证约束单独报告,同样我们可以使用每个约束的 ignoreBlank(默认为 true)标志。

使用该库的小例子是:


    @密码匹配
    公共类 PasswordDto {
     @Size(最小=8,最大=30)
     @NotContainWhitespace
     @ContainSpecial
     @ContainDigit
     私有字符串密码;
     @NotBlank
     私有字符串确认密码;
    }

原文由 Ankur Pathak 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题