简体   繁体   中英

How To Mask a password input in java console

I'm building a login system in Java and I'm trying to mask the password input due to security measures but can't seem to find a way around it. This is what I'm Trying to do:

Username: User1

Password:******

Here's my code to give you an idea

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
    Scanner Input = new Scanner (System.in);
    System.out.println("Username:");
    String Username = Input.nextLine();
    System.out.println("Password:");
    String Password = Input.nextLine();
    }
}

Try readPassword() method of java It doesn't mask a * but it hide the input we type to console

cnsl = System.console();
char[] pwd = cnsl.readPassword("Password: ");
System.out.println("Password is: "+pwd);

like this.......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM