`
runfeel
  • 浏览: 935758 次
文章分类
社区版块
存档分类
最新评论

How to Use Password Fields(密码框的使用)

 
阅读更多

How to Use Password Fields(密码框的使用)

JPasswordField JTextField类的子类,提供了专门的文本输入框作为密码的入口。出于安全的考虑,密码框并不会显示用户所输入的内容。相反,密码框显示的却是和输入不同的字符,例如’*’。作为另外一个安全的机制,密码框是以字符数组来存储内容,而不是字符串。像普通的文本框一样,当用户想要结束输入时,例如按下回车键,密码框会发出一个action类型的事件。

下图是一个打开一个小窗口,并且提示用户输入密码的示例程序的截图:

源程序如下:

其中创建并设置密码框的代码如下:

passwordField = new PasswordField(10);

passwordField.setActionCommand(OK);

passwordField.addActionListener(this);

传给JPasswordField类的构造方法的参数指示了密码输入框的预设大小,此例是10宽。默认情况下,每输入一个字符,密码框就显示一个点号。假如你想改变回显的字符,可以调用setEchoChar方法。上面的代码中然后为密码框增加了一个事件监听器,用以检测用户输入的内容,具体见上面的actionPerformed方法。

安全提示:

尽管JPasswordField类继承了getText方法,但你应该用getPassword方法替代它。不仅仅是getText方法不够安全,而且更近一步说,它可能会显示当前密码框中的可视字符串(例如:”******”)而不是真正键入的字符串。

为了更进一步增强安全性,一旦你不再使用从getPassword方法返回的字符数组,你应该将其没一个元素都设置为0

一个使用密码框的程序通常会在结束任何需要密码的操作之前都会更新密码。这个程序调用了一个私有方法,isPasswordCorrect,用来将从getPassword方法返回的值和存储在字符数组中的值相比对。具体代码如下:

private static boolean isPasswordCorrect(char[] input) {

boolean isCorrect = true;

char[] correctPassword = {'b', 'u', 'g', 'a', 'b', 'o', 'o'};

if (input.length != correctPassword.length) {

isCorrect = false;

} else {

isCorrect = Arrays.equals(input, correctPassword);

}

// 清空正确的密码数组

Arrays.fill(correctPassword, '0');

return isCorrect;

}

总结:相对来说密码框的使用较为容易,但要考虑很多安全性的问题,要注意。

分享到:
评论

相关推荐

    VB编程资源大全(英文源码 API)

    BatteryAPIdemo.zip This example demonstrates how to "To get information about the battery status with out having to use the SysInfo.ocx from MS"<END><br>3 , DisableXexam.zip This example easily...

    Java邮件开发Fundamentals of the JavaMail API

    protocols will help you understand how to use the JavaMail API. While the API is designed to be protocol agnostic, you can't overcome the limitations of the underlying protocols. If a capability isn...

    VB编程资源大全(英文源码 数据库)

    <END><br>47,DataCntl.zip Simple application that shows how to use the Data Control to connect to the Biblio.mdb database and display all authors in the Authors table. <END><br>48,MyTool.zip An ...

    VclZip pro v3.10.1

    Use this with care as this is still somewhat experimental and I'm not sure how useful it is yet. You must make all changes within the buffer sent in to you. Treat the entire file as a stream. Byte ...

    VB编程资源大全(英文源码 ASP)

    With just a few simple steps you can password protect any of your pages without knowing how to code ASP (But it Helps!) You can assign individual access levels or you can assign Group access levels. ...

    Sakemail

    Some stupid mail servers put tabs in some fields (CC:, TO:) when they want to make a new line, the correct is to put at least a space in the beginning of the line, added a little code to "...

    JSP Simple Examples

    In this program we are going to know how the server determines whether the password entered by the user is correct or not. This whole process is controlled on the server side. Multiple forms in jsp ...

    ICS delphixe10源码版

    Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The...

    Android10自动连接WiFi问题的解决

    Here is an example code snippet that demonstrates how to use the `WifiNetworkSpecifier.Builder` class to connect to a WiFi network: ```java WifiNetworkSpecifier specifier = new WifiNetworkSpecifier....

    oa源码 大型oa asp.net 源码

    It demonstrates how to manage user sessions, interact with databases, and handle user interface elements like buttons and text boxes. Additionally, it showcases the use of cookies for storing user ...

    php.ini-development

    If you use constants in your value, and these constants belong to a ; dynamically loaded extension (either a PHP extension or a Zend extension), ; you may only use these constants *after* the line ...

    Network Security: Private Communication in a Public World, Second Edition

    Fields Section 8.5. Mathematics of Rijndael Section 8.6. Elliptic Curve Cryptography Section 8.7. Homework Part 2: Authentication Chapter 9. Overview of Authentication Systems Section 9.1...

    ZendFramework中文文档

    1. Introduction to Zend Framework 1.1. 概述 1.2. 安装 2. Zend_Acl 2.1. 简介 2.1.1. 关于资源(Resource) 2.1.2. 关于角色(Role) 2.1.3. 创建访问控制列表(ACL) 2.1.4. 注册角色(Role) 2.1.5. 定义访问...

    Using Perl For Web Programming.pdf

    How to Make Perl Do CGI H G Understanding CGI Calling a Script from a Form H Getting Information to the Script H Processing QUERY_STRING into Useful Chunks H Using Regular Expressions tr/// and s...

    python3.6.5参考手册 chm

    PEP 529: Change Windows filesystem encoding to UTF-8 PEP 528: Change Windows console encoding to UTF-8 PEP 520: Preserving Class Attribute Definition Order PEP 468: Preserving Keyword Argument ...

Global site tag (gtag.js) - Google Analytics