本文主要是介绍在命令行应用程序中接受安全登陆,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import java.io.Console;
import java.util.Arrays;public class ConsoleApp {private static final int MAX_LOGINS = 3;//最多输入三次密码public static void main(String[] args) {// TODO Auto-generated method stubConsoleApp app = new ConsoleApp();if(app.login()){System.out.println("Thanks for logging in!");}else{System.out.println("Login failed!");}}private boolean login(){Console console = System.console();//获取Console类的实例boolean isAuthenticated = false;if(console!=null){int count=0;do {char[] pwd = console.readPassword("[%s]","Password:");isAuthenticated = authenticate(pwd);//delete password from memoryArrays.fill(pwd,' ');//清空密码console.writer().write("\n");} while (!isAuthenticated&&++count<MAX_LOGINS);}return isAuthenticated;}private boolean authenticate(char[] passwd){char[] secret = {'M','c','G','R','A','W','H','I','L','L'};//McGRAWHILL为密码if(java.util.Arrays.equals(passwd,secret)){java.util.Arrays.fill(passwd,' ');System.out.println("Authenticated\n");return true;}else{System.out.println("Authentication failed\n");}return false;}}
注意是在命令行里面cmd,
I:
cd 地址
javac xxx.java
java xxx
这篇关于在命令行应用程序中接受安全登陆的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!