如何在Java控制台中隐藏密码输入

huangapple go评论65阅读模式
英文:

How To Mask a password input in java console

问题

我正在Java中构建一个登录系统,我试图遮蔽密码输入以提高安全性,但似乎找不到解决方法。这是我尝试做的:

用户名:
User1

密码:******

这是我的代码,以便让你了解一下:

package com.company;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);
        System.out.println("用户名:");
        String Username = Input.nextLine();
        System.out.println("密码:");
        String Password = Input.nextLine();
    }
}
英文:

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();
	}
}

</details>


# 答案1
**得分**: 1

尝试使用Java的readPassword()方法:
它不会用*掩盖字符,但会隐藏我们在控制台上键入的输入。

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

就像这样……

英文:

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(&quot;Password: &quot;);
System.out.println(&quot;Password is: &quot;+pwd);

like this.......

答案2

得分: 1

你可以使用来自 https://docs.oracle.com/javase/7/docs/api/java/io/Console.html 的 Console.readPassword()。

英文:

You can use Console.readPassword() from https://docs.oracle.com/javase/7/docs/api/java/io/Console.html

huangapple
  • 本文由 发表于 2020年10月2日 01:51:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64160744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定