简体   繁体   English

Java 1.5命令行密码屏蔽

[英]Java 1.5 Command Line Password Masking

All, 所有,

Our server is running Java 1.5 and I am having difficulty trying to mask user input from the command line. 我们的服务器运行的是Java 1.5,我很难掩盖命令行中的用户输入。 I am executing a jar file (java -jar my.jar) and am working through command line prompts via printlns. 我正在执行一个jar文件(java -jar my.jar),并通过printlns通过命令行提示符进行工作。 I cannot use Java Console. 我无法使用Java控制台。

Thanks 谢谢

The best approach would be to use Java 6's Console readPassword() method. 最好的方法是使用Java 6的Console readPassword()方法。 Since you mentioned that you are using Java 5, that is not an option. 由于您提到使用的是Java 5,因此这不是一种选择。 A lot of Java 6 utilities have been backported to Java 5. I have not found anyone who has done it for this class though. 许多Java 6实用程序已被反向移植到Java5。尽管如此,我还没有找到为该类做过任何事情的人。

This site has a good article on how to do it using Java 5. http://www.devdaily.com/java/edu/pj/pj010005/ . 该站点上有一篇很好的文章,介绍了如何使用Java 5进行操作。http://www.devdaily.com/java/edu/pj/pj010005/ Basically they wrap System.in with an InputStreamReader and read a line. 基本上,它们使用InputStreamReader包装System.in并读取一行。

Here is an interesting article: http://java.sun.com/developer/technicalArticles/Security/pwordmask/ 这是一篇有趣的文章: http : //java.sun.com/developer/technicalArticles/Security/pwordmask/

edit now that I re-read that, I think their command-line "solution" is really stupid. 现在编辑我重新读了,我认为他们的命令行“解决方案”是非常愚蠢的。 What we've got in our application is an auxiliary program to do that, one that understands how to mask input according to the OS (Linux, Windows, whatever). 我们的应用程序中提供的是一个辅助程序来执行此操作,该程序可以了解如何根据OS(Linux,Windows等)屏蔽输入。 The Java code listens for commands on a socket, and the front-end password reader gets the password and anything else needed, then sends commands to the Java code. Java代码侦听套接字上的命令,前端密码读取器获取密码和其他所需的内容,然后将命令发送到Java代码。

On a Unix system? 在Unix系统上? You could try running the system program stty with argument -echo before reading the program and the program stty echo afterwards. 您可以在读取程序之前尝试使用参数-echo运行系统程序stty-echo再执行程序stty echo

ProcessBuilder pb = new ProcessBuilder("stty", "-echo");
pb.start().waitFor();
// Read the password here
pb = new ProcessBuilder("stty", "echo");
pb.start().waitFor();

Note that I have not tried this! 请注意,我还没有尝试过! Some stty programs are happy with it and some aren't (those that aren't need their stdin to come from /dev/tty to work right...) 有些stty程序对此感到满意,而有些则不满意(那些不需要他们的stdin来自/dev/tty可以正常工作...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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